"use strict";
const React = require("react");
const classNames = require("../_util/classNames.js");
const is = require("../_util/is.js");
const index$1 = require("../Grid/index.js");
const _interface = require("./interface.js");
const control = require("./control.js");
const context = require("./context.js");
const index = require("../ConfigProvider/index.js");
const omit = require("../_util/omit.js");
const formLabel = require("./form-label.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 CSSTransition = require("../../../../../../react-transition-group@4.4.5_biqbaboplfbrettd7655fr4n2y/node_modules/react-transition-group/esm/CSSTransition.js");
const _interopDefaultLegacy = (e) => e && typeof e === "object" && "default" in e ? e : { default: e };
const React__default = /* @__PURE__ */ _interopDefaultLegacy(React);
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 __rest = globalThis && globalThis.__rest || function(s, e) {
  var t = {};
  for (var p in s)
    if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
      t[p] = s[p];
  if (s != null && typeof Object.getOwnPropertySymbols === "function")
    for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
      if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
        t[p[i]] = s[p[i]];
    }
  return t;
};
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));
};
var Row = index$1.Row;
var Col = index$1.Col;
var FormItemTip = function(_a) {
  var _b;
  var prefixCls = _a.prefixCls, help = _a.help, propsErrors = _a.errors, warnings = _a.warnings;
  var errorTip = propsErrors.map(function(item, i) {
    if (item) {
      return /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx("div", {
        role: "alert",
        children: item.message
      }, i);
    }
  });
  var warningTip = [];
  warnings.map(function(item, i) {
    warningTip.push(/* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx("div", {
      role: "alert",
      className: prefixCls + "-message-help-warning",
      children: item
    }, i));
  });
  var isHelpTip = !is.isUndefined(help) || !!warningTip.length;
  var visible = isHelpTip || !!errorTip.length;
  return visible && /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(CSSTransition, {
    in: visible,
    appear: true,
    classNames: "formblink",
    timeout: 300,
    unmountOnExit: true,
    children: /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx("div", {
      className: classNames(prefixCls + "-message", (_b = {}, _b[prefixCls + "-message-help"] = isHelpTip, _b)),
      children: !is.isUndefined(help) ? help : /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsxs(emotionReactJsxRuntime_browser_esm.Fragment, {
        children: [errorTip.length > 0 && errorTip, warningTip.length > 0 && warningTip]
      })
    })
  });
};
var Item = function(props, ref) {
  var _a, _b, _c, _d;
  var _e, _f;
  var _g = React.useContext(index.ConfigContext), getPrefixCls = _g.getPrefixCls, prefix = _g.prefixCls;
  var topFormContext = React.useContext(context.FormItemContext);
  var formListContext = React.useContext(context.FormListContext);
  var _h = __read(React.useState(null), 2), errors = _h[0], setErrors = _h[1];
  var _j = __read(React.useState(null), 2), warnings = _j[0], setWarnings = _j[1];
  var formContext = React.useContext(context.FormContext);
  var prefixCls = formContext.prefixCls || getPrefixCls("form");
  var formLayout = props.layout || formContext.layout;
  var labelAlign = props.labelAlign || formContext.labelAlign;
  var isDestroyed = React.useRef(false);
  var updateInnerFormItem = function(field, params) {
    if (params === void 0) {
      params = {};
    }
    if (isDestroyed.current) {
      return;
    }
    var _a2 = params || {}, errors2 = _a2.errors, warnings2 = _a2.warnings;
    setErrors(function(innerErrors) {
      var newErrors = __assign({}, innerErrors || {});
      if (errors2) {
        newErrors[field] = errors2;
      } else {
        delete newErrors[field];
      }
      return newErrors;
    });
    setWarnings(function(current) {
      var newVal = __assign({}, current || {});
      if (warnings2 && warnings2.length) {
        newVal[field] = warnings2;
      } else {
        delete newVal[field];
      }
      return newVal;
    });
  };
  var updateFormItem = is.isObject(props.noStyle) && props.noStyle.showErrorTip && topFormContext.updateFormItem ? topFormContext.updateFormItem : updateInnerFormItem;
  React.useEffect(function() {
    isDestroyed.current = false;
    return function() {
      isDestroyed.current = true;
      setErrors(null);
      setWarnings(null);
    };
  }, []);
  var contextProps = __assign(__assign({}, formContext), {
    validateMessages: formContext.validateMessages && utils.formatValidateMsg(formContext.validateMessages, {
      label: props.label
    }),
    prefixCls,
    updateFormItem,
    disabled: "disabled" in props ? props.disabled : formContext.disabled
  });
  var label = props.label, extra = props.extra, className = props.className, style = props.style, validateStatus = props.validateStatus, hidden = props.hidden, rest = __rest(props, ["label", "extra", "className", "style", "validateStatus", "hidden"]);
  var labelClassNames = classNames(prefixCls + "-label-item", (_a = {}, _a[prefixCls + "-label-item-left"] = labelAlign === "left", _a));
  var errorInfo = errors ? Object.values(errors) : [];
  var warningInfo = warnings ? Object.values(warnings).reduce(function(total, next) {
    return total.concat(next);
  }, []) : [];
  var itemStatus = React.useMemo(function() {
    if (validateStatus) {
      return validateStatus;
    }
    if (errorInfo.length) {
      return _interface.VALIDATE_STATUS.error;
    }
    if (warningInfo.length) {
      return _interface.VALIDATE_STATUS.warning;
    }
    return void 0;
  }, [errors, warnings, validateStatus]);
  var hasHelp = React.useMemo(function() {
    return !is.isUndefined(props.help) || warningInfo.length > 0;
  }, [props.help, warnings]);
  var classNames$1 = classNames(prefixCls + "-item", (_b = {}, _b[prefixCls + "-item-error"] = hasHelp || !validateStatus && itemStatus === _interface.VALIDATE_STATUS.error, _b[prefixCls + "-item-status-" + itemStatus] = itemStatus, _b[prefixCls + "-item-has-help"] = hasHelp, _b[prefixCls + "-item-hidden"] = hidden, _b[prefixCls + "-item-has-feedback"] = itemStatus && props.hasFeedback, _b), prefixCls + "-layout-" + formLayout, className);
  var cloneElementWithDisabled = function() {
    var _a2, _b2;
    var field = props.field, children = props.children;
    var disabled = "disabled" in props ? props.disabled : formContext.disabled;
    if (is.isFunction(children)) {
      return /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(control, {
        ...__assign({
          disabled
        }, props, field ? {
          key: field,
          _key: field
        } : {}),
        children: function() {
          var rest2 = [];
          for (var _i = 0; _i < arguments.length; _i++) {
            rest2[_i] = arguments[_i];
          }
          return children.apply(void 0, __spreadArray([], __read(rest2), false));
        }
      });
    }
    if (is.isArray(children)) {
      var childrenDom = React__default.default.Children.map(children, function(child, i) {
        var key2 = is.isObject(child) && child.key || i;
        var childProps2 = !is.isUndefined(disabled) ? {
          key: key2,
          disabled
        } : {
          key: key2
        };
        return is.isObject(child) ? React.cloneElement(child, childProps2) : child;
      });
      return /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(control, {
        ...__assign({}, props, {
          field: void 0
        }),
        children: childrenDom
      });
    }
    if (React__default.default.Children.count(children) === 1) {
      if (field) {
        var key = ((_a2 = formListContext === null || formListContext === void 0 ? void 0 : formListContext.getItemKey) === null || _a2 === void 0 ? void 0 : _a2.call(formListContext, field)) || field;
        return /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(control, {
          ...__assign({
            disabled
          }, props, {
            key,
            _key: key
          }),
          children
        });
      }
      if (is.isObject(children)) {
        if ((_b2 = children.type) === null || _b2 === void 0 ? void 0 : _b2.isFormControl) {
          return children;
        }
        var childProps = is.isUndefined(disabled) ? {} : {
          disabled
        };
        return /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(control, {
          ...__assign({}, props, {
            field: void 0
          }),
          children: React.cloneElement(children, childProps)
        });
      }
    }
    return children;
  };
  var FormItemContext = context.FormItemContext;
  var newFormContext = __assign({}, formContext);
  if (!props.noStyle) {
    newFormContext.wrapperCol = void 0;
    newFormContext.labelCol = void 0;
  }
  return /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(context.FormContext.Provider, {
    value: newFormContext,
    children: /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(FormItemContext.Provider, {
      value: contextProps,
      children: props.noStyle ? cloneElementWithDisabled() : /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsxs(Row, {
        ...__assign({
          ref
        }, omit(rest, ["children", "prefixCls", "store", "initialValue", "field", "labelCol", "wrapperCol", "colon", "disabled", "rules", "trigger", "triggerPropName", "validateTrigger", "noStyle", "required", "hasFeedback", "help", "normalize", "formatter", "getValueFromEvent", "shouldUpdate", "field", "isInner", "labelAlign", "layout", "requiredSymbol", "isFormList"]), {
          className: classNames$1,
          div: formLayout !== "horizontal",
          style
        }),
        children: [label ? /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(Col, {
          ...__assign({}, props.labelCol || formContext.labelCol, {
            className: classNames(labelClassNames, (_e = props.labelCol) === null || _e === void 0 ? void 0 : _e.className, (_f = formContext.labelCol) === null || _f === void 0 ? void 0 : _f.className, (_c = {}, _c[prefixCls + "-label-item-flex"] = !props.labelCol && !formContext.labelCol, _c))
          }),
          children: /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(formLabel, {
            htmlFor: props.field && formContext.getFormElementId(props.field),
            label,
            prefix,
            requiredSymbol: "requiredSymbol" in props ? props.requiredSymbol : formContext.requiredSymbol,
            required: props.required,
            rules: props.rules,
            showColon: "colon" in props ? props.colon : formContext.colon
          })
        }) : null, /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsxs(Col, {
          ...__assign({
            className: classNames(prefixCls + "-item-wrapper", (_d = {}, _d[prefixCls + "-item-wrapper-flex"] = !props.wrapperCol && !formContext.wrapperCol, _d))
          }, props.wrapperCol || formContext.wrapperCol),
          children: [cloneElementWithDisabled(), /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(FormItemTip, {
            prefixCls,
            help: props.help,
            errors: errorInfo,
            warnings: warningInfo
          }), extra && /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx("div", {
            className: prefixCls + "-extra",
            children: extra
          })]
        })]
      })
    })
  });
};
var ItemComponent = React.forwardRef(Item);
ItemComponent.defaultProps = {
  trigger: "onChange",
  triggerPropName: "value"
};
ItemComponent.displayName = "FormItem";
const FormItem = ItemComponent;
module.exports = FormItem;
//# sourceMappingURL=form-item.js.map
