"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useCelestialNodeActionsContext = exports.CelestialNodeActionsProvider = void 0;
var _react = _interopRequireWildcard(require("react"));
var _react2 = require("@xyflow/react");
var _celestial_state_context = require("./celestial_state_context");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
/*
 * Copyright OpenSearch Contributors
 * SPDX-License-Identifier: Apache-2.0
 */

const CelestialNodeActions = /*#__PURE__*/(0, _react.createContext)(undefined);
const CelestialNodeActionsProvider = ({
  onDataFetch,
  addBreadcrumb,
  onDashboardClick,
  children
}) => {
  // Track the selected node ID
  const {
    selectedNodeId,
    setSelectedNodeId,
    unstackedAggregateNodeIds,
    setUnstackedAggregateNodeIds
  } = (0, _celestial_state_context.useCelestialStateContext)();
  const {
    getNodes,
    getEdges,
    setNodes,
    setEdges
  } = (0, _react2.useReactFlow)();
  const onGroupToggle = (0, _react.useCallback)((event, props) => {
    event.stopPropagation();
    const groupId = props.id;
    addBreadcrumb === null || addBreadcrumb === void 0 || addBreadcrumb(props.title || groupId, props);
    onDataFetch === null || onDataFetch === void 0 || onDataFetch(props);
    setSelectedNodeId(undefined);
  },
  // eslint-disable-next-line react-hooks/exhaustive-deps
  [onDataFetch, addBreadcrumb]);
  const handleDashboardClick = (0, _react.useCallback)((event, props) => {
    onDashboardClick === null || onDashboardClick === void 0 || onDashboardClick(props);
  }, [onDashboardClick]);

  // Create onClick handler for unstacking nodes
  const onUnstackNode = (0, _react.useCallback)((event, props) => {
    var _getNodes, _getEdges;
    event.stopPropagation();
    if (!props.aggregatedNodeId) return;

    // Find the stacked node and get its stackedNodeIds
    const nodes = (_getNodes = getNodes()) !== null && _getNodes !== void 0 ? _getNodes : [];
    const edges = (_getEdges = getEdges()) !== null && _getEdges !== void 0 ? _getEdges : [];
    const hasStackedNodes = nodes.some(node => {
      return node.data.aggregatedNodeId === props.aggregatedNodeId && node.hidden === true;
    });
    if (!hasStackedNodes) return;
    const updatedNodeIds = [];
    // Find the individual nodes that were stacked
    const updatedNodes = nodes.map(node => {
      if (node.data.aggregatedNodeId === props.aggregatedNodeId) {
        updatedNodeIds.push(node.id);
        return {
          ...node,
          hidden: false
        };
      }
      return node;
    });
    const updatedEdges = edges.map(edge => {
      if (updatedNodeIds.includes(edge.source) || updatedNodeIds.includes(edge.target)) {
        return {
          ...edge,
          hidden: false
        };
      }
      return edge;
    });

    // Handle edges: restore hidden edges and hide stacked edge
    setUnstackedAggregateNodeIds([...unstackedAggregateNodeIds, props.aggregatedNodeId]);
    setNodes(updatedNodes);
    setEdges(updatedEdges);
  },
  // eslint-disable-next-line react-hooks/exhaustive-deps
  [setNodes, setEdges, unstackedAggregateNodeIds, setUnstackedAggregateNodeIds]);
  const onStackNode = (0, _react.useCallback)((event, props) => {
    var _getNodes2, _getEdges2;
    event.preventDefault();
    if (!props.aggregatedNodeId) return;
    const nodes = (_getNodes2 = getNodes()) !== null && _getNodes2 !== void 0 ? _getNodes2 : [];
    const edges = (_getEdges2 = getEdges()) !== null && _getEdges2 !== void 0 ? _getEdges2 : [];
    const hasNodesToStack = nodes.some(node => {
      return node.data.aggregatedNodeId === props.aggregatedNodeId && node.id !== props.id;
    });
    if (!hasNodesToStack) return;

    // Find the unstacked parent node
    const unStackedNodes = nodes.filter(node => node.data.aggregateId === props.aggregatedNodeId && props.id !== node.id).map(node => node.id);
    const updatedNodes = nodes.map(node => {
      if (unStackedNodes.includes(node.id)) {
        return {
          ...node,
          hidden: true
        };
      }
      return node;
    });
    const updateEdges = edges.map(edge => {
      if (unStackedNodes.includes(edge.source) || unStackedNodes.includes(edge.target)) {
        return {
          ...edge,
          hidden: true
        };
      }
      return edge;
    });
    setNodes(updatedNodes);
    setEdges(updateEdges);
  },
  // eslint-disable-next-line react-hooks/exhaustive-deps
  [setNodes, setEdges]);
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(CelestialNodeActions.Provider, {
    value: {
      selectedNodeId,
      onGroupToggle,
      onDashboardClick: handleDashboardClick,
      onUnstackNode,
      onStackNode
    },
    children: children
  });
};
exports.CelestialNodeActionsProvider = CelestialNodeActionsProvider;
const useCelestialNodeActionsContext = () => {
  const context = (0, _react.useContext)(CelestialNodeActions);
  if (context === undefined) {
    throw new Error('useCelestialNodeActionsContext must be used within a CelestialNodeActionsProvider');
  }
  return context;
};
exports.useCelestialNodeActionsContext = useCelestialNodeActionsContext;