"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useExpandSiblings = void 0;
var _react = require("react");
var _react2 = require("@xyflow/react");
var _use_node_relationships = require("../hooks/use_node_relationships.hook");
/*
 * Copyright OpenSearch Contributors
 * SPDX-License-Identifier: Apache-2.0
 */

const useExpandSiblings = () => {
  const {
    getNodes,
    getEdges
  } = (0, _react2.useReactFlow)();
  const {
    getAggregateSiblings,
    getParentNodes,
    getChildrenNodes
  } = (0, _use_node_relationships.useNodeRelationships)();
  const applyExpandSiblings = (0, _react.useCallback)(nodeId => {
    const siblings = getAggregateSiblings(nodeId);
    const siblingsIds = siblings.map(sibling => sibling.id);
    const updatedNodes = getNodes().map(node => {
      if (siblingsIds.includes(node.id)) {
        return {
          ...node,
          hidden: false,
          data: {
            ...node.data,
            isStacked: false
          }
        };
      }
      if (node.id === nodeId) {
        return {
          ...node,
          data: {
            ...node.data,
            isStacked: false,
            isTopOfTheStack: false
          }
        };
      }
      return node;
    });
    const parents = getParentNodes(nodeId);
    const parentIds = parents.map(parent => parent.id);
    const children = getChildrenNodes(nodeId);
    const childrenIds = children.map(child => child.id);
    const updatedEdges = getEdges().map(edge => {
      var _edge$data;
      if (siblingsIds.includes(edge.source) && childrenIds.includes(edge.target)) {
        return {
          ...edge,
          hidden: false
        };
      }
      if (parentIds.includes(edge.source) && siblingsIds.includes(edge.target)) {
        return {
          ...edge,
          hidden: false
        };
      }

      // if edge was rewired during collapse of the node
      if ((edge.source === nodeId || edge.target === nodeId) && (_edge$data = edge.data) !== null && _edge$data !== void 0 && _edge$data.original) {
        var _edge$data2;
        return {
          ...((_edge$data2 = edge.data) === null || _edge$data2 === void 0 ? void 0 : _edge$data2.original),
          hidden: false
        };
      }
      return edge;
    });
    return {
      nodes: updatedNodes,
      edges: updatedEdges
    };
  }, [getAggregateSiblings, getParentNodes, getChildrenNodes, getNodes, getEdges]);
  return applyExpandSiblings;
};
exports.useExpandSiblings = useExpandSiblings;