var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
  for (var prop in b || (b = {}))
    if (__hasOwnProp.call(b, prop))
      __defNormalProp(a, prop, b[prop]);
  if (__getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(b)) {
      if (__propIsEnum.call(b, prop))
        __defNormalProp(a, prop, b[prop]);
    }
  return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
var __objRest = (source, exclude) => {
  var target = {};
  for (var prop in source)
    if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
      target[prop] = source[prop];
  if (source != null && __getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(source)) {
      if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
        target[prop] = source[prop];
    }
  return target;
};
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);
}
import React, { useRef, useContext } from "react";
import classnames from "classnames";
import { Field as RcField } from "rc-field-form";
import Field from "../field";
import { toArray } from "../uploader/utils";
import { FIELD_KEY } from "../field/Field";
import { COMPONENT_TYPE_KEY } from "../utils/constant";
import { FormContext } from "./FormContext";
import ConfigProviderContext from "../config-provider/ConfigProviderContext";
function devWarning(component, message) {
  if (process.env.NODE_ENV !== "production") {
    console.warn(`[${component}] ${message}`);
  }
}
const MemoInput = /* @__PURE__ */ React.memo((_a) => {
  var _b = _a, {
    children
  } = _b, props = __objRest(_b, [
    "children"
  ]);
  return /* @__PURE__ */ React.cloneElement(children, props);
}, (prev, next) => prev.value === next.value && prev.update === next.update);
const FormItemLayout = (props) => {
  const _a = props, {
    className,
    meta,
    children,
    isFieldChildren,
    showValidateMessage,
    colon,
    layout,
    validateTrigger
  } = _a, fieldProps = __objRest(_a, [
    "className",
    "meta",
    "children",
    "isFieldChildren",
    "showValidateMessage",
    "colon",
    "layout",
    "validateTrigger"
  ]);
  const {
    prefixCls,
    createNamespace
  } = useContext(ConfigProviderContext);
  const [bem] = createNamespace("form", prefixCls);
  const context = useContext(FormContext);
  const itemLayout = layout != null ? layout : context.layout;
  const itemColon = colon != null ? colon : context.colon;
  const itemValidateTrigger = validateTrigger != null ? validateTrigger : context.validateTrigger;
  const itemShowValidateMessage = showValidateMessage != null ? showValidateMessage : context.showValidateMessage;
  const error = meta && meta.errors.length > 0;
  const errorMessage = itemShowValidateMessage && error ? meta.errors[0] : null;
  const attrs = __spreadProps(__spreadValues({}, fieldProps), {
    className: classnames(bem({
      vertical: itemLayout === "vertical" && !isFieldChildren
    }), className),
    colon: itemColon,
    error,
    errorMessage,
    validateTrigger: itemValidateTrigger
  });
  if (isFieldChildren)
    return /* @__PURE__ */ React.cloneElement(children, attrs);
  return /* @__PURE__ */ React.createElement(Field, attrs, children);
};
const FormItem = (props) => {
  var _b;
  const _a = props, {
    className,
    style,
    label,
    name,
    required,
    noStyle,
    tooltip,
    intro,
    customField,
    disabled,
    colon,
    rules,
    children,
    messageVariables,
    trigger,
    validateTrigger = trigger,
    onClick,
    shouldUpdate,
    dependencies,
    labelWidth,
    labelAlign,
    labelClass,
    showValidateMessage
  } = _a, fieldProps = __objRest(_a, [
    "className",
    "style",
    "label",
    "name",
    "required",
    "noStyle",
    "tooltip",
    "intro",
    "customField",
    "disabled",
    "colon",
    "rules",
    "children",
    "messageVariables",
    "trigger",
    "validateTrigger",
    "onClick",
    "shouldUpdate",
    "dependencies",
    "labelWidth",
    "labelAlign",
    "labelClass",
    "showValidateMessage"
  ]);
  const {
    validateTrigger: contextValidateTrigger
  } = useContext(FormContext);
  const mergedValidateTrigger = validateTrigger !== void 0 ? validateTrigger : contextValidateTrigger;
  const updateRef = useRef(0);
  updateRef.current += 1;
  const isFieldChildren = ((_b = children.type) == null ? void 0 : _b[COMPONENT_TYPE_KEY]) === FIELD_KEY || customField;
  function renderLayout(baseChildren, fieldId, meta, isRequired) {
    if (noStyle) {
      return baseChildren;
    }
    return /* @__PURE__ */ React.createElement(FormItemLayout, {
      isFieldChildren,
      className,
      style,
      label,
      tooltip,
      intro,
      required: isRequired,
      disabled,
      htmlFor: fieldId,
      meta,
      colon,
      labelWidth,
      labelAlign,
      labelClass,
      showValidateMessage,
      onClick
    }, baseChildren);
  }
  const isRenderProps = typeof children === "function";
  if (!name && !isRenderProps && !props.dependencies) {
    return renderLayout(children);
  }
  return /* @__PURE__ */ React.createElement(RcField, _extends({}, fieldProps, {
    name,
    shouldUpdate,
    dependencies,
    rules,
    trigger,
    validateTrigger: mergedValidateTrigger
  }), (control, meta, context) => {
    let childNode = null;
    const isRequired = required !== void 0 ? required : !!(rules && rules.some((rule) => {
      if (rule && typeof rule === "object" && rule.required) {
        return true;
      }
      return false;
    }));
    const fieldId = (toArray(name).length && meta ? meta.name : []).join("_");
    if (shouldUpdate && dependencies) {
      devWarning("FormItem", "`shouldUpdate` and `dependencies` shouldn't be used together.");
    }
    if (isRenderProps) {
      if ((shouldUpdate || dependencies) && !name) {
        childNode = children(context);
      } else {
        if (!(shouldUpdate || dependencies)) {
          devWarning("FormItem", "`children` of render props only work with `shouldUpdate` or `dependencies`.");
        }
        if (name) {
          devWarning("FormItem", "Do not use `name` with `children` of render props since it's not a field.");
        }
      }
    } else if (dependencies && !name) {
      devWarning("FormItem", "Must set `name` or use render props when `dependencies` is set.");
    } else if (/* @__PURE__ */ React.isValidElement(children)) {
      const childProps = __spreadValues(__spreadValues({}, children.props), control);
      if (!childProps.id) {
        childProps.id = fieldId;
      }
      if (disabled) {
        childProps.disabled = disabled;
      }
      const triggers = /* @__PURE__ */ new Set([...toArray(Object.keys(children.props).filter((item) => item.indexOf("on") === 0)), ...toArray(mergedValidateTrigger)]);
      triggers.forEach((eventName) => {
        childProps[eventName] = (...args) => {
          var _a2, _b2, _c;
          (_a2 = control[eventName]) == null ? void 0 : _a2.call(control, ...args);
          (_c = (_b2 = children.props)[eventName]) == null ? void 0 : _c.call(_b2, ...args);
        };
      });
      childNode = /* @__PURE__ */ React.createElement(MemoInput, {
        value: control[props.valuePropName || "value"],
        update: updateRef.current
      }, /* @__PURE__ */ React.cloneElement(children, childProps));
      if (isFieldChildren) {
        childProps.value = childProps.value || "";
        childNode = /* @__PURE__ */ React.cloneElement(children, childProps);
      }
    } else {
      if (name) {
        devWarning("FormItem", "`name` is only used for validate React element. If you are using FormItem as layout display, please remove `name` instead.");
      }
      childNode = children;
    }
    return renderLayout(childNode, fieldId, meta, isRequired);
  });
};
var stdin_default = FormItem;
export {
  stdin_default as default
};
