"use strict";

exports.__esModule = true;
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _form = _interopRequireWildcard(require("../form"));
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && 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 _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
var FieldElement = /*#__PURE__*/function (_Component) {
  _inheritsLoose(FieldElement, _Component);
  function FieldElement(props) {
    var _this = _Component.call(this, props) || this;
    _this.field = undefined;
    _this.renderPreview = _this.renderPreview.bind(_assertThisInitialized(_this));
    return _this;
  }
  var _proto = FieldElement.prototype;
  _proto.getValueName = function getValueName() {
    return 'value';
  };
  _proto.getValue = function getValue() {
    var _a;
    var name = this.getValueName();
    if (name in this.props) {
      return this.props[name];
    }
    return (_a = this.field) === null || _a === void 0 ? void 0 : _a.getValue(this.props.name);
  };
  _proto.setValue = function setValue(value) {
    var _a;
    return (_a = this.field) === null || _a === void 0 ? void 0 : _a.setValue(this.props.name, value);
  };
  _proto.renderPreview = function renderPreview(value) {
    return value;
  };
  _proto.renderControl = function renderControl() {
    return '请重载 renderControl';
  };
  _proto.getProps = function getProps() {
    return this.props;
  };
  _proto.componentWillUnmount = function componentWillUnmount() {
    this.field = undefined;
  };
  _proto.render = function render() {
    var _this2 = this;
    return /*#__PURE__*/(0, _react.createElement)(_form.Context.Consumer, null, function (ctx) {
      if (ctx.field && _this2.field !== ctx.field) {
        _this2.field = ctx.field;
        _this2.forceUpdate();
      }
      var item = /*#__PURE__*/(0, _react.createElement)(_form.Item, _extends({
        valuePropName: _this2.getValueName(),
        renderPreview: _this2.renderPreview
      }, _this2.getProps()), _this2.renderControl());
      if (ctx.form) {
        return item;
      } else {
        return /*#__PURE__*/(0, _react.createElement)(_form.default, {
          saveField: function saveField(field) {
            _this2.field = field;
            _this2.forceUpdate();
          }
        }, item);
      }
    });
  };
  return FieldElement;
}(_react.Component);
exports.default = FieldElement;
FieldElement.displayName = 'Field';