"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = void 0;
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var ComponentSpec = exports["default"] = /*#__PURE__*/function () {
  function ComponentSpec(componentSpecRaw) {
    var _this = this;
    this.configure = [];
    this.rawData = void 0;
    this.extraProps = void 0;
    this.parseRawData = function () {
      _this.genConfigure();
      _this.initExtraProps();
    };
    this.initExtraProps = function () {
      var _this$rawData$advance, _this$rawData$advance2, _this$rawData$advance3, _this$rawData$advance4;
      // 将取值路径、name 和 id 放在 extraProps 中
      _this.extraProps = {
        id: {
          type: 'JSRunFunction',
          value: "node => node.id"
        }
      };
      if (((_this$rawData$advance = _this.rawData.advanced) === null || _this$rawData$advance === void 0 ? void 0 : (_this$rawData$advance2 = _this$rawData$advance.component) === null || _this$rawData$advance2 === void 0 ? void 0 : _this$rawData$advance2.containerType) !== 'Page') {
        _this.extraProps.pathToVal = '';
      }
      if ((_this$rawData$advance3 = _this.rawData.advanced) !== null && _this$rawData$advance3 !== void 0 && (_this$rawData$advance4 = _this$rawData$advance3.component) !== null && _this$rawData$advance4 !== void 0 && _this$rawData$advance4.isFormControl) {
        _this.extraProps.name = '';
      }
      if (_this.hasDataSource) {
        _this.extraProps.dataSource = {
          type: 'DataSource',
          value: {
            url: '',
            method: 'GET',
            requestHandler: {
              type: 'JSFunction',
              value: 'function requestHandler(params){return params}'
            },
            responseHandler: {
              type: 'JSFunction',
              value: 'function responseHandler(response) { return response.data }'
            }
          }
        };
      }
      if (_this.hasHiddenRule) {
        _this.extraProps.isHidden = {
          type: 'JSFunction',
          value: 'function isHidden(pageData, containerData, formData){ return false }'
        };
      }
      if (_this.isFormControl) {
        _this.extraProps.isDisabled = {
          type: 'JSFunction',
          value: 'function isDisabled(pageData, containerData, formData){ return false }'
        };
        _this.extraProps.getValue = {
          type: 'JSFunction',
          value: ''
        };
      }
    };
    this.genConfigure = function () {
      var _this$rawData$advance5;
      _this.configure.push(_this.getPropsConfig());
      var supports = (_this$rawData$advance5 = _this.rawData.advanced) === null || _this$rawData$advance5 === void 0 ? void 0 : _this$rawData$advance5.supports;
      if (supports !== null && supports !== void 0 && supports.styles) {
        _this.configure.push({
          type: 'group',
          title: '样式',
          name: 'style',
          fields: [{
            type: 'field',
            name: 'style',
            setters: [{
              name: 'StyleSetter'
            }]
          }]
        });
      }
      if (_this.hasDataSource) {
        _this.configure.push({
          type: 'group',
          title: '数据源',
          name: 'dataSource',
          fields: [{
            type: 'field',
            title: 'tip',
            name: 'tip',
            isExtra: true,
            setters: [{
              name: 'TextSetter',
              props: {
                defaultValue: '这里的配置将发送网络请求，它返回的数据优先级高于从父容器取值',
                style: {
                  color: 'red',
                  fontSize: '12px'
                }
              }
            }]
          }, {
            type: 'field',
            title: 'URL',
            name: 'url',
            parentName: 'dataSource',
            isExtra: true,
            setters: [{
              name: 'StringSetter'
            }]
          }, {
            type: 'field',
            title: '请求方式',
            name: 'method',
            parentName: 'dataSource',
            isExtra: true,
            setters: [{
              name: 'RadioGroupSetter',
              props: {
                options: [{
                  label: 'GET',
                  value: 'GET'
                }, {
                  label: 'POST',
                  value: 'POST'
                }]
              }
            }]
          }, {
            type: 'field',
            title: '请求处理器',
            parentName: 'dataSource',
            name: 'requestHandler',
            isExtra: true,
            setters: [{
              name: 'FunctionSetter'
            }]
          }, {
            type: 'field',
            title: '响应处理器',
            parentName: 'dataSource',
            name: 'responseHandler',
            isExtra: true,
            setters: [{
              name: 'FunctionSetter'
            }]
          }]
        });
      }
      if (_this.hasLinkage) {
        _this.configure.push(_this.getLinkageConfig());
      }
      if (_this.isFormControl) {
        _this.configure.push(_this.getVerifyConfig());
      }
    };
    this.getPropsConfig = function () {
      var getSettersName = function getSettersName(setterConfig) {
        if (!Array.isArray(setterConfig)) {
          setterConfig = [setterConfig];
        }
        return setterConfig.map(function (config) {
          return {
            name: config.isUseSelf ? _this.rawData.packageName + '/' + config.name : config.name,
            props: config.props
          };
        });
      };
      var getExtraPropsConfig = function getExtraPropsConfig() {
        var _this$rawData$advance6, _this$rawData$advance7, _this$rawData$advance8, _this$rawData$advance9;
        var extraPropsConfig = [];
        extraPropsConfig.push({
          type: 'field',
          title: 'ID',
          name: 'id',
          isExtra: true,
          setters: [{
            name: 'TextSetter',
            props: {
              style: {
                color: '#999'
              }
            }
          }]
        });
        if (((_this$rawData$advance6 = _this.rawData.advanced) === null || _this$rawData$advance6 === void 0 ? void 0 : (_this$rawData$advance7 = _this$rawData$advance6.component) === null || _this$rawData$advance7 === void 0 ? void 0 : _this$rawData$advance7.containerType) !== 'Page') {
          extraPropsConfig.push({
            type: 'field',
            title: '取值路径',
            name: 'pathToVal',
            isExtra: true,
            setters: [{
              name: 'StringSetter'
            }]
          });
        }
        if ((_this$rawData$advance8 = _this.rawData.advanced) !== null && _this$rawData$advance8 !== void 0 && (_this$rawData$advance9 = _this$rawData$advance8.component) !== null && _this$rawData$advance9 !== void 0 && _this$rawData$advance9.isFormControl) {
          extraPropsConfig.push({
            type: 'field',
            title: 'name',
            name: 'name',
            isExtra: true,
            setters: [{
              name: 'StringSetter'
            }]
          });
        }
        return extraPropsConfig;
      };
      return {
        type: 'group',
        title: '属性',
        name: 'props',
        fields: [].concat(getExtraPropsConfig(), _this.rawData.props.filter(function (prop) {
          return prop.name !== 'style';
        }).map(function (prop) {
          return {
            type: 'field',
            name: prop.name,
            title: prop.description || prop.name,
            setters: getSettersName(prop.setter)
          };
        }))
      };
    };
    this.getLinkageConfig = function () {
      var getHiddenRule = function getHiddenRule() {
        if (_this.hasHiddenRule) {
          return {
            type: 'field',
            title: '隐藏',
            name: 'isHidden',
            isExtra: true,
            setters: [{
              name: 'FunctionSetter'
            }]
          };
        }
      };
      var getDisableRule = function getDisableRule() {
        if (_this.isFormControl) {
          return {
            type: 'field',
            title: '禁用',
            name: 'isDisabled',
            isExtra: true,
            setters: [{
              name: 'FunctionSetter'
            }]
          };
        }
      };
      var getValueRule = function getValueRule() {
        if (_this.isFormControl) {
          return {
            type: 'field',
            title: '求值',
            name: 'getValue',
            isExtra: true,
            setters: [{
              name: 'FunctionSetter'
            }]
          };
        }
      };
      var hiddenRule = getHiddenRule();
      var disableRule = getDisableRule();
      var valueRule = getValueRule();
      var config = {
        type: 'group',
        title: '联动',
        name: 'linkage',
        fields: []
      };
      if (hiddenRule) {
        config.fields.push(hiddenRule);
      }
      if (disableRule) {
        config.fields.push(disableRule);
      }
      if (valueRule) {
        config.fields.push(valueRule);
      }
      return config;
    };
    this.getVerifyConfig = function () {
      var config = {
        type: 'group',
        title: '校验',
        name: 'verify',
        fields: [{
          type: 'field',
          isExtra: true,
          name: 'verifyRules',
          setters: [{
            name: 'VerifyRulesSetter'
          }]
        }]
      };
      return config;
    };
    this.rawData = componentSpecRaw;
    this.parseRawData();
  }
  var _proto = ComponentSpec.prototype;
  _proto.isCanInclude = function isCanInclude(componentSpec) {
    var _this$childWhitelist,
      _componentSpec$parent,
      _this$childWhitelist2,
      _componentSpec$parent2,
      _this2 = this;
    if (((_this$childWhitelist = this.childWhitelist) === null || _this$childWhitelist === void 0 ? void 0 : _this$childWhitelist.length) === 0 && ((_componentSpec$parent = componentSpec.parentWhitelist) === null || _componentSpec$parent === void 0 ? void 0 : _componentSpec$parent.length) === 0) {
      return false;
    }
    var notMatchChild = (_this$childWhitelist2 = this.childWhitelist) === null || _this$childWhitelist2 === void 0 ? void 0 : _this$childWhitelist2.every(function (item) {
      // 使用模糊匹配
      if (item.includes('*')) {
        item = item.replace(/\*/g, '.');
        var reg = new RegExp(item, 'i');
        return !reg.test(componentSpec.componentName);
      } else {
        return item !== componentSpec.componentName;
      }
    });
    var notMatchParent = (_componentSpec$parent2 = componentSpec.parentWhitelist) === null || _componentSpec$parent2 === void 0 ? void 0 : _componentSpec$parent2.every(function (item) {
      // 使用模糊匹配
      if (item.includes('*')) {
        item = item.replace(/\*/g, '.');
        var reg = new RegExp(item, '');
        return !reg.test(_this2.componentName);
      } else {
        return item !== _this2.componentName;
      }
    });
    if (notMatchChild || notMatchParent) {
      return false;
    }
    return true;
  };
  (0, _createClass2["default"])(ComponentSpec, [{
    key: "parentWhitelist",
    get: function get() {
      var _this$rawData$advance10, _this$rawData$advance11;
      return (_this$rawData$advance10 = this.rawData.advanced) === null || _this$rawData$advance10 === void 0 ? void 0 : (_this$rawData$advance11 = _this$rawData$advance10.nestingRule) === null || _this$rawData$advance11 === void 0 ? void 0 : _this$rawData$advance11.parentWhitelist;
    }
  }, {
    key: "childWhitelist",
    get: function get() {
      var _this$rawData$advance12, _this$rawData$advance13;
      return (_this$rawData$advance12 = this.rawData.advanced) === null || _this$rawData$advance12 === void 0 ? void 0 : (_this$rawData$advance13 = _this$rawData$advance12.nestingRule) === null || _this$rawData$advance13 === void 0 ? void 0 : _this$rawData$advance13.childWhitelist;
    }
  }, {
    key: "componentName",
    get: function get() {
      return this.rawData.componentName;
    }
  }, {
    key: "unableDel",
    get: function get() {
      var _this$rawData$advance14, _this$rawData$advance15;
      return ((_this$rawData$advance14 = this.rawData.advanced) === null || _this$rawData$advance14 === void 0 ? void 0 : (_this$rawData$advance15 = _this$rawData$advance14.component) === null || _this$rawData$advance15 === void 0 ? void 0 : _this$rawData$advance15.containerType) === 'Page';
    }
  }, {
    key: "unableCopy",
    get: function get() {
      var _this$rawData$advance16, _this$rawData$advance17;
      return ((_this$rawData$advance16 = this.rawData.advanced) === null || _this$rawData$advance16 === void 0 ? void 0 : (_this$rawData$advance17 = _this$rawData$advance16.component) === null || _this$rawData$advance17 === void 0 ? void 0 : _this$rawData$advance17.containerType) === 'Page';
    }
  }, {
    key: "unableMove",
    get: function get() {
      var _this$rawData$advance18, _this$rawData$advance19;
      return ((_this$rawData$advance18 = this.rawData.advanced) === null || _this$rawData$advance18 === void 0 ? void 0 : (_this$rawData$advance19 = _this$rawData$advance18.component) === null || _this$rawData$advance19 === void 0 ? void 0 : _this$rawData$advance19.containerType) === 'Page';
    }
  }, {
    key: "title",
    get: function get() {
      return this.rawData.title;
    }
  }, {
    key: "hasDataSource",
    get: function get() {
      var _this$rawData$advance20, _this$rawData$advance21;
      return (_this$rawData$advance20 = this.rawData.advanced) === null || _this$rawData$advance20 === void 0 ? void 0 : (_this$rawData$advance21 = _this$rawData$advance20.component) === null || _this$rawData$advance21 === void 0 ? void 0 : _this$rawData$advance21.isContainer;
    }
  }, {
    key: "hasLinkage",
    get: function get() {
      return this.hasHiddenRule || this.isFormControl;
    }
  }, {
    key: "isFormControl",
    get: function get() {
      var _this$rawData$advance22, _this$rawData$advance23;
      return (_this$rawData$advance22 = this.rawData.advanced) === null || _this$rawData$advance22 === void 0 ? void 0 : (_this$rawData$advance23 = _this$rawData$advance22.component) === null || _this$rawData$advance23 === void 0 ? void 0 : _this$rawData$advance23.isFormControl;
    }
  }, {
    key: "hasHiddenRule",
    get: function get() {
      var _this$rawData$advance24, _this$rawData$advance25;
      return !((_this$rawData$advance24 = this.rawData.advanced) !== null && _this$rawData$advance24 !== void 0 && (_this$rawData$advance25 = _this$rawData$advance24.component) !== null && _this$rawData$advance25 !== void 0 && _this$rawData$advance25.isContainer) || this.rawData.advanced.component.containerType !== 'Page';
    }
  }, {
    key: "type",
    get: function get() {
      return this.rawData.group === 'template' ? 'template' : 'component';
    }

    /**
     * 这个是初始 schema
     */
  }, {
    key: "schema",
    get: function get() {
      // 模板的第一层不表示任何组件，从第二层开始才表示组件
      if (this.type === 'template') {
        // 从第二层开始
        return (this.rawData.children || []).map(function (child) {
          var childSpec = new ComponentSpec(child);
          return childSpec.schema;
        });
      } else {
        var _this$rawData$advance26, _this$rawData$advance27, _this$rawData$advance28, _this$rawData$advance29, _this$rawData$advance30, _this$rawData$advance31, _this$rawData$advance32;
        var props = {};
        this.rawData.props.forEach(function (prop) {
          props[prop.name] = prop.defaultValue;
        });
        var supports = (_this$rawData$advance26 = this.rawData.advanced) === null || _this$rawData$advance26 === void 0 ? void 0 : _this$rawData$advance26.supports;
        if (supports !== null && supports !== void 0 && supports.styles) {
          props['style'] = '';
        }
        var children = (this.rawData.children || []).map(function (child) {
          var childSpec = new ComponentSpec(child);
          return childSpec.schema;
        });
        return {
          componentName: this.componentName,
          props: props,
          extraProps: this.extraProps,
          isContainer: !!((_this$rawData$advance27 = this.rawData.advanced) !== null && _this$rawData$advance27 !== void 0 && (_this$rawData$advance28 = _this$rawData$advance27.component) !== null && _this$rawData$advance28 !== void 0 && _this$rawData$advance28.isContainer),
          children: children,
          containerType: ((_this$rawData$advance29 = this.rawData.advanced) === null || _this$rawData$advance29 === void 0 ? void 0 : (_this$rawData$advance30 = _this$rawData$advance29.component) === null || _this$rawData$advance30 === void 0 ? void 0 : _this$rawData$advance30.containerType) || undefined,
          packageName: this.rawData.packageName,
          isFormControl: (_this$rawData$advance31 = this.rawData.advanced) === null || _this$rawData$advance31 === void 0 ? void 0 : (_this$rawData$advance32 = _this$rawData$advance31.component) === null || _this$rawData$advance32 === void 0 ? void 0 : _this$rawData$advance32.isFormControl
        };
      }
    }
  }]);
  return ComponentSpec;
}();