import { jsx as d } from "@emotion/react/jsx-runtime";
import { forwardRef as n, useEffect as u, useImperativeHandle as p } from "react";
import F from "clsx";
import { useForm as y, FormProvider as P } from "react-hook-form";
import { yupResolver as b } from "@hookform/resolvers/yup";
import { FormProviderRoot as R } from "./styles.js";
import { RHFormProviderUtilityClasses as S } from "./classes/index.js";
const C = S();
function N({
  validationSchema: m,
  values: e,
  statusLoad: s,
  mode: t
}) {
  const r = y({
    resolver: b(m),
    defaultValues: e,
    ...t && { mode: t }
  });
  return u(() => {
    if (s === "reload_values_provider") {
      Object.keys(e).forEach((o) => {
        const a = o, l = e[o];
        r.setValue(a, l, {
          shouldValidate: !1,
          shouldDirty: !1,
          shouldTouch: !1
        });
      }), r.clearErrors();
      return;
    }
  }, [r, s, e]), u(() => {
    s === "ready" && r.setValue("statusLoad", "ready", {
      shouldValidate: !1,
      shouldDirty: !1,
      shouldTouch: !1
    });
  }, [s]), r;
}
const V = (m, e) => {
  const { children: s, onSubmit: t, onSubmitError: r, className: i, formMethods: o } = m;
  return p(
    e,
    () => ({
      formMethods: o,
      onSubmit: t
    }),
    [o, t]
  ), /* @__PURE__ */ d(P, { ...o, children: /* @__PURE__ */ d(
    R,
    {
      className: F(C.root, i),
      onSubmit: o.handleSubmit(
        t,
        r
      ),
      children: s
    }
  ) });
}, c = n(V);
c.displayName = "FormProviderCustom";
const E = c, f = n(
  (m, e) => {
    const { children: s, onSubmit: t, onSubmitError: r, values: i, validationSchema: o, statusLoad: a = "ready", className: l, mode: h } = m, v = N({ validationSchema: o, statusLoad: a, values: i, mode: h });
    return /* @__PURE__ */ d(
      E,
      {
        ref: e,
        className: l,
        values: i,
        validationSchema: o,
        statusLoad: a,
        onSubmit: t,
        onSubmitError: r,
        formMethods: v,
        children: s
      }
    );
  }
);
f.displayName = "RHFormProvider";
const D = f;
export {
  E as FormProviderCustom,
  D as RHFormProvider,
  N as useCustomForm
};
