import _createClass from "@babel/runtime/helpers/createClass";
import { makeAutoObservable } from 'mobx';
import Props from './props';
import { uniqueId } from '../utils';
import SettingTopEntry from '../setting/SettingTopEntry';
var Node = /*#__PURE__*/function () {
  function Node(owner, initSchema, parent) {
    var _this = this;
    this.id = void 0;
    this.componentName = void 0;
    this.isContainer = void 0;
    this.parent = void 0;
    this.owner = void 0;
    this.children = void 0;
    this.props = void 0;
    this.extraProps = void 0;
    this.containerType = void 0;
    this.packageName = void 0;
    this._settingEntry = void 0;
    this.isFormControl = void 0;
    this.inertChildAtIndex = function (node, index) {
      node.parent = _this;
      _this.children.splice(index, 0, node);
    };
    this.insertAfter = function (node, afterNode) {
      node.parent = _this;
      var index = _this.children.findIndex(function (child) {
        return child === afterNode;
      });
      if (index === -1) {
        _this.children.push(node);
      } else {
        _this.children.splice(index, 0, node);
      }
    };
    this.delChildAtIndex = function (index) {
      _this.children.splice(index, 1);
    };
    this.getChildAtIndex = function (index) {
      return _this.children[index];
    };
    this.delChild = function (child) {
      _this.children = _this.children.filter(function (item) {
        return item !== child;
      });
    };
    this.getProp = function (propName, createIfNone) {
      if (createIfNone === void 0) {
        createIfNone = true;
      }
      return _this.props.getProp(propName, createIfNone);
    };
    this.getExtraProp = function (propName, createIfNone) {
      if (createIfNone === void 0) {
        createIfNone = true;
      }
      return _this.extraProps.getProp(propName, createIfNone);
    };
    makeAutoObservable(this, {
      id: false,
      componentName: false,
      isContainer: false,
      owner: false,
      isFormControl: false,
      containerType: false
    });
    this.parent = parent;
    this.id = initSchema.id || uniqueId('node');
    this.componentName = initSchema.componentName;
    this.packageName = initSchema.packageName;
    this.isContainer = initSchema.isContainer;
    this.containerType = initSchema.containerType;
    this.isFormControl = !!initSchema.isFormControl;
    this.owner = owner;
    this.children = initSchema.children.map(function (child) {
      return _this.owner.createNode(child, _this);
    });
    this.props = new Props(this, initSchema.props);
    this.extraProps = new Props(this, initSchema.extraProps);
  }
  var _proto = Node.prototype;
  _proto["export"] = function _export() {
    return {
      id: this.id,
      componentName: this.componentName,
      packageName: this.packageName,
      isContainer: this.isContainer,
      props: this.props["export"](),
      extraProps: this.extraProps["export"](),
      containerType: this.containerType,
      children: this.children.map(function (child) {
        return child["export"]();
      }),
      isFormControl: this.isFormControl
    };
  };
  _createClass(Node, [{
    key: "componentSpec",
    get: function get() {
      var result = this.owner.project.designer.componentSpecMap.get(this.packageName);
      if (!result) {
        throw "\u4E0D\u5B58\u5728 " + this.packageName + " \u7EC4\u4EF6";
      }
      return result;
    }
  }, {
    key: "title",
    get: function get() {
      return this.componentSpec.title;
    }
  }, {
    key: "lastChild",
    get: function get() {
      return this.children[this.children.length - 1];
    }
  }, {
    key: "childrenSize",
    get: function get() {
      return this.children.length;
    }
  }, {
    key: "settingEntry",
    get: function get() {
      if (this._settingEntry) {
        return this._settingEntry;
      }
      this._settingEntry = new SettingTopEntry(this);
      return this._settingEntry;
    }
  }]);
  return Node;
}();
export { Node as default };