import { useMemo as I, useCallback as V, useReducer as G, useEffect as C, useState as H } from "../../../../../preact@10.29.0/node_modules/preact/hooks/dist/hooks.module.js";
import { m as Q, b2 as W, ax as X } from "./StoreProvider-CBB99upB.js";
import { V as j, o as R, a as O } from "./utils-nUjUW0ze.js";
try {
  let n = typeof window < "u" ? window : typeof global < "u" ? global : typeof globalThis < "u" ? globalThis : typeof self < "u" ? self : {}, o = new n.Error().stack;
  o && (n._sentryDebugIds = n._sentryDebugIds || {}, n._sentryDebugIds[o] = "c1498ce3-2175-4bad-9ff6-aea1cfab578d", n._sentryDebugIdIdentifier = "sentry-dbid-c1498ce3-2175-4bad-9ff6-aea1cfab578d");
} catch {
}
const Y = Q(), Z = (n) => {
  const [o, g] = H({}), t = V((l) => {
    g((r) => ({
      ...r,
      [l]: void 0
    }));
  }, []), a = V(
    (l, r, v) => {
      const s = n?.[l];
      if (!s || !s.modes.includes(v)) {
        t(l);
        return;
      }
      const p = r.data[l];
      return s.asyncValidate(r.data[l], { state: r }).then((b) => {
        g((c) => ({
          ...c,
          [l]: new j([
            {
              isValid: b,
              errorMessage: typeof s.errorMessage == "function" ? s.errorMessage(p, { state: r }) : s.errorMessage,
              hasError: !b
            }
          ])
        }));
      }).catch(Y.error);
    },
    [n, t]
  );
  return {
    asyncValidationResults: o,
    triggerAsyncValidation: a,
    clearAsyncValidationResults: t
  };
}, $ = (n) => {
  const o = V(
    (t) => {
      const a = {
        validate: () => !0,
        modes: ["blur", "input"]
      }, l = n?.[t] ?? [a];
      return Array.isArray(l) ? l : [l];
    },
    [n]
  );
  return {
    triggerStaticValidation: V(
      ({ key: t, value: a, mode: l = "blur" }, r) => {
        const v = o(t), s = !!r?.state.optionalFields?.includes(t), p = !!r?.state.obscuredFields?.includes(t), b = v.map((c) => {
          const u = c.modes.includes(l) && !p, m = s && X(a) ? !0 : c.validate(a, r);
          return {
            isValid: m,
            errorMessage: typeof c.errorMessage == "function" ? c.errorMessage(a, r) : c.errorMessage,
            hasError: u && !m
          };
        });
        return new j(b);
      },
      [o]
    )
  };
}, q = ({
  key: n,
  value: o = null,
  mode: g,
  defaultData: t,
  fieldContext: a,
  formatters: l,
  staticValidate: r,
  asyncValidate: v
}) => {
  const s = l?.[n]?.formatter ? l[n]?.formatter : l?.[n], p = s && typeof s == "function" ? s(o, a) : o, b = !(t?.[n] === o && t?.[n]);
  b && o && v(n, a.state, g);
  const c = b ? r({ key: n, value: p, mode: g }, a) : new j([
    {
      isValid: !0,
      hasError: !1
    }
  ]);
  return [p, c];
};
function J({
  schema: n,
  defaultData: o,
  fieldProblems: g,
  obscuredFields: t,
  optionalFields: a,
  trustedFields: l,
  formatters: r,
  staticValidate: v,
  asyncValidate: s
}) {
  const p = (u) => {
    const [m, P] = q({
      key: u,
      value: o?.[u],
      mode: "blur",
      formatters: r,
      defaultData: o,
      fieldContext: {
        state: {
          data: o,
          obscuredFields: t,
          optionalFields: a,
          trustedFields: l
        }
      },
      staticValidate: v,
      asyncValidate: s
    });
    return typeof o?.[u] > "u" ? {
      valid: P.isValid && !g?.[u] || !1,
      errors: null,
      data: null,
      fieldProblems: g?.[u] ?? !1
    } : {
      valid: P.isValid && !g?.[u] || !1,
      errors: P.getError() ?? null,
      data: m,
      fieldProblems: g?.[u] ?? !1
    };
  };
  return (a ? [...n, ...a] : n).reduce(
    (u, m) => {
      const {
        valid: P,
        errors: S,
        data: M,
        fieldProblems: e
      } = p(m);
      return {
        ...u,
        data: { ...u.data, [m]: M },
        valid: { ...u.valid, [m]: P },
        errors: { ...u.errors, [m]: S },
        fieldProblems: { ...u.fieldProblems, [m]: e }
      };
    },
    {
      schema: n,
      data: {},
      valid: {},
      errors: {},
      fieldProblems: {},
      obscuredFields: t,
      optionalFields: a,
      trustedFields: l
    }
  );
}
function K({
  staticValidate: n,
  asyncValidate: o,
  obscuredFields: g,
  optionalFields: t,
  trustedFields: a,
  formatters: l
}) {
  return (r, {
    type: v,
    key: s,
    mode: p,
    value: b,
    schema: c,
    selectedSchema: u,
    formValue: m,
    fieldProblems: P,
    defaultData: S
  }) => {
    const M = u || r.schema;
    switch (v) {
      case "setData":
        return s ? { ...r, data: { ...r.data, [s]: b } } : r;
      case "setValid":
        return s ? { ...r, valid: { ...r.valid, [s]: b } } : r;
      case "setErrors":
        return s ? { ...r, errors: { ...r.errors, [s]: b } } : r;
      case "setFieldProblems":
        return r?.schema?.reduce(
          (e, d) => ({
            ...e,
            fieldProblems: {
              ...r.fieldProblems,
              [d]: P?.[d] ?? !1
            },
            valid: { ...r.valid, [d]: r.valid?.[d] && !P?.[d] }
          }),
          r
        );
      case "setSchema": {
        if (!c) return r;
        const e = J({
          schema: c,
          // requiredFields
          defaultData: S,
          fieldProblems: P,
          obscuredFields: g,
          optionalFields: t,
          trustedFields: a,
          staticValidate: n,
          asyncValidate: o
        }), d = r.optionalFields?.length ? [
          ...r.schema.filter((h) => !c.includes(h)),
          ...r.optionalFields.filter((h) => !t?.includes(h))
        ] : r.schema.filter((h) => !c.includes(h)), f = t?.length ? [
          ...c.filter((h) => !r.schema?.includes(h)),
          ...t.filter((h) => !r.optionalFields?.includes(h))
        ] : c.filter((h) => !r.schema?.includes(h)), y = {
          data: R(r.data, f),
          errors: R(r.errors, f),
          valid: R(r.valid, f),
          fieldProblems: R(r.fieldProblems, f)
        }, F = O(
          R(r.data, d),
          f,
          null,
          e.data,
          r.local?.data
        ), w = O(
          R(r.valid, d),
          f,
          !1,
          e.valid,
          r.local?.valid
        ), A = O(
          R(r.errors, d),
          f,
          null,
          e.errors,
          r.local?.errors
        ), T = O(
          R(r.fieldProblems, d),
          f,
          !1,
          e.fieldProblems,
          r.local?.fieldProblems
        );
        return {
          ...r,
          schema: c,
          data: F,
          valid: w,
          errors: A,
          fieldProblems: T,
          obscuredFields: g,
          optionalFields: t,
          trustedFields: a,
          local: y
        };
      }
      case "updateField": {
        if (!s || !p) return r;
        const [e, d] = q({
          key: s,
          value: b,
          mode: p,
          defaultData: S,
          formatters: l,
          fieldContext: { state: r },
          staticValidate: n,
          asyncValidate: o
        }), y = r.data[s] !== e;
        return {
          ...r,
          data: { ...r.data, [s]: e },
          errors: {
            ...r.errors,
            [s]: d.getError() ?? null
          },
          valid: {
            ...r.valid,
            [s]: d.isValid && !r.fieldProblems?.[s] || !1
          },
          fieldProblems: {
            ...r.fieldProblems,
            [s]: y ? !1 : r.fieldProblems?.[s] ?? !1
          }
        };
      }
      case "mergeForm": {
        const e = {
          ...r,
          data: { ...r.data, ...m?.data },
          errors: { ...r.errors, ...m?.errors },
          valid: { ...r.valid, ...m?.valid },
          fieldProblems: { ...r.fieldProblems, ...m?.fieldProblems }
        };
        return e.valid && (e.isValid = Object.values(e.valid).every((d) => d)), e;
      }
      case "validateForm": {
        const e = M?.reduce(
          (d, f) => {
            const [y, F] = q({
              key: f,
              value: r.data[f],
              mode: "blur",
              defaultData: S,
              formatters: l,
              fieldContext: { state: r },
              staticValidate: n,
              asyncValidate: o
            }), A = r.data[f] !== y;
            return {
              valid: {
                ...d.valid,
                [f]: F.isValid && !r.fieldProblems?.[f] || !1
              },
              errors: {
                ...d.errors,
                [f]: F.getError() ?? null
              },
              fieldProblems: {
                ...d.fieldProblems,
                [f]: A ? !1 : r.fieldProblems?.[f] ?? !1
              }
            };
          },
          { valid: r.valid, errors: r.errors, fieldProblems: r.fieldProblems }
        );
        return {
          ...r,
          valid: e?.valid,
          errors: e?.errors,
          fieldProblems: e?.fieldProblems
        };
      }
      default:
        throw new Error("Undefined useForm action");
    }
  };
}
const D = (n, o, g, t, a) => {
  const l = a ? [...g, ...a] : g;
  return l.length ? l.reduce(
    (v, s) => (o[s]?.getError() && t(s, !1), {
      ...v,
      [s]: n[s] ?? o[s]?.getError() ?? null
    }),
    {}
  ) : n;
};
function or({
  schema: n,
  defaultData: o,
  rules: g,
  asyncRules: t,
  fieldProblems: a,
  obscuredFields: l,
  optionalFields: r,
  trustedFields: v,
  formatters: s,
  shouldValidate: p = !1
}) {
  const { triggerStaticValidation: b } = $(g), { triggerAsyncValidation: c, asyncValidationResults: u } = Z(t), m = I(
    () => typeof n == "function" ? n : () => n,
    [n]
  ), P = I(
    () => o ? m(o) : [],
    [o, m]
  ), S = V(
    () => K({
      staticValidate: b,
      asyncValidate: c,
      obscuredFields: l,
      optionalFields: r,
      trustedFields: v,
      formatters: s
    }),
    [
      b,
      c,
      l,
      r,
      v,
      s
    ]
  ), M = I(
    () => ({
      schema: P,
      defaultData: o,
      fieldProblems: a,
      obscuredFields: l,
      optionalFields: r,
      trustedFields: v,
      formatters: s,
      staticValidate: b,
      asyncValidate: c
    }),
    [
      P,
      o,
      a,
      l,
      r,
      v,
      s,
      b,
      c
    ]
  ), [e, d] = G(S(), M, J), f = I(
    () => e.schema?.every((i) => e.valid[i]),
    [e.schema, e.valid]
  ), y = V(
    (i, E) => E?.target ? E.target.type === "checkbox" ? !e.data[i] : E.target.value : E,
    [e.data]
  ), F = V((i, E) => {
    d({ type: "setErrors", key: i, value: E });
  }, []), w = V((i, E) => {
    d({ type: "setValid", key: i, value: E });
  }, []), A = V((i, E) => {
    d({ type: "setData", key: i, value: E });
  }, []), T = V(
    (i) => {
      d({ type: "setSchema", schema: i, defaultData: o });
    },
    [o]
  ), h = V(
    (i) => {
      d({ type: "setFieldProblems", fieldProblems: i });
    },
    []
  ), L = V((i) => {
    d({ type: "mergeForm", formValue: i });
  }, []), _ = V(
    (i, E = "blur") => (z) => {
      const B = y(i, z);
      d({
        type: "updateField",
        key: i,
        value: B,
        mode: E,
        defaultData: o
      });
    },
    [o, y]
  ), x = V(
    (i) => {
      d({
        type: "validateForm",
        selectedSchema: i,
        defaultData: o
      });
    },
    [o]
  ), N = V(() => {
    P.forEach((i) => {
      if (!o || typeof o[i] < "u") {
        _(i)(o?.[i]);
        return;
      }
      w(i, !1), F(i, null), A(i, o[i]);
    });
  }, [o, P, _, A, F, w]);
  C(() => {
    const i = m(e.data);
    W(e.schema, i) || T(i);
  }, [e.schema, e.data, T, m]), C(() => {
    p && x();
  }, [p, e.schema]), C(() => {
    a && h(a);
  }, [JSON.stringify(a), h]);
  const U = I(
    () => D(
      e.errors,
      u,
      e.schema,
      w,
      e.optionalFields
    ),
    [u, e.errors, e.optionalFields, e.schema]
  );
  return {
    mergeForm: L,
    setData: A,
    setValid: w,
    setErrors: F,
    setFieldProblems: h,
    handleChangeFor: _,
    triggerValidation: x,
    resetToDefaultData: N,
    isValid: f,
    schema: e.schema,
    valid: e.valid,
    errors: U,
    data: e.data,
    fieldProblems: e.fieldProblems
  };
}
export {
  or as u
};
