"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.nodeTypes = exports.getLayoutedElements = void 0;

var _reactFlowRenderer = require("@titaui/react-flow-renderer");

var _dagre = _interopRequireDefault(require("@titaui/dagre"));

var _childNode = _interopRequireDefault(require("../child-node"));

var _rootNode = _interopRequireDefault(require("../root-node"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var nodeTypes = {
  ChildNode: _childNode["default"],
  RootNode: _rootNode["default"]
};
exports.nodeTypes = nodeTypes;

var getLayoutedElements = function getLayoutedElements(elements) {
  var direction = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'horizontal';
  var isHorizontal = direction === 'horizontal'; // const g = new dagre.graphlib.Graph({compound: true });

  var g = new _dagre["default"].graphlib.Graph();
  g.setGraph({
    rankdir: isHorizontal ? 'LR' : 'TB',
    nodesep: 12,
    ranksep: 80
  });
  g.setDefaultEdgeLabel(function () {
    return {};
  });
  elements.forEach(function (element) {
    if ((0, _reactFlowRenderer.isNode)(element)) {
      var _element$__rf, _element$__rf2;

      // const { data,id } = element
      // if(id!=="0"){
      //   const {parentId} = data
      //   if(parentId===0||parentId){
      //     g.setParent(id, parentId+"");
      //   }
      // }
      g.setNode(element.id, {
        width: (_element$__rf = element.__rf) === null || _element$__rf === void 0 ? void 0 : _element$__rf.width,
        height: (_element$__rf2 = element.__rf) === null || _element$__rf2 === void 0 ? void 0 : _element$__rf2.height
      });
    } else {
      g.setEdge(element.source, element.target);
    }
  });

  _dagre["default"].layout(g);

  return elements.map(function (element) {
    if ((0, _reactFlowRenderer.isNode)(element)) {
      var node = g.node(element.id);
      return _objectSpread(_objectSpread({}, element), {}, {
        targetPosition: isHorizontal ? 'left' : 'top',
        sourcePosition: isHorizontal ? 'right' : 'bottom',
        position: {
          x: node ? (node === null || node === void 0 ? void 0 : node.x) - (node === null || node === void 0 ? void 0 : node.width) / 2 : 0,
          y: node ? (node === null || node === void 0 ? void 0 : node.y) - (node === null || node === void 0 ? void 0 : node.height) / 2 : 0
        }
      });
    }

    return element;
  });
};

exports.getLayoutedElements = getLayoutedElements;