"use client";
import o from "react";
var $ = (e) => e.type === "checkbox", b = (e) => e instanceof Date, E = (e) => e == null;
const B = (e) => typeof e == "object";
var g = (e) => !E(e) && !Array.isArray(e) && B(e) && !b(e), q = (e) => g(e) && e.target ? $(e.target) ? e.target.checked : e.target.value : e, K = (e) => e.substring(0, e.search(/\.\d+(\.|$)/)) || e, z = (e, s) => e.has(K(s)), J = (e) => {
  const s = e.constructor && e.constructor.prototype;
  return g(s) && s.hasOwnProperty("isPrototypeOf");
}, Q = typeof window < "u" && typeof window.HTMLElement < "u" && typeof document < "u";
function T(e) {
  let s;
  const t = Array.isArray(e), n = typeof FileList < "u" ? e instanceof FileList : !1;
  if (e instanceof Date)
    s = new Date(e);
  else if (!(Q && (e instanceof Blob || n)) && (t || g(e)))
    if (s = t ? [] : Object.create(Object.getPrototypeOf(e)), !t && !J(e))
      s = e;
    else
      for (const r in e)
        e.hasOwnProperty(r) && (s[r] = T(e[r]));
  else
    return e;
  return s;
}
var U = (e) => /^\w*$/.test(e), x = (e) => e === void 0, X = (e) => Array.isArray(e) ? e.filter(Boolean) : [], I = (e) => X(e.replace(/["|']|\]/g, "").split(/\.|\[/)), f = (e, s, t) => {
  if (!s || !g(e))
    return t;
  const n = (U(s) ? [s] : I(s)).reduce((r, a) => E(r) ? r : r[a], e);
  return x(n) || n === e ? x(e[s]) ? t : e[s] : n;
}, O = (e) => typeof e == "boolean", M = (e, s, t) => {
  let n = -1;
  const r = U(s) ? [s] : I(s), a = r.length, i = a - 1;
  for (; ++n < a; ) {
    const u = r[n];
    let y = t;
    if (n !== i) {
      const l = e[u];
      y = g(l) || Array.isArray(l) ? l : isNaN(+r[n + 1]) ? {} : [];
    }
    if (u === "__proto__" || u === "constructor" || u === "prototype")
      return;
    e[u] = y, e = e[u];
  }
};
const R = {
  BLUR: "blur",
  CHANGE: "change"
}, L = {
  all: "all"
}, S = o.createContext(null);
S.displayName = "HookFormContext";
const P = () => o.useContext(S), ne = (e) => {
  const { children: s, ...t } = e;
  return o.createElement(S.Provider, { value: t }, s);
};
var Y = (e, s, t, n = !0) => {
  const r = {
    defaultValues: s._defaultValues
  };
  for (const a in e)
    Object.defineProperty(r, a, {
      get: () => {
        const i = a;
        return s._proxyFormState[i] !== L.all && (s._proxyFormState[i] = !n || L.all), t && (t[i] = !0), e[i];
      }
    });
  return r;
};
const H = typeof window < "u" ? o.useLayoutEffect : o.useEffect;
function Z(e) {
  const s = P(), { control: t = s.control, disabled: n, name: r, exact: a } = e || {}, [i, u] = o.useState(t._formState), y = o.useRef({
    isDirty: !1,
    isLoading: !1,
    dirtyFields: !1,
    touchedFields: !1,
    validatingFields: !1,
    isValidating: !1,
    isValid: !1,
    errors: !1
  });
  return H(() => t._subscribe({
    name: r,
    formState: y.current,
    exact: a,
    callback: (l) => {
      !n && u({
        ...t._formState,
        ...l
      });
    }
  }), [r, n, a]), o.useEffect(() => {
    y.current.isValid && t._setValid(!0);
  }, [t]), o.useMemo(() => Y(i, t, y.current, !1), [i, t]);
}
var j = (e) => typeof e == "string", ee = (e, s, t, n, r) => j(e) ? f(t, e, r) : Array.isArray(e) ? e.map((a) => f(t, a)) : t, D = (e) => E(e) || !B(e);
function W(e, s, t = /* @__PURE__ */ new WeakSet()) {
  if (D(e) || D(s))
    return e === s;
  if (b(e) && b(s))
    return e.getTime() === s.getTime();
  const n = Object.keys(e), r = Object.keys(s);
  if (n.length !== r.length)
    return !1;
  if (t.has(e) || t.has(s))
    return !0;
  t.add(e), t.add(s);
  for (const a of n) {
    const i = e[a];
    if (!r.includes(a))
      return !1;
    if (a !== "ref") {
      const u = s[a];
      if (b(i) && b(u) || g(i) && g(u) || Array.isArray(i) && Array.isArray(u) ? !W(i, u, t) : i !== u)
        return !1;
    }
  }
  return !0;
}
function te(e) {
  const s = P(), { control: t = s.control, name: n, defaultValue: r, disabled: a, exact: i, compute: u } = e || {}, y = o.useRef(r), l = o.useRef(u), c = o.useRef(void 0);
  l.current = u;
  const m = o.useMemo(() => t._getWatch(n, y.current), [t, n]), [h, V] = o.useState(l.current ? l.current(m) : m);
  return H(() => t._subscribe({
    name: n,
    formState: {
      values: !0
    },
    exact: i,
    callback: (F) => {
      if (!a) {
        const v = ee(n, t._names, F.values || t._formValues, !1, y.current);
        if (l.current) {
          const _ = l.current(v);
          W(_, c.current) || (V(_), c.current = _);
        } else
          V(v);
      }
    }
  }), [t, a, n, i]), o.useEffect(() => t._removeUnmounted()), h;
}
function re(e) {
  const s = P(), { name: t, disabled: n, control: r = s.control, shouldUnregister: a, defaultValue: i } = e, u = z(r._names.array, t), y = o.useMemo(() => f(r._formValues, t, f(r._defaultValues, t, i)), [r, t, i]), l = te({
    control: r,
    name: t,
    defaultValue: y,
    exact: !0
  }), c = Z({
    control: r,
    name: t,
    exact: !0
  }), m = o.useRef(e), h = o.useRef(void 0), V = o.useRef(r.register(t, {
    ...e.rules,
    value: l,
    ...O(e.disabled) ? { disabled: e.disabled } : {}
  }));
  m.current = e;
  const F = o.useMemo(() => Object.defineProperties({}, {
    invalid: {
      enumerable: !0,
      get: () => !!f(c.errors, t)
    },
    isDirty: {
      enumerable: !0,
      get: () => !!f(c.dirtyFields, t)
    },
    isTouched: {
      enumerable: !0,
      get: () => !!f(c.touchedFields, t)
    },
    isValidating: {
      enumerable: !0,
      get: () => !!f(c.validatingFields, t)
    },
    error: {
      enumerable: !0,
      get: () => f(c.errors, t)
    }
  }), [c, t]), v = o.useCallback((d) => V.current.onChange({
    target: {
      value: q(d),
      name: t
    },
    type: R.CHANGE
  }), [t]), _ = o.useCallback(() => V.current.onBlur({
    target: {
      value: f(r._formValues, t),
      name: t
    },
    type: R.BLUR
  }), [t, r._formValues]), w = o.useCallback((d) => {
    const p = f(r._fields, t);
    p && d && (p._f.ref = {
      focus: () => d.focus && d.focus(),
      select: () => d.select && d.select(),
      setCustomValidity: (A) => d.setCustomValidity(A),
      reportValidity: () => d.reportValidity()
    });
  }, [r._fields, t]), N = o.useMemo(() => ({
    name: t,
    value: l,
    ...O(n) || c.disabled ? { disabled: c.disabled || n } : {},
    onChange: v,
    onBlur: _,
    ref: w
  }), [t, n, c.disabled, v, _, w, l]);
  return o.useEffect(() => {
    const d = r._options.shouldUnregister || a, p = h.current;
    p && p !== t && !u && r.unregister(p), r.register(t, {
      ...m.current.rules,
      ...O(m.current.disabled) ? { disabled: m.current.disabled } : {}
    });
    const A = (k, G) => {
      const C = f(r._fields, k);
      C && C._f && (C._f.mount = G);
    };
    if (A(t, !0), d) {
      const k = T(f(r._options.defaultValues, t, m.current.defaultValue));
      M(r._defaultValues, t, k), x(f(r._formValues, t)) && M(r._formValues, t, k);
    }
    return !u && r.register(t), h.current = t, () => {
      (u ? d && !r._state.action : d) ? r.unregister(t) : A(t, !1);
    };
  }, [t, r, u, a]), o.useEffect(() => {
    r._setDisabledField({
      disabled: n,
      name: t
    });
  }, [n, t, r]), o.useMemo(() => ({
    field: N,
    formState: c,
    fieldState: F
  }), [N, c, F]);
}
const oe = (e) => e.render(re(e));
export {
  oe as Controller,
  ne as FormProvider,
  f as get,
  M as set,
  re as useController,
  P as useFormContext,
  Z as useFormState,
  te as useWatch
};
