import _Form from "antd/es/form";
import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/objectWithoutPropertiesLoose";
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
var _excluded = ["values", "onValuesChange", "name"],
  _excluded2 = ["children"];
import React, { Component, createRef } from 'react';
var Form = /*#__PURE__*/function (_Component) {
  _inheritsLoose(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__*/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 = _objectWithoutPropertiesLoose(_this$props, _excluded);
    return /*#__PURE__*/React.createElement(_Form, _extends({
      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;
}(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 = _objectWithoutPropertiesLoose(props, _excluded2);
  var node = children;
  if (Array.isArray(children) && children.length === 1) {
    // 如果 children.length > 1， 说明 Form.Item 只充当布局的作用
    node = children[0];
  }
  return /*#__PURE__*/React.createElement(_Form.Item, _extends({}, other, {
    name: namePath,
    rules: rules
  }), node);
};
Form.displayName = 'Form';
export default Form;