"use strict";
const React = require("react");
const index$1 = require("../../../../../../scroll-into-view-if-needed@2.2.20/node_modules/scroll-into-view-if-needed/es/index.js");
const merge = require("../../../../../../lodash@4.17.21/node_modules/lodash/merge.js");
const classNames = require("../_util/classNames.js");
const useForm = require("./useForm.js");
const index = require("../ConfigProvider/index.js");
const context = require("./context.js");
const is = require("../_util/is.js");
const omit = require("../_util/omit.js");
const useMergeProps = require("../_util/hooks/useMergeProps.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");
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;
};
function getFormElementId(prefix, field) {
  var id = field.replace(/[\[\.]/g, "_").replace(/\]/g, "");
  return prefix ? prefix + "-" + id : "" + id;
}
var defaultProps = {
  layout: "horizontal",
  labelCol: {
    span: 5,
    offset: 0
  },
  labelAlign: "right",
  wrapperCol: {
    span: 19,
    offset: 0
  },
  requiredSymbol: true,
  wrapper: "form",
  validateTrigger: "onChange"
};
var Form = function(baseProps, ref) {
  var _a;
  var _b, _c;
  var ctx = React.useContext(index.ConfigContext);
  var formProviderCtx = React.useContext(context.FormProviderContext);
  var wrapperRef = React.useRef(null);
  var _d = __read(useForm.default(baseProps.form), 1), formInstance = _d[0];
  var isMount = React.useRef();
  var props = useMergeProps(baseProps, defaultProps, (_b = ctx.componentConfig) === null || _b === void 0 ? void 0 : _b.Form);
  var layout = props.layout, labelCol = props.labelCol, wrapperCol = props.wrapperCol, Wrapper = props.wrapper, id = props.id, requiredSymbol = props.requiredSymbol, labelAlign = props.labelAlign, disabled = props.disabled, colon = props.colon, className = props.className, validateTrigger = props.validateTrigger, formPrefixCls = props.prefixCls, validateMessages = props.validateMessages, rest = __rest(props, ["layout", "labelCol", "wrapperCol", "wrapper", "id", "requiredSymbol", "labelAlign", "disabled", "colon", "className", "validateTrigger", "prefixCls", "validateMessages"]);
  var prefixCls = formPrefixCls || ctx.getPrefixCls("form");
  var rtl = ctx.rtl;
  var size = "size" in props ? props.size : ctx.size;
  var innerMethods = formInstance.getInnerMethods(true);
  if (!isMount.current) {
    innerMethods.innerSetInitialValues(props.initialValues);
  }
  React.useEffect(function() {
    isMount.current = true;
  }, []);
  React.useEffect(function() {
    var unregister;
    if (formProviderCtx.register) {
      unregister = formProviderCtx.register(props.id, formInstance);
    }
    return unregister;
  }, [props.id, formInstance]);
  React.useImperativeHandle(ref, function() {
    return formInstance;
  });
  formInstance.scrollToField = function(field, options) {
    var node = wrapperRef.current;
    var id2 = props.id;
    if (!node) {
      return;
    }
    var fieldNode = node.querySelector("#" + getFormElementId(id2, field));
    if (!fieldNode) {
      fieldNode = node.querySelector("#" + getFormElementId(id2, field) + utils.ID_SUFFIX);
    }
    fieldNode && index$1(fieldNode, __assign({
      behavior: "smooth",
      block: "nearest",
      scrollMode: "if-needed"
    }, options));
  };
  innerMethods.innerSetCallbacks({
    onValuesChange: function(value, values) {
      props.onValuesChange && props.onValuesChange(value, values);
      formProviderCtx.onFormValuesChange && formProviderCtx.onFormValuesChange(props.id, value);
    },
    onChange: props.onChange,
    onValidateFail: function(errors) {
      if (props.scrollToFirstError) {
        var options = is.isObject(props.scrollToFirstError) ? props.scrollToFirstError : {};
        formInstance.scrollToField(Object.keys(errors)[0], options);
      }
    },
    onSubmitFailed: props.onSubmitFailed,
    onSubmit: function(values) {
      props.onSubmit && props.onSubmit(values);
      formProviderCtx.onFormSubmit && formProviderCtx.onFormSubmit(props.id, values);
    }
  });
  var contextProps = {
    requiredSymbol,
    labelAlign,
    disabled,
    colon,
    labelCol,
    wrapperCol,
    layout,
    store: formInstance,
    prefixCls,
    validateTrigger,
    validateMessages: merge({}, (_c = ctx.locale.Form) === null || _c === void 0 ? void 0 : _c.validateMessages, validateMessages),
    getFormElementId: function(field) {
      return getFormElementId(id, field);
    }
  };
  var FormContext = context.FormContext;
  return /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(index.default, {
    ...__assign({}, ctx, {
      size
    }),
    children: /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(FormContext.Provider, {
      value: contextProps,
      children: /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(Wrapper, {
        ...__assign({
          ref: wrapperRef
        }, omit(rest, ["form", "size", "initialValues", "onValuesChange", "onChange", "wrapperProps", "scrollToFirstError", "onSubmit", "onSubmitFailed"]), props.wrapperProps, {
          className: classNames(prefixCls, prefixCls + "-" + layout, prefixCls + "-size-" + size, (_a = {}, _a[prefixCls + "-rtl"] = rtl, _a), className),
          style: props.style,
          onSubmit: function(e) {
            e.preventDefault();
            e.stopPropagation();
            formInstance.submit();
          },
          id
        }),
        children: props.children
      })
    })
  });
};
var FormComponent = React.forwardRef(Form);
FormComponent.displayName = "Form";
const Form$1 = FormComponent;
module.exports = Form$1;
//# sourceMappingURL=form.js.map
