"use strict";
const React = require("react");
const isEqualWith = require("../../../../../../lodash@4.17.21/node_modules/lodash/isEqualWith.js");
const has = require("../../../../../../lodash@4.17.21/node_modules/lodash/has.js");
const set = require("../../../../../../lodash@4.17.21/node_modules/lodash/set.js");
const get = require("../../../../../../lodash@4.17.21/node_modules/lodash/get.js");
const setWith = require("../../../../../../lodash@4.17.21/node_modules/lodash/setWith.js");
const context = require("./context.js");
const is = require("../_util/is.js");
const warning = require("../_util/warning.js");
const index = require("../../icon/react-icon/IconExclamationCircleFill/index.js");
const index$2 = require("../../icon/react-icon/IconCloseCircleFill/index.js");
const index$1 = require("../../icon/react-icon/IconCheckCircleFill/index.js");
const index$3 = require("../../icon/react-icon/IconLoading/index.js");
const classNames = require("../_util/classNames.js");
const utils = require("./utils.js");
const emotionReactJsxRuntime_browser_esm = require("../../../../../../@emotion_react@11.10.4_3toe27fv7etiytxb5kxc7fxaw4/node_modules/@emotion/react/jsx-runtime/dist/emotion-react-jsx-runtime.browser.esm.js");
const _interopDefaultLegacy = (e) => e && typeof e === "object" && "default" in e ? e : { default: e };
const React__default = /* @__PURE__ */ _interopDefaultLegacy(React);
var __extends = globalThis && globalThis.__extends || function() {
  var extendStatics = function(d, b) {
    extendStatics = Object.setPrototypeOf || {
      __proto__: []
    } instanceof Array && function(d2, b2) {
      d2.__proto__ = b2;
    } || function(d2, b2) {
      for (var p in b2)
        if (Object.prototype.hasOwnProperty.call(b2, p))
          d2[p] = b2[p];
    };
    return extendStatics(d, b);
  };
  return function(d, b) {
    if (typeof b !== "function" && b !== null)
      throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
    extendStatics(d, b);
    function __() {
      this.constructor = d;
    }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  };
}();
var __assign = globalThis && globalThis.__assign || function() {
  __assign = Object.assign || function(t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
      s = arguments[i];
      for (var p in s)
        if (Object.prototype.hasOwnProperty.call(s, p))
          t[p] = s[p];
    }
    return t;
  };
  return __assign.apply(this, arguments);
};
var __read = globalThis && globalThis.__read || function(o, n) {
  var m = typeof Symbol === "function" && o[Symbol.iterator];
  if (!m)
    return o;
  var i = m.call(o), r, ar = [], e;
  try {
    while ((n === void 0 || n-- > 0) && !(r = i.next()).done)
      ar.push(r.value);
  } catch (error) {
    e = {
      error
    };
  } finally {
    try {
      if (r && !r.done && (m = i["return"]))
        m.call(i);
    } finally {
      if (e)
        throw e.error;
    }
  }
  return ar;
};
var __spreadArray = globalThis && globalThis.__spreadArray || function(to, from, pack) {
  if (pack || arguments.length === 2)
    for (var i = 0, l = from.length, ar; i < l; i++) {
      if (ar || !(i in from)) {
        if (!ar)
          ar = Array.prototype.slice.call(from, 0, i);
        ar[i] = from[i];
      }
    }
  return to.concat(ar || Array.prototype.slice.call(from));
};
function isFieldMath(field, fields) {
  var fieldObj = setWith({}, field, void 0, Object);
  return fields.some(function(item) {
    return has(fieldObj, item);
  });
}
var Control = function(_super) {
  __extends(Control2, _super);
  function Control2(props, context2) {
    var _this = _super.call(this, props) || this;
    _this.errors = null;
    _this.warnings = null;
    _this.isDestroyed = false;
    _this.childrenElement = null;
    _this.getErrors = function() {
      return _this.errors;
    };
    _this.isTouched = function() {
      return _this.touched;
    };
    _this.hasFieldProps = function() {
      return !!_this.props.field;
    };
    _this.clearFormItemError = function(field) {
      if (field === void 0) {
        field = _this.props.field;
      }
      var updateFormItem = _this.context.updateFormItem;
      updateFormItem && updateFormItem(field, {
        errors: null,
        warnings: null
      });
    };
    _this.updateFormItem = function() {
      if (_this.isDestroyed)
        return;
      _this.forceUpdate();
      var updateFormItem = _this.context.updateFormItem;
      updateFormItem && updateFormItem(_this.props.field, {
        errors: _this.errors,
        warnings: _this.warnings
      });
    };
    _this.onStoreChange = function(type, info) {
      var fields = is.isArray(info.field) ? info.field : [info.field];
      var _a = _this.props, field = _a.field, shouldUpdate = _a.shouldUpdate, dependencies = _a.dependencies;
      var shouldUpdateItem = function(extra) {
        if (dependencies && shouldUpdate) {
          warning(true, "`shouldUpdate` of the `Form.Item` will be ignored.");
        }
        if (dependencies) {
          if (is.isArray(dependencies) || dependencies.some(function(depField) {
            return isFieldMath(depField, fields);
          })) {
            if (_this.isTouched()) {
              _this.validateField();
            }
          }
        } else if (shouldUpdate) {
          var shouldRender = false;
          if (is.isFunction(shouldUpdate)) {
            shouldRender = shouldUpdate(info.prev, info.current, __assign({
              field: info.field
            }, extra));
          } else {
            shouldRender = !isEqualWith(info.prev, info.current);
          }
          if (shouldRender) {
            _this.updateFormItem();
          }
        }
      };
      switch (type) {
        case "reset":
          _this.touched = false;
          _this.errors = null;
          _this.warnings = null;
          _this.updateFormItem();
          break;
        case "innerSetValue":
          if (isFieldMath(field, fields)) {
            _this.touched = true;
            _this.updateFormItem();
            return;
          }
          shouldUpdateItem({
            isInner: true,
            isFormList: info.isFormList
          });
          break;
        case "setFieldValue":
          if (isFieldMath(field, fields)) {
            _this.touched = true;
            if (info.data && "touched" in info.data) {
              _this.touched = info.data.touched;
            }
            if (info.data && "warnings" in info.data) {
              _this.warnings = is.isNullOrUndefined(info.data.warnings) ? [] : [].concat(info.data.warnings);
            }
            if (info.data && "errors" in info.data) {
              _this.errors = info.data.errors;
            } else if (!isEqualWith(get(info.prev, field), get(info.current, field))) {
              _this.errors = null;
            }
            _this.updateFormItem();
            return;
          }
          shouldUpdateItem();
          break;
      }
    };
    _this.innerSetFieldValue = function(field, value) {
      if (!field)
        return;
      var store = _this.context.store;
      var methods = store.getInnerMethods(true);
      methods.innerSetFieldValue(field, value);
      var changedValue = {};
      set(changedValue, field, value);
      _this.props.onValuesChange && _this.props.onValuesChange(changedValue, __assign({}, store.getFieldsValue()));
    };
    _this.validateField = function(triggerType) {
      var _a = _this.context, store = _a.store, ctxValidateTrigger = _a.validateTrigger, validateMessages = _a.validateMessages;
      var _b = _this.props, field = _b.field, rules = _b.rules, validateTrigger = _b.validateTrigger;
      var value = store.getFieldValue(field);
      var _rules = !triggerType ? rules : (rules || []).filter(function(rule) {
        var triggers = [].concat(rule.validateTrigger || validateTrigger || ctxValidateTrigger);
        return triggers.indexOf(triggerType) > -1;
      });
      if (_rules && _rules.length && field) {
        return utils.schemaValidate(field, value, _rules, validateMessages).then(function(_a2) {
          var error = _a2.error, warning2 = _a2.warning;
          _this.errors = error ? error[field] : null;
          _this.warnings = warning2 || null;
          _this.updateFormItem();
          return Promise.resolve({
            error,
            value,
            field
          });
        });
      }
      if (_this.errors) {
        _this.errors = null;
        _this.warnings = null;
        _this.updateFormItem();
      }
      return Promise.resolve({
        error: null,
        value,
        field
      });
    };
    _this.handleTrigger = function(_value) {
      var _a;
      var args = [];
      for (var _i = 1; _i < arguments.length; _i++) {
        args[_i - 1] = arguments[_i];
      }
      var children = _this.childrenElement || _this.props.children;
      var store = _this.context.store;
      var _b = _this.props, field = _b.field, trigger = _b.trigger, normalize = _b.normalize, getValueFromEvent = _b.getValueFromEvent;
      var value = is.isFunction(getValueFromEvent) ? getValueFromEvent.apply(void 0, __spreadArray([_value], __read(args), false)) : _value;
      var normalizeValue = value;
      if (utils.isSyntheticEvent(value)) {
        warning(true, "changed value missed, please check whether extra elements is outta input/select controled by Form.Item");
        value.stopPropagation();
        return;
      }
      if (typeof normalize === "function") {
        normalizeValue = normalize(value, store.getFieldValue(field), __assign({}, store.getFieldsValue()));
      }
      _this.touched = true;
      _this.innerSetFieldValue(field, normalizeValue);
      _this.validateField(trigger);
      if (React.isValidElement(children) && children.props && children.props[trigger]) {
        (_a = children.props)[trigger].apply(_a, __spreadArray([normalizeValue], __read(args), false));
      }
    };
    _this.getChild = function() {
      var children = _this.props.children;
      var store = _this.context.store;
      var child = children;
      if (is.isFunction(children)) {
        child = children(store.getFields(), __assign({}, store));
      }
      _this.childrenElement = child;
      return child;
    };
    if ("initialValue" in props && _this.hasFieldProps()) {
      var innerMethods = context2.store.getInnerMethods(true);
      innerMethods.innerSetInitialValue(props.field, props.initialValue);
    }
    return _this;
  }
  Control2.prototype.componentDidMount = function() {
    var store = this.context.store;
    if (store) {
      var innerMethods = store.getInnerMethods(true);
      this.removeRegisterField = innerMethods.registerField(this);
    }
    this.isDestroyed = false;
  };
  Control2.prototype.componentDidUpdate = function(prevProps) {
    if (prevProps.field !== this.props.field && this.props._key && prevProps._key === this.props._key) {
      this.updateFormItem();
      this.clearFormItemError(prevProps.field);
    }
  };
  Control2.prototype.componentWillUnmount = function() {
    this.removeRegisterField && this.removeRegisterField();
    this.removeRegisterField = null;
    this.clearFormItemError();
    this.isDestroyed = true;
  };
  Control2.prototype.getValidateTrigger = function() {
    var _validateTrigger = this.props.validateTrigger || this.context.validateTrigger || "onChange";
    var rules = this.props.rules || [];
    var result = [];
    rules.map(function(item) {
      result = result.concat(item.validateTrigger || _validateTrigger);
    });
    return Array.from(new Set(result));
  };
  Control2.prototype.renderControl = function(children, id) {
    var _a;
    var _this = this;
    var _b;
    var _c = this.props, field = _c.field, _d = _c.trigger, trigger = _d === void 0 ? "onChange" : _d, _e = _c.triggerPropName, triggerPropName = _e === void 0 ? "value" : _e, validateStatus = _c.validateStatus, formatter = _c.formatter;
    var _f = this.context, store = _f.store, ctxDisabled = _f.disabled;
    var disabled = "disabled" in this.props ? this.props.disabled : ctxDisabled;
    var child = React__default.default.Children.only(children);
    var childProps = {
      id: classNames(((_b = child.props) === null || _b === void 0 ? void 0 : _b.id) || (_a = {}, _a["" + id + utils.ID_SUFFIX] = id, _a))
    };
    this.getValidateTrigger().forEach(function(vt) {
      childProps[vt] = function(e) {
        _this.validateField(vt);
        child.props[vt] && child.props[vt](e);
      };
    });
    childProps[trigger] = this.handleTrigger;
    if (disabled !== void 0) {
      childProps.disabled = disabled;
    }
    var _value = store.getFieldValue(field);
    if (is.isFunction(formatter)) {
      _value = formatter(_value);
    }
    childProps[triggerPropName] = _value;
    if (!validateStatus && this.errors) {
      childProps.error = true;
    }
    return React__default.default.cloneElement(child, childProps);
  };
  Control2.prototype.render = function() {
    var _a = this.props, noStyle = _a.noStyle, field = _a.field, isFormList = _a.isFormList, hasFeedback = _a.hasFeedback;
    var validateStatus = this.props.validateStatus || (this.errors ? "error" : "");
    var _b = this.context, prefixCls = _b.prefixCls, getFormElementId = _b.getFormElementId;
    var child = this.getChild();
    var id = this.hasFieldProps() ? getFormElementId(field) : void 0;
    if (this.hasFieldProps() && !isFormList && React__default.default.Children.count(child) === 1) {
      child = this.renderControl(child, id);
    }
    if (noStyle) {
      return child;
    }
    return /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx("div", {
      className: prefixCls + "-item-control-wrapper",
      children: /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx("div", {
        className: prefixCls + "-item-control",
        id,
        children: /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsxs("div", {
          className: prefixCls + "-item-control-children",
          children: [child, validateStatus && hasFeedback && /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsxs("div", {
            className: prefixCls + "-item-feedback",
            children: [validateStatus === "warning" && /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(index, {}), validateStatus === "success" && /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(index$1, {}), validateStatus === "error" && /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(index$2, {}), validateStatus === "validating" && /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(index$3, {})]
          })]
        })
      })
    });
  };
  Control2.defaultProps = {
    trigger: "onChange",
    triggerPropName: "value"
  };
  Control2.isFormControl = true;
  Control2.contextType = context.FormItemContext;
  return Control2;
}(React.Component);
const FormControl = Control;
module.exports = FormControl;
//# sourceMappingURL=control.js.map
