"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useContextMenuActions = void 0;
var _react = require("@xyflow/react");
var _react2 = require("react");
var _use_node_relationships = require("../../shared/hooks/use_node_relationships.hook");
var _use_collapse_siblings = require("../../shared/strategies/use_collapse_siblings.hook");
var _use_expand_siblings = require("../../shared/strategies/use_expand_siblings.hook");
var _use_expand_children = require("../../shared/strategies/use_expand_children.hook");
var _use_collapse_descendants = require("../../shared/strategies/use_collapse_descendants.hook");
/*
 * Copyright OpenSearch Contributors
 * SPDX-License-Identifier: Apache-2.0
 */

/**
 * Custom hook that provides context menu actions for node manipulation
 *
 * This hook encapsulates the logic for various node operations that can be triggered
 * from context menus or action buttons. It handles:
 * - Expanding/collapsing sibling nodes (stacking/unstacking)
 * - Expanding children nodes (showing hidden descendants)
 * - Collapsing descendant nodes (hiding child hierarchies)
 *
 * All actions update the React Flow state and optionally close the context menu.
 *
 * @param nodeId - The ID of the node these actions will operate on
 * @param onClose - Optional callback to close the context menu after action execution
 * @returns Object containing all available action handlers
 */
const useContextMenuActions = (nodeId, onClose) => {
  // React Flow state setters for updating nodes and edges
  const {
    setNodes,
    setEdges
  } = (0, _react.useReactFlow)();

  // Hook for checking node relationship properties
  const {
    isAggregateNode
  } = (0, _use_node_relationships.useNodeRelationships)();

  // Strategy hooks for different node manipulation operations
  const applyCollapseSiblings = (0, _use_collapse_siblings.useCollapseSiblings)();
  const applyExpandSiblings = (0, _use_expand_siblings.useExpandSiblings)();
  const applyExpandChildren = (0, _use_expand_children.useExpandChildren)();
  const applyCollapseDescendants = (0, _use_collapse_descendants.useCollapseDescendants)();

  /**
   * Expands sibling nodes (unstacking operation)
   *
   * This action reveals sibling nodes that were previously stacked/collapsed together.
   * Only works on aggregate nodes that have siblings to expand.
   *
   * @param event - Mouse event from the action trigger
   */
  const onExpandSiblings = (0, _react2.useCallback)(event => {
    event.preventDefault();

    // Only expand siblings if this is an aggregate node with siblings
    if (isAggregateNode(nodeId)) {
      const {
        nodes,
        edges
      } = applyExpandSiblings(nodeId);
      setNodes(nodes);
      setEdges(edges);
    }
    onClose === null || onClose === void 0 || onClose();
  }, [nodeId, onClose, applyExpandSiblings, setNodes, setEdges, isAggregateNode]);

  /**
   * Collapses sibling nodes (stacking operation)
   *
   * This action groups sibling nodes together into a visual stack,
   * hiding individual siblings while maintaining their relationships.
   * Only works on aggregate nodes that have siblings to collapse.
   *
   * @param event - Mouse event from the action trigger
   */
  const onCollapseSiblings = (0, _react2.useCallback)(event => {
    event.preventDefault();

    // Only collapse siblings if this is an aggregate node with siblings
    if (isAggregateNode(nodeId)) {
      const {
        nodes,
        edges
      } = applyCollapseSiblings(nodeId);
      setNodes(nodes);
      setEdges(edges);
    }
    onClose === null || onClose === void 0 || onClose();
  }, [nodeId, onClose, applyCollapseSiblings, setNodes, setEdges, isAggregateNode]);

  /**
   * Expands child nodes
   *
   * This action reveals child nodes that were previously hidden/collapsed.
   * Shows the immediate children of the target node, making them visible
   * in the diagram along with their connecting edges.
   *
   * @param event - Mouse event from the action trigger
   */
  const onExpandChildren = (0, _react2.useCallback)(event => {
    event.preventDefault();

    // Apply the expand children strategy and update React Flow state
    const {
      nodes,
      edges
    } = applyExpandChildren(nodeId);
    setNodes(nodes);
    setEdges(edges);
    onClose === null || onClose === void 0 || onClose();
  }, [nodeId, applyExpandChildren, onClose, setNodes, setEdges]);

  /**
   * Collapses descendant nodes
   *
   * This action hides all descendant nodes (children, grandchildren, etc.)
   * of the target node, effectively collapsing the entire subtree.
   * Useful for simplifying complex hierarchical diagrams.
   *
   * @param event - Mouse event from the action trigger
   */
  const onCollapseDescendants = (0, _react2.useCallback)(event => {
    event.preventDefault();

    // Apply the collapse descendants strategy and update React Flow state
    const {
      nodes,
      edges
    } = applyCollapseDescendants(nodeId);
    setNodes(nodes);
    setEdges(edges);
    onClose === null || onClose === void 0 || onClose();
  }, [nodeId, applyCollapseDescendants, onClose, setNodes, setEdges]);

  // Memoize the returned object to prevent unnecessary re-renders
  return (0, _react2.useMemo)(() => ({
    onExpandSiblings,
    onCollapseSiblings,
    onExpandChildren,
    onCollapseDescendants
  }), [onExpandSiblings, onCollapseSiblings, onExpandChildren, onCollapseDescendants]);
};
exports.useContextMenuActions = useContextMenuActions;