"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = void 0;
var _form = _interopRequireDefault(require("antd/lib/form"));
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));
var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));
var _react = _interopRequireWildcard(require("react"));
var _excluded = ["values", "onValuesChange", "name"],
  _excluded2 = ["children"];
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
var Form = /*#__PURE__*/function (_Component) {
  (0, _inheritsLoose2["default"])(Form, _Component);
  function Form() {
    var _this;
    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }
    _this = _Component.call.apply(_Component, [this].concat(args)) || this;
    _this.formRef = /*#__PURE__*/(0, _react.createRef)();
    return _this;
  }
  var _proto = Form.prototype;
  _proto.componentDidUpdate = function componentDidUpdate(prevProps) {
    var prevValues = prevProps.values;
    var currentValues = this.props.values;
    if (prevValues !== currentValues) {
      if (currentValues && Object.keys(currentValues).length > 0) {
        this.formRef.current.setFieldsValue(currentValues);
      } else {
        this.formRef.current.resetFields();
      }
    }
  };
  _proto.componentDidMount = function componentDidMount() {
    var _this2 = this;
    var values = this.props.values;
    Object.defineProperties(this, Object.keys(this.formRef.current).reduce(function (out, key) {
      var property = _this2.formRef.current[key];
      var getter = function getter() {
        return property;
      };
      if (typeof property === 'function') {
        getter = function getter() {
          return property.bind(_this2.formRef.current);
        };
      }
      out[key] = {
        get: getter
      };
      return out;
      // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
    }, {}));

    // 绑定表达式的时候，初次不生效，需要第一次调用一下 setFieldsValue
    if (values) {
      this.formRef.current.setFieldsValue(values);
    }
  };
  _proto.render = function render() {
    var _this3 = this;
    var _this$props = this.props,
      values = _this$props.values,
      _onValuesChange = _this$props.onValuesChange,
      name = _this$props.name,
      rest = (0, _objectWithoutPropertiesLoose2["default"])(_this$props, _excluded);
    return /*#__PURE__*/_react["default"].createElement(_form["default"], (0, _extends2["default"])({
      ref: this.formRef,
      onValuesChange: function onValuesChange(value) {
        _onValuesChange(value);
        if (name) {
          var _window$top;
          /* 向主应用发送消息 */
          (_window$top = window.top) === null || _window$top === void 0 ? void 0 : _window$top.postMessage(_this3.formRef.current.getFieldsValue(), '*');
        }
      }
    }, rest));
  };
  return Form;
}(_react.Component);
Form.Item = function (props) {
  var name = props.name,
    requiredobj = props.requiredobj,
    typeobj = props.typeobj,
    patternobj = props.patternobj,
    lenobj = props.lenobj,
    _validator = props.validator;
  var rules = [];
  if (requiredobj && requiredobj.required) {
    rules.push(requiredobj);
  }
  if (typeobj && typeobj.type) {
    rules.push(typeobj);
  }
  if (patternobj && patternobj.pattern) {
    rules.push(patternobj);
  }
  if (lenobj && (lenobj.max || lenobj.min)) {
    rules.push(lenobj);
  }
  if (_validator && typeof _validator === 'function') {
    rules.push({
      validator: function validator(_, value) {
        return _validator(value);
      }
    });
  }
  var namePath = typeof name === 'string' && name.indexOf('.') > 0 ? name.split('.') : name;

  // https://ant.design/components/form-cn/#components-form-demo-complex-form-control
  // <Form.Item name="field" /> 只会对它的直接子元素绑定表单功能
  var children = props.children,
    other = (0, _objectWithoutPropertiesLoose2["default"])(props, _excluded2);
  var node = children;
  if (Array.isArray(children) && children.length === 1) {
    // 如果 children.length > 1， 说明 Form.Item 只充当布局的作用
    node = children[0];
  }
  return /*#__PURE__*/_react["default"].createElement(_form["default"].Item, (0, _extends2["default"])({}, other, {
    name: namePath,
    rules: rules
  }), node);
};
Form.displayName = 'Form';
var _default = exports["default"] = Form;