import { useState as k, useCallback as n } from "react";
import { useFormActions as sr } from "./actions/actions.mjs";
import { getInputOnChange as tr } from "./get-input-on-change/get-input-on-change.mjs";
import { useFormErrors as or } from "./hooks/use-form-errors/use-form-errors.mjs";
import { useFormList as lr } from "./hooks/use-form-list/use-form-list.mjs";
import { useFormStatus as cr } from "./hooks/use-form-status/use-form-status.mjs";
import { useFormValues as ir } from "./hooks/use-form-values/use-form-values.mjs";
import { useFormWatch as ur } from "./hooks/use-form-watch/use-form-watch.mjs";
import { getPath as m } from "./paths/get-path.mjs";
import { getDataPath as w } from "./paths/get-data-path.mjs";
import { validateValues as j } from "./validate/validate-values.mjs";
import { validateFieldValue as y } from "./validate/validate-field-value.mjs";
import { shouldValidateOnChange as x } from "./validate/should-validate-on-change.mjs";
function Lr({
  name: D,
  mode: c = "controlled",
  initialValues: O,
  initialErrors: U = {},
  initialDirty: q = {},
  initialTouched: A = {},
  clearInputErrorOnChange: T = !0,
  validateInputOnChange: B = !1,
  validateInputOnBlur: N = !1,
  onValuesChange: b,
  transformValues: v = (t) => t,
  enhanceGetInputProps: L,
  validate: i,
  onSubmitPreventDefault: z = "always",
  touchTrigger: W = "change"
} = {}) {
  const t = or(U), e = ir({ initialValues: O, onValuesChange: b, mode: c }), l = cr({ initialDirty: q, initialTouched: A, $values: e, mode: c }), V = lr({ $values: e, $errors: t, $status: l }), F = ur({ $status: l }), [H, $] = k(0), [J, M] = k({}), [P, S] = k(!1), C = n(() => {
    e.resetValues(), t.clearErrors(), l.resetDirty(), l.resetTouched(), c === "uncontrolled" && $((r) => r + 1);
  }, []), E = n(
    (r) => {
      T && t.clearErrors(), c === "uncontrolled" && $((s) => s + 1), Object.keys(F.subscribers.current).forEach((s) => {
        const o = m(s, e.refValues.current), u = m(s, r);
        o !== u && F.getFieldSubscribers(s).forEach((d) => d({ previousValues: r, updatedValues: e.refValues.current }));
      });
    },
    [T]
  ), Q = n(
    (r) => {
      const s = e.refValues.current;
      e.initialize(r, () => c === "uncontrolled" && $((o) => o + 1)), E(s);
    },
    [E]
  ), K = n(
    (r, s, o) => {
      const u = x(r, B), d = s instanceof Function ? s(m(r, e.refValues.current)) : s;
      l.setCalculatedFieldDirty(r, d), W === "change" && l.setFieldTouched(r, !0), !u && T && t.clearFieldError(r), e.setFieldValue({
        path: r,
        value: s,
        updateState: c === "controlled",
        subscribers: [
          ...F.getFieldSubscribers(r),
          u ? (f) => {
            const a = y(r, i, f.updatedValues);
            a.hasError ? t.setFieldError(r, a.error) : t.clearFieldError(r);
          } : null,
          (o == null ? void 0 : o.forceUpdate) !== !1 && c !== "controlled" ? () => M((f) => ({
            ...f,
            [r]: (f[r] || 0) + 1
          })) : null
        ]
      });
    },
    [b, i]
  ), X = n(
    (r) => {
      const s = e.refValues.current;
      e.setValues({ values: r, updateState: c === "controlled" }), E(s);
    },
    [b, E]
  ), R = n(() => {
    const r = j(i, e.refValues.current);
    return t.setErrors(r.errors), r;
  }, [i]), Y = n(
    (r) => {
      const s = y(r, i, e.refValues.current);
      return s.hasError ? t.setFieldError(r, s.error) : t.clearFieldError(r), s;
    },
    [i]
  ), Z = (r, { type: s = "input", withError: o = !0, withFocus: u = !0, ...d } = {}) => {
    const a = { onChange: tr(
      (g) => K(r, g, { forceUpdate: !1 })
    ), "data-path": w(D, r) };
    return o && (a.error = t.errorsState[r]), s === "checkbox" ? a[c === "controlled" ? "checked" : "defaultChecked"] = m(
      r,
      e.refValues.current
    ) : a[c === "controlled" ? "value" : "defaultValue"] = m(
      r,
      e.refValues.current
    ), u && (a.onFocus = () => l.setFieldTouched(r, !0), a.onBlur = () => {
      if (x(r, N)) {
        const g = y(r, i, e.refValues.current);
        g.hasError ? t.setFieldError(r, g.error) : t.clearFieldError(r);
      }
    }), Object.assign(
      a,
      L == null ? void 0 : L({
        inputProps: a,
        field: r,
        options: { type: s, withError: o, withFocus: u, ...d },
        form: I
      })
    );
  }, _ = (r, s) => (o) => {
    z === "always" && (o == null || o.preventDefault());
    const u = R();
    if (u.hasErrors)
      z === "validation-failed" && (o == null || o.preventDefault()), s == null || s(u.errors, e.refValues.current, o);
    else {
      const d = r == null ? void 0 : r(
        v(e.refValues.current),
        o
      );
      d instanceof Promise && (S(!0), d.finally(() => S(!1)));
    }
  }, p = (r) => v(r || e.refValues.current), h = n((r) => {
    r.preventDefault(), C();
  }, []), G = n(
    (r) => r ? !y(r, i, e.refValues.current).hasError : !j(i, e.refValues.current).hasErrors,
    [i]
  ), rr = (r) => `${H}-${r}-${J[r] || 0}`, er = n(
    (r) => document.querySelector(`[data-path="${w(D, r)}"]`),
    []
  ), I = {
    watch: F.watch,
    initialized: e.initialized.current,
    values: e.stateValues,
    getValues: e.getValues,
    getInitialValues: e.getValuesSnapshot,
    setInitialValues: e.setValuesSnapshot,
    initialize: Q,
    setValues: X,
    setFieldValue: K,
    submitting: P,
    setSubmitting: S,
    errors: t.errorsState,
    setErrors: t.setErrors,
    setFieldError: t.setFieldError,
    clearFieldError: t.clearFieldError,
    clearErrors: t.clearErrors,
    resetDirty: l.resetDirty,
    setTouched: l.setTouched,
    setDirty: l.setDirty,
    isTouched: l.isTouched,
    resetTouched: l.resetTouched,
    isDirty: l.isDirty,
    getTouched: l.getTouched,
    getDirty: l.getDirty,
    reorderListItem: V.reorderListItem,
    insertListItem: V.insertListItem,
    removeListItem: V.removeListItem,
    replaceListItem: V.replaceListItem,
    reset: C,
    validate: R,
    validateField: Y,
    getInputProps: Z,
    onSubmit: _,
    onReset: h,
    isValid: G,
    getTransformedValues: p,
    key: rr,
    getInputNode: er
  };
  return sr(D, I), I;
}
export {
  Lr as useForm
};
//# sourceMappingURL=use-form.mjs.map
