"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

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

var _tslib = require("tslib");

var _consoleComponents = require("@alicloud/console-components");

var _classnames = _interopRequireDefault(require("classnames"));

var _react = _interopRequireWildcard(require("react"));

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

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

var mapper = {
  text: 'div',
  input: _consoleComponents.Input,
  select: _consoleComponents.Select,
  radioGroup: _consoleComponents.Radio.Group,
  checkboxGroup: _consoleComponents.Checkbox.Group,
  datePicker: _consoleComponents.DatePicker,
  numberPicker: _consoleComponents.NumberPicker,
  "switch": _consoleComponents.Switch,
  range: _consoleComponents.Range,
  upload: _consoleComponents.Upload,
  combobox: _consoleComponents.Select,
  textarea: _consoleComponents.Input.TextArea,
  cascaderSelect: _consoleComponents.CascaderSelect,
  treeSelect: _consoleComponents.TreeSelect
};
;
;

var IotForm =
/** @class */
function (_super) {
  (0, _tslib.__extends)(IotForm, _super);

  function IotForm(props) {
    var _this = _super.call(this, props) || this;

    _this.helpLabel = function (label, tips) {
      if (!label || label === "") {
        return null;
      } else {
        return /*#__PURE__*/_react["default"].createElement("span", null, label, tips ? /*#__PURE__*/_react["default"].createElement(_consoleComponents.Balloon, {
          autoFocus: false,
          type: "primary",
          align: "t",
          trigger: /*#__PURE__*/_react["default"].createElement(_consoleComponents.Icon, {
            type: 'help',
            className: "lp-form-tip-icon",
            size: "xs"
          }),
          closable: false
        }, tips) : null);
      }

      ;
    };

    _this.field = props.field || new _consoleComponents.Field(_this, props.fieldOptions);
    _this.state = {
      source: props.source || []
    };
    return _this;
  }

  IotForm.getDerivedStateFromProps = function (nextProps) {
    if (nextProps.source) {
      return {
        source: nextProps.source
      };
    }

    return null;
  };

  IotForm.prototype.componentDidMount = function () {
    if (this.props.orgData) {
      this.field.setValues(this.props.orgData);
    }
  };

  IotForm.prototype.render = function () {
    var _a = this.props,
        field = _a.field,
        fieldOptions = _a.fieldOptions,
        className = _a.className,
        handleSubmit = _a.handleSubmit,
        handleReset = _a.handleReset,
        handleSubmitError = _a.handleSubmitError,
        localButtons = _a.localButtons,
        labelAlign = _a.labelAlign,
        orgData = _a.orgData,
        isNest = _a.isNest,
        allStart = _a.allStart,
        nest = _a.nest,
        footer = _a.footer,
        noSplitLine = _a.noSplitLine,
        footerAligin = _a.footerAligin,
        rest = (0, _tslib.__rest)(_a, ["field", "fieldOptions", "className", "handleSubmit", "handleReset", "handleSubmitError", "localButtons", "labelAlign", "orgData", "isNest", "allStart", "nest", "footer", "noSplitLine", "footerAligin"]);
    var source = this.state.source;
    var content = this.pushFormItems(source, allStart);
    return /*#__PURE__*/_react["default"].createElement(_consoleComponents.Form, (0, _tslib.__assign)({
      labelAlign: labelAlign,
      className: (0, _classnames["default"])('lp-form', {
        "lp-form-nest": isNest !== undefined ? isNest : nest
      }, className),
      field: this.field
    }, rest), content, footer === false ? null : /*#__PURE__*/_react["default"].createElement("div", {
      className: (0, _classnames["default"])('footer', {
        "lp-form-footer-nest": isNest !== undefined ? isNest : nest
      }, {
        "lp-form-footer-left": footerAligin === "left"
      }, {
        "lp-footer-noLine": noSplitLine
      }, className)
    }, this.renderButtons(localButtons, handleSubmitError, handleSubmit, handleReset)));
  };

  IotForm.prototype.renderButtons = function (localButtons, handleSubmitError, handleSubmit, handleReset) {
    var buttons = [];

    if (localButtons) {
      return localButtons.map(function (item, index) {
        switch (item.type) {
          case 'submit':
            return /*#__PURE__*/_react["default"].createElement(_consoleComponents.Form.Submit, (0, _tslib.__assign)({
              key: 'lp_form_submit_Button_local' + index
            }, item.props, {
              validate: item.validate ? item.validate : true,
              type: "primary",
              onClick: function onClick(v, e) {
                e ? item.handleSubmitError && item.handleSubmitError(e) : item.handleIt && item.handleIt(v);
              }
            }), item.name);

          case 'reset':
            return /*#__PURE__*/_react["default"].createElement(_consoleComponents.Form.Reset, (0, _tslib.__assign)({}, item.props, {
              key: 'lp_form_submit_Button_local' + index,
              onClick: item.handleIt
            }), item.name);

          case 'normal':
            return /*#__PURE__*/_react["default"].createElement(_consoleComponents.Button, (0, _tslib.__assign)({
              key: 'lp_form_submit_Button_local' + index
            }, item.props, {
              onClick: item.handleIt
            }), item.name);

          default:
            return null;
        }
      });
    } else {
      buttons.push( /*#__PURE__*/_react["default"].createElement(_consoleComponents.Form.Submit, {
        key: "lp_form_submit_Button",
        validate: true,
        type: "primary",
        onClick: function onClick(v, e) {
          e ? handleSubmitError && handleSubmitError(e) : handleSubmit && handleSubmit(v);
        }
      }, "\u5B8C\u6210"));
      buttons.push( /*#__PURE__*/_react["default"].createElement(_consoleComponents.Form.Reset, {
        key: "lp_form_reset_Button",
        onClick: handleReset
      }, "\u91CD\u7F6E"));
    }

    return buttons;
  };

  IotForm.prototype.pushFormItems = function (source, allStart) {
    var _this = this; //如果每个选项都是必填，那么label上不出现*


    var requireCount = 0;
    var allRequire = false;

    if (!allStart) {
      for (var _i = 0, source_1 = source; _i < source_1.length; _i++) {
        var i = source_1[_i];

        if (i.required || i.type === 'switch') {
          requireCount++;
        }
      }

      if (requireCount == source.length) {
        allRequire = true;
      }
    }

    return source.map(function (item, index) {
      return /*#__PURE__*/_react["default"].createElement("div", {
        className: index === source.length - 1 ? "formGridRow lastOne" : "formGridRow",
        key: 'lp_form_row' + index
      }, Array.isArray(item) ? function () {
        return item.map(function (c, i) {
          return /*#__PURE__*/_react["default"].createElement("div", {
            className: "GridCol",
            key: 'lp_form_col' + i
          }, _this.renderFormItem(c, allRequire, true));
        });
      }() : function () {
        return _this.renderFormItem(item, allRequire, undefined);
      }());
    });
  };

  IotForm.prototype.renderFormItem = function (item, allRequire, fillOver) {
    var _this = this;

    var type = item.type,
        name = item.name,
        label = item.label,
        _a = item.props,
        props = _a === void 0 ? {} : _a,
        _b = item.options,
        options = _b === void 0 ? {} : _b,
        template = item.template,
        _c = item.formItemProps,
        formItemProps = _c === void 0 ? {} : _c,
        tips = item.tips,
        required = item.required; //如果是必填自动添加规则

    if (required) {
      if (options.rules) {
        if (Array.isArray(options.rules)) {
          options.rules.push({
            required: true,
            message: '抱歉,' + label + '不能为空值'
          });
        } else {
          var temprRules = [];
          temprRules.push(options.rules);
          temprRules.push({
            required: true,
            message: '抱歉,' + label + '不能为空值'
          });
          options.rules = temprRules;
        }
      } else {
        options['rules'] = {
          required: true,
          message: '抱歉,' + label + '不能为空值'
        };
      }
    }

    var fieldProps = this.field.init(name, options);
    var itemType = template ? 'custom' : type;
    var itemProps = (0, _tslib.__assign)((0, _tslib.__assign)({}, fieldProps), props);
    var component = {
      "class": function _class() {}
    }; //使checkbox，radio 等垂直排部

    if (itemProps.vertical) {
      if (itemProps["className"]) {
        itemProps["className"] = itemProps["className"] + " vertical";
      } else {
        itemProps["className"] = "vertical";
      }
    }

    delete itemProps.vertical;
    this.updateItemStyles(itemType, itemProps, fillOver);
    var componentClass = mapper[itemType];

    if (itemType === 'custom') {
      if (!(template.prototype instanceof _react["default"].Component)) {
        delete itemProps.ref;
      }

      itemProps.setValue = function (value) {
        return _this.field.setValue(name, value);
      };

      if (typeof template === 'function') {
        var RenderComponent = template;
        return /*#__PURE__*/_react["default"].createElement(_consoleComponents.Form.Item, (0, _tslib.__assign)({
          key: name,
          label: this.helpLabel(label, tips),
          required: allRequire ? false : required,
          help: this.renderHelp(name)
        }, formItemProps), /*#__PURE__*/_react["default"].createElement(RenderComponent, (0, _tslib.__assign)({}, itemProps)));
      } else {
        return /*#__PURE__*/_react["default"].createElement(_consoleComponents.Form.Item, (0, _tslib.__assign)({
          key: name,
          label: this.helpLabel(label, tips),
          required: allRequire ? false : required,
          help: this.renderHelp(name)
        }, formItemProps), template);
      }
    } else {
      if (componentClass) {
        component["class"] = componentClass;

        if (itemType === 'combobox') {
          itemProps['mode'] = 'tag';
          itemProps['hasArrow'] = false;
        }
      } else {
        console.log("\u4E0D\u652F\u6301" + itemType + "\u8868\u5355\u7C7B\u578B. Sorry, " + itemType + " is not supported by current version's form.");
      }

      var Component_ = component["class"];
      return /*#__PURE__*/_react["default"].createElement(_consoleComponents.Form.Item, (0, _tslib.__assign)({
        key: name,
        label: this.helpLabel(label, tips),
        required: allRequire ? false : required,
        help: this.renderHelp(name)
      }, formItemProps), /*#__PURE__*/_react["default"].createElement(Component_, (0, _tslib.__assign)({}, itemProps)));
    }
  };

  IotForm.prototype.updateItemStyles = function (type, props, fillOver) {
    var typesWithNoWidth = ['radioGroup', 'checkboxGroup', 'numberPicker', 'switch', 'upload'];

    if (fillOver) {
      props.style = (0, _tslib.__assign)({
        width: '100%'
      }, props.style);
    } else if (!typesWithNoWidth.includes(type)) {
      props.style = (0, _tslib.__assign)({
        width: 280
      }, props.style);
    }
  };

  IotForm.prototype.renderHelp = function (name) {
    var type = this.field.getState(name);
    var errors = this.field.getError(name) || [];

    if (type === 'validating') {
      return '校验中...';
    } else if (errors.length > 0) {
      return errors[0];
    }

    return '';
  };

  return IotForm;
}(_react.Component);

IotForm.defaultProps = {
  source: [],
  labelAlign: 'top',
  autoComplete: "off",
  allStart: true,
  fieldOptions: {
    forceUpdate: true,
    autoUnmount: true
  },
  footer: true
};
var _default = IotForm;
exports["default"] = _default;