import "core-js/modules/es6.number.constructor";
import _Object$assign from "@babel/runtime-corejs2/core-js/object/assign";
import _inheritsLoose from "../../../inheritsLoose";
import _defineProperty from "@babel/runtime-corejs2/helpers/defineProperty";
import Intact from 'intact-angular';
import { mapChildren } from '../../utils';
import { DShape } from '../shapes/shape';
export var DLayout =
/*#__PURE__*/
function (_Intact) {
  _inheritsLoose(DLayout, _Intact);

  function DLayout() {
    return _Intact.apply(this, arguments) || this;
  }

  var _proto = DLayout.prototype;

  _proto.defaults = function defaults() {
    return {
      left: 0,
      top: 0,
      _diagram: undefined,
      _parent: undefined
    };
  };

  _proto._init = function _init() {
    this.shapes = [];
  };

  _proto._mount = function _mount() {
    this.get('_diagram').addLayout(this);
  };

  _proto.addShape = function addShape(shape) {
    this.shapes.push(shape);
  };

  _proto.deleteShape = function deleteShape(shape) {
    this.shapes.splice(this.shapes.indexOf(shape), 1);
  };

  _proto.draw = function draw() {
    var _this$get = this.get(),
        diagram = _this$get._diagram,
        parent = _this$get._parent,
        marginRight = _this$get.marginRight,
        marginBottom = _this$get.marginBottom,
        left = _this$get.left,
        top = _this$get.top;

    var graph = diagram.graph;

    var layout = this._getLayout(graph); // Allows the layout to move cells even though cells
    // aren't movable in the graph


    layout.isVertexMovable = function () {
      return true;
    };

    var cells = this.shapes.map(function (shape) {
      return shape.cell;
    });

    this._execute(layout, cells, parent, graph); // move cells totally


    graph.moveCells(cells, +left, +top);
  };

  _proto._getLayout = function _getLayout(graph) {};

  _proto._execute = function _execute(layout, cells, parent, graph) {
    layout.execute(parent.cell, cells);
  };

  _proto._destroy = function _destroy() {
    this.get('_diagram').deleteLayout(this);
  };

  return DLayout;
}(Intact);

_defineProperty(DLayout, "template", function (data, Vdt) {
  var children = mapChildren(data.get('children'), function (vNode) {
    if (vNode.tag && vNode.tag.prototype instanceof DShape) {
      vNode.props = _Object$assign({}, vNode.props, {
        _diagram: data.get('_diagram'),
        _parent: data.get('_parent'),
        _layout: data
      });
    } else {
      /* istanbul ignore next */
      Intact.utils.error(new Error("DLayout can only contains DShape, but " + vNode.tag + " found."));
    }

    return vNode;
  });
  return Vdt.miss.h(data.constructor.displayName, null, children);
});

_defineProperty(DLayout, "propTypes", {
  left: [Number, String],
  top: [Number, String]
});