"use strict";

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

var _g = _interopRequireDefault(require("@antv/g6"));

var _options = _interopRequireDefault(require("./utils/options"));

var _tweak = _interopRequireDefault(require("./inner/tweak"));

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

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

// eslint-disable-next-line @typescript-eslint/no-explicit-any
var isEmpty = function isEmpty(data) {
  if (data && data.nodes && data.nodes.length !== 0) {
    return false;
  }

  return true;
};

var FORCE_LAYOUTS = ['force', 'graphin-force', 'g6force', 'gForce', 'comboForce'];

var LayoutController = /*#__PURE__*/function () {
  function LayoutController(context) {
    var _this = this;

    _classCallCheck(this, LayoutController);

    /** 更新布局参数 */
    this.updateOptions = function () {
      var DEFAULT_LAYOUT = {
        type: 'concentric'
      };
      var _this$graphin = _this.graphin,
          width = _this$graphin.width,
          height = _this$graphin.height,
          props = _this$graphin.props;
      var _props$layout = props.layout,
          layout = _props$layout === void 0 ? DEFAULT_LAYOUT : _props$layout;
      /** 如果数据为空，不进行布局 */

      var _layout$type = layout.type,
          type = _layout$type === void 0 ? 'concentric' : _layout$type,
          preset = layout.preset;
      /** 通用布局参数 */

      var commonLayoutParams = {
        width: width,
        height: height,
        center: [width / 2, height / 2]
      };
      /**  */

      _this.options = Object.assign(Object.assign(Object.assign({}, commonLayoutParams), _options.default[type] || {}), layout);

      if (isEmpty(_this.graphin.data)) {
        _this.prevOptions = {};
        return;
      }
      /** 力导布局有前置布局的概念，特殊处理 */


      if (FORCE_LAYOUTS.indexOf(type) !== -1) {
        // 布局切换产生的prevType 是最低优先级
        var presetType = _this.prevOptions.type || 'grid';

        if (preset === null || preset === void 0 ? void 0 : preset.type) {
          // 用户给的preset.type是第一优先级
          presetType = preset.type;
        }

        if (isEmpty(_this.graphin.data)) {
          // 特殊场景处理，不带preset的力导，第二次渲染
          presetType = preset.type || 'grid';
        }

        _this.options.preset = Object.assign(Object.assign(Object.assign({
          type: presetType
        }, commonLayoutParams), _options.default[presetType] || {}), preset);
      }
    };

    this.processForce = function () {
      var options = _this.options,
          graphin = _this.graphin;
      var graph = graphin.graph;
      var type = options.type;

      if (type === 'graphin-force') {
        _this.options.graph = _this.graph;
      }

      if (type === 'force' || type === 'g6force' || type === 'gForce') {
        var onTick = _this.options.onTick;

        var tick = function tick() {
          if (onTick) {
            onTick();
          }

          graph.refreshPositions();
        };

        _this.options.tick = tick;
        var onLayoutEnd = _this.options.onLayoutEnd;

        _this.options.onLayoutEnd = function () {
          if (onLayoutEnd) {
            onLayoutEnd();
          }

          graph.emit('afterlayout');
        };
      }

      if (_this.type === 'comboForce') {
        _this.options.comboTrees = graph.get('comboTrees');
      }

      var isForceLayout = {
        prev: FORCE_LAYOUTS.indexOf(_this.prevOptions.type) !== -1,
        current: FORCE_LAYOUTS.indexOf(_this.options.type) !== -1
      };
      var isSameLayoutType = _this.options.type === _this.prevOptions.type;

      if (isEmpty(graphin.data)) {
        return;
      }

      if (isForceLayout.current && !isSameLayoutType) {
        /**
         * 当前布局为force，且两次布局类型不一致
         * 应当设置当前布局的preset为前一个布局
         */
        var preset = _this.options.preset;
        _this.presetLayout = new _g.default.Layout[preset.type](Object.assign({}, preset) || {});

        _this.presetLayout.init(graphin.data);

        _this.presetLayout.execute();

        _this.presetLayout.data = Object.assign({}, graphin.data);
      }

      if (isForceLayout.current && isForceLayout.prev) {
        /**
         * 当前布局类型为force， 前一次布局也为force
         * 渐进布局
         */
        var prevData = _this.graph.save(); // 必须从graph上取数据的原因是，用户可能拖拽改变数据


        var _preset = _this.options.preset;

        if (isEmpty(prevData)) {
          // preset.type = 'grid';
          _this.presetLayout = new _g.default.Layout[_preset.type](Object.assign({}, _preset) || {});

          _this.presetLayout.init(graphin.data);

          _this.presetLayout.execute();

          prevData = graphin.data;
        }

        graphin.data = (0, _tweak.default)(graphin.data, prevData);
      }
      /** 布局切换 */

    };

    this.refreshPosition = function () {
      var animate = _this.graphin.options.animate;

      if (animate) {
        _this.graph.positionsAnimate();
      } else {
        _this.graph.refreshPositions();
      }
    };

    this.destroy = function () {
      if (_this.presetLayout && _this.presetLayout.destroy) {
        _this.presetLayout.destroy();
      }

      if (_this.instance && _this.instance.destroy) {
        _this.instance.destroy();
      }

      _this.presetLayout = null;
      _this.instance = null;
    };

    this.getDataFromGraph = function () {
      var nodes = [];
      var edges = [];
      var combos = [];

      var nodeItems = _this.graph.getNodes();

      var edgeItems = _this.graph.getEdges();

      var comboItems = _this.graph.getCombos();

      var nodeLength = nodeItems.length;

      for (var i = 0; i < nodeLength; i++) {
        var nodeItem = nodeItems[i];

        if (nodeItem && nodeItem.isVisible()) {
          var model = nodeItem.getModel();
          nodes.push(model);
        }
      }

      var edgeLength = edgeItems.length;

      for (var _i = 0; _i < edgeLength; _i++) {
        var edgeItem = edgeItems[_i];

        if (edgeItem && edgeItem.isVisible()) {
          var _model = edgeItem.getModel();

          if (!_model.isComboEdge) {
            edges.push(_model);
          }
        }
      }

      var comboLength = comboItems.length;

      for (var _i2 = 0; _i2 < comboLength; _i2++) {
        var comboItem = comboItems[_i2];

        if (comboItem && comboItem.isVisible()) {
          var _model2 = comboItem.getModel();

          combos.push(_model2);
        }
      }

      return {
        nodes: nodes,
        edges: edges,
        combos: combos
      };
    };

    this.graphin = context;
    this.graph = this.graphin.graph;
    this.presetLayout = null;
    this.prevOptions = {};
    this.init();
  }
  /**
   * 初始化布局
   */


  _createClass(LayoutController, [{
    key: "init",
    value: function init() {
      /** 更新布局参数 */
      this.updateOptions();
      var options = this.options,
          graphin = this.graphin;
      var data = graphin.data;
      var type = options.type;
      /** 力导布局特殊处理 */

      this.processForce();

      if (!_g.default.Layout[type]) {
        console.warn("".concat(type, " layout not found, current layout is grid"));
      }

      var LayoutClass = _g.default.Layout[type] || _g.default.Layout.grid;
      this.graph.emit('beforelayout');
      this.instance = new LayoutClass(this.options);
      this.instance.init(data);
    }
    /** 启动布局 */

  }, {
    key: "start",
    value: function start() {
      this.instance.execute();
      this.graph.emit('afterlayout');
    }
    /** 重新布局 */

  }, {
    key: "changeLayout",
    value: function changeLayout() {
      var _this$graphin2 = this.graphin,
          graph = _this$graphin2.graph,
          data = _this$graphin2.data,
          isTree = _this$graphin2.isTree;

      if (!graph || graph.destroyed || !data || !data.nodes || !data.nodes.length || isTree) {
        return false;
      }

      if (FORCE_LAYOUTS.indexOf(this.options.type) !== -1) {
        this.destroy();
      }
      /** 设置前置布局参数 */


      this.prevOptions = Object.assign({}, this.options);
      /** 重新走初始化流程 */

      this.init();
      this.start();
    }
  }]);

  return LayoutController;
}();

var _default = LayoutController;
exports.default = _default;