"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useLayoutControls = void 0;
var _react = require("@xyflow/react");
var _react2 = require("react");
var _use_celestial_layout = require("../../shared/hooks/use_celestial_layout.hook");
var _celestial_state_context = require("../../shared/contexts/celestial_state_context");
/*
 * Copyright OpenSearch Contributors
 * SPDX-License-Identifier: Apache-2.0
 */

const useLayoutControls = () => {
  const {
    layoutOptions
  } = (0, _celestial_state_context.useCelestialStateContext)();
  const {
    getLaidOutElements
  } = (0, _use_celestial_layout.useCelestialLayout)(layoutOptions);
  const {
    getNodes,
    setNodes,
    getEdges,
    setEdges,
    fitView
  } = (0, _react.useReactFlow)();

  /**
   * Handler for zooming in the view
   * @param e Mouse event from zoom button click
   */
  const onLayoutChange = (0, _react2.useCallback)(e => {
    e.stopPropagation();
    const nodes = getNodes();
    const edges = getEdges();
    const layout = getLaidOutElements(nodes, edges);
    setNodes(layout.nodes);
    setEdges(layout.edges);
    fitView({
      duration: 300
    });
  },
  // eslint-disable-next-line react-hooks/exhaustive-deps
  [getLaidOutElements, getNodes, setNodes, getEdges, setEdges]);
  const onExpandAll = (0, _react2.useCallback)(e => {
    e.stopPropagation();
    const nodes = getNodes().map(node => ({
      ...node,
      hidden: false
    }));
    const edges = getEdges().map(edge => ({
      ...edge,
      hidden: false
    }));
    const layout = getLaidOutElements(nodes, edges);
    setNodes(layout.nodes);
    setEdges(layout.edges);
    fitView({
      duration: 300
    });
  },
  // eslint-disable-next-line react-hooks/exhaustive-deps
  [getNodes, getEdges, setNodes, setEdges, fitView]);
  return {
    onLayoutChange,
    onExpandAll
  };
};
exports.useLayoutControls = useLayoutControls;