import { useMemo as x, useCallback as I, useReducer as Z, useEffect as $ } from "../../../../../preact@10.29.0/node_modules/preact/hooks/dist/hooks.module.js";
import { m as K, ax as k, ah as B } from "./StoreProvider-CBB99upB.js";
import { V as L } from "./utils-nUjUW0ze.js";
import { a as f } from "./commonValidators-CInBRB1V.js";
try {
  let t = typeof window < "u" ? window : typeof global < "u" ? global : typeof globalThis < "u" ? globalThis : typeof self < "u" ? self : {}, a = new t.Error().stack;
  a && (t._sentryDebugIds = t._sentryDebugIds || {}, t._sentryDebugIds[a] = "0fd000eb-e6bb-4a72-8ce4-ead018e718e5", t._sentryDebugIdIdentifier = "sentry-dbid-0fd000eb-e6bb-4a72-8ce4-ead018e718e5");
} catch {
}
const _ = (t, a) => Object.keys(t).filter((F) => !a.includes(F)).reduce((F, g) => (F[g] = t[g], F), {}), z = (t, a, F, g, o) => a.reduce(
  (d, i) => ({
    ...d,
    [i]: d[i] ?? o?.[i] ?? g?.[i] ?? F
  }),
  t
), N = (t, a) => typeof t == "object" ? !B(t, a) : t !== a, D = ({
  key: t,
  value: a,
  formName: F,
  mode: g,
  defaultData: o,
  data: d,
  fieldContext: i,
  formatters: b,
  staticValidate: e
}) => {
  const h = b?.[t]?.formatter ? b[t]?.formatter : b?.[t], V = h && typeof h == "function" ? h(a, i) : a, O = !!o?.[t], r = o?.[t] !== a, c = (i.state.obscuredFields?.includes(t) ? r : r || O) ? e({ key: t, value: V, mode: g, formName: F }, d, i) : new L([
    {
      isValid: !0,
      hasError: !1
    }
  ]), n = i.state?.asyncErrors?.[t];
  return n && c.addError(n), [V, c];
};
function G({
  requiredFields: t,
  staticValidate: a,
  defaultData: F,
  obscuredFields: g,
  optionalFields: o,
  trustedFields: d,
  fieldProblems: i,
  formatters: b,
  data: e,
  asyncErrors: h
}) {
  const V = (E, c) => {
    const [n, M] = D({
      key: E,
      value: e?.[c]?.[E] ?? F?.[c]?.[E] ?? null,
      // When requiredField changes prefill any defaultData if no data is input
      mode: "blur",
      formName: c,
      formatters: b?.[c],
      defaultData: F?.[c],
      data: e,
      fieldContext: {
        state: {
          data: e?.[c],
          obscuredFields: g?.[c],
          optionalFields: o?.[c],
          trustedFields: d?.[c],
          requiredFields: t?.[c],
          asyncErrors: h?.[c]
        }
      },
      staticValidate: a
    });
    return {
      valid: M.isValid && !i?.[c]?.[E] || !1,
      errors: null,
      data: n,
      fieldProblems: i?.[c]?.[E]
    };
  };
  if (!t)
    return {
      data: F,
      errors: {},
      fieldProblems: {},
      valid: {},
      requiredFields: {},
      asyncErrors: {},
      defaultData: F
    };
  const O = Object.keys(o ?? {}), r = Object.keys(t);
  return Array.from(
    /* @__PURE__ */ new Set([...O, ...r])
  ).reduce(
    (E, c) => {
      const n = c, M = o?.[n] ?? [], s = t?.[n] ?? [], p = [...M, ...s]?.reduce(
        (y, l) => {
          const {
            valid: T,
            errors: A,
            data: R,
            fieldProblems: S
          } = V(l, n);
          return {
            ...y,
            data: { ...y.data, [l]: R },
            valid: { ...y.valid, [l]: T },
            errors: { ...y.errors, [l]: A },
            fieldProblems: {
              ...y.fieldProblems,
              ...S ? { [l]: S } : {}
            }
          };
        },
        {}
      );
      return {
        requiredFields: t,
        data: {
          ...E.data,
          [c]: p.data
        },
        valid: {
          ...E.valid,
          [c]: p.valid
        },
        errors: {
          ...E.errors,
          [c]: p.errors
        },
        fieldProblems: {
          ...E.fieldProblems,
          [c]: p.fieldProblems
        },
        asyncErrors: {
          ...E.asyncErrors
        },
        obscuredFields: g,
        optionalFields: o,
        trustedFields: d,
        defaultData: F
      };
    },
    {
      requiredFields: t,
      data: {},
      valid: {},
      errors: {},
      fieldProblems: {},
      obscuredFields: g,
      optionalFields: o,
      trustedFields: d,
      asyncErrors: {}
    }
  );
}
function m({
  staticValidate: t,
  obscuredFields: a,
  optionalFields: F,
  trustedFields: g,
  formatters: o,
  defaultData: d
}) {
  return (i, {
    type: b,
    key: e,
    mode: h,
    value: V,
    formData: O,
    formName: r,
    selectedSchema: q,
    innerFormFields: E,
    requiredFields: c,
    optionalFields: n,
    asyncValidationResults: M
  }) => {
    const s = structuredClone(i);
    switch (b) {
      case "setData":
        return !e || !r ? i : {
          ...s,
          data: {
            ...s.data,
            [r]: {
              ...s.data?.[r],
              [e]: V
            }
          }
        };
      case "setFormData": {
        if (!r || !O) return i;
        const w = s.requiredFields?.[r] ?? [], p = s.optionalFields?.[r] ?? [], y = [...w, ...p], l = Object.keys(O).filter((R) => y.includes(R)).reduce((R, S) => (R[S] = O[S], R), {}), T = {
          // provide the updated data before running validations
          ...s.data,
          [r]: l
        }, A = y.reduce(
          (R, S) => {
            const [, v] = D({
              key: S,
              value: l[S],
              mode: "blur",
              formName: r,
              defaultData: d?.[r],
              data: T,
              formatters: o?.[r],
              fieldContext: {
                state: {
                  data: s?.data?.[r],
                  obscuredFields: a?.[r],
                  optionalFields: F?.[r],
                  trustedFields: g?.[r],
                  errors: s.errors?.[r],
                  valid: s.valid?.[r],
                  fieldProblems: s.fieldProblems?.[r],
                  asyncErrors: s.asyncErrors?.[r]
                }
              },
              staticValidate: t
            });
            return {
              ...R,
              [S]: v.isValid
            };
          },
          {}
        );
        return {
          ...s,
          data: {
            ...s.data,
            [r]: {
              // ...with the new, filtered updates.
              ...l
            }
          },
          valid: {
            ...s.valid,
            [r]: A
          }
        };
      }
      case "setValid":
        return !e || !r || s?.valid?.[r]?.[e] === V ? i : {
          ...s,
          valid: {
            ...s.valid,
            [r]: {
              ...s.valid?.[r],
              [e]: V
            }
          }
        };
      case "setErrors":
        return !e || !r ? i : {
          ...s,
          errors: {
            ...s.errors,
            [r]: {
              ...s.errors?.[r],
              [e]: V
            }
          }
        };
      case "setRequiredFields": {
        const w = !B(d, i.defaultData);
        if (B(c, s.requiredFields) && B(n, s.optionalFields) && !w)
          return i;
        const p = w ? d : s.data;
        return G({
          requiredFields: c,
          data: p,
          fieldProblems: s.fieldProblems,
          obscuredFields: a,
          optionalFields: n,
          trustedFields: g,
          staticValidate: t,
          defaultData: d
        });
      }
      case "setInnerFormRequiredFields": {
        if (!E || !r) return i;
        const w = {
          ...s.requiredFields,
          [r]: E
        }, p = G({
          requiredFields: w,
          defaultData: d,
          fieldProblems: s.fieldProblems,
          obscuredFields: a,
          optionalFields: F,
          trustedFields: g,
          staticValidate: t,
          data: d
        }), y = s.optionalFields?.[r].length ? [
          ...(s.requiredFields?.[r] ?? []).filter(
            (j) => !E.includes(j)
          ),
          ...s.optionalFields[r].filter(
            (j) => !F?.[r].includes(j)
          )
        ] : s.requiredFields?.[r].filter(
          (j) => !E.includes(j)
        ), l = F?.[r].length ? [
          ...E.filter(
            (j) => !s.requiredFields?.[r].includes(j)
          ),
          ...(F[r] ?? []).filter(
            (j) => !s.optionalFields?.[r].includes(j)
          )
        ] : E.filter(
          (j) => !s.requiredFields?.[r].includes(j)
        ), T = {
          data: _(s.data?.[r], l),
          errors: _(s.errors?.[r], l),
          valid: _(s.valid?.[r], l),
          fieldProblems: _(s.fieldProblems?.[r], l)
        }, A = {
          ...s.local,
          [r]: T
        }, R = z(
          _(s.data?.[r], y),
          l,
          null,
          p.data?.[r],
          s.local?.[r].data
        ), S = {
          ...s.data,
          [r]: R
        }, v = z(
          _(s.valid?.[r], y),
          l,
          !1,
          p.valid?.[r],
          s.local?.[r].valid
        ), u = {
          ...s.valid,
          [r]: v
        }, P = z(
          _(s.errors?.[r], y),
          l,
          null,
          p.errors?.[r],
          s.local?.[r].errors
        ), C = {
          ...s.errors,
          [r]: P
        }, H = z(
          _(s.fieldProblems?.[r], y),
          l,
          !1,
          p.fieldProblems?.[r],
          s.local?.[r].fieldProblems
        ), U = {
          ...s.fieldProblems,
          [r]: H
        };
        return {
          requiredFields: w,
          data: S,
          valid: u,
          errors: C,
          fieldProblems: U,
          asyncErrors: s.asyncErrors,
          optionalFields: F,
          trustedFields: g,
          local: A,
          defaultData: d
        };
      }
      case "updateField": {
        if (!e || !h || !r) return i;
        const w = {
          // provide the updated data before running validations
          ...s.data,
          [r]: {
            ...s.data?.[r],
            [e]: V
          }
        }, p = N(V, s.data?.[r]?.[e]) ? {
          ...s.asyncErrors,
          [r]: {
            ...s.asyncErrors?.[r],
            [e]: null
          }
        } : s.asyncErrors, [y, l] = D({
          key: e,
          value: V,
          mode: h,
          formName: r,
          defaultData: d?.[r],
          data: w,
          formatters: o?.[r],
          fieldContext: {
            state: {
              data: s.data?.[r],
              obscuredFields: a?.[r],
              optionalFields: F?.[r],
              trustedFields: g?.[r],
              errors: s.errors[r],
              valid: s.valid[r],
              fieldProblems: s.fieldProblems[r],
              asyncErrors: p[r]
            }
          },
          staticValidate: t
        }), A = s.data?.[r]?.[e] !== y;
        return {
          ...s,
          data: {
            ...s.data,
            [r]: {
              ...s.data?.[r],
              [e]: y
            }
          },
          asyncErrors: p,
          errors: {
            ...s.errors,
            [r]: {
              ...s.errors[r],
              [e]: l.getError() ?? null
            }
          },
          valid: {
            ...s.valid,
            [r]: {
              ...s?.valid?.[r],
              [e]: (l.isValid && !s.fieldProblems?.[r]?.[e]) ?? !1
            }
          },
          fieldProblems: {
            ...s.fieldProblems,
            [r]: {
              ...s.fieldProblems[r],
              ...!A && s.fieldProblems?.[r]?.[e] ? { [e]: !0 } : {}
            }
          }
        };
      }
      case "validateForm": {
        if (!r)
          return i;
        const w = s.requiredFields?.[r];
        if (!w)
          return i;
        const p = (q || w).reduce(
          (y, l) => {
            const T = s.data?.[r], [A, R] = D({
              key: l,
              mode: "blur",
              value: T?.[l] ?? null,
              formName: r,
              defaultData: d?.[r],
              data: s.data,
              formatters: o?.[r],
              fieldContext: {
                state: {
                  data: s.data?.[r],
                  obscuredFields: a?.[r],
                  optionalFields: F?.[r],
                  trustedFields: g?.[r],
                  errors: s.errors[r],
                  valid: s.valid[r],
                  fieldProblems: s.fieldProblems[r],
                  asyncErrors: s.asyncErrors[r]
                }
              },
              staticValidate: t
            }), v = s?.data?.[r]?.[l] !== A, u = M?.[l];
            return {
              data: T,
              asyncErrors: {
                ...y.asyncErrors,
                [l]: u?.getError() ?? null
              },
              errors: {
                ...y.errors,
                [l]: R.getError() ?? u?.getError() ?? null
              },
              valid: {
                ...y.valid,
                [l]: R.isValid && (u?.isValid ?? !0) && !s.fieldProblems[r][l]
              },
              fieldProblems: {
                ...y.fieldProblems,
                ...!v && y.fieldProblems?.[l] ? { [l]: !0 } : {}
              }
            };
          },
          {}
        );
        return {
          ...s,
          errors: {
            ...s.errors,
            [r]: p.errors
          },
          asyncErrors: {
            ...s.asyncErrors,
            [r]: p.asyncErrors
          },
          valid: {
            ...s.valid,
            [r]: p.valid
          },
          fieldProblems: {
            ...s.fieldProblems,
            [r]: p.fieldProblems
          }
        };
      }
      default:
        throw new Error("Undefined useForm action");
    }
  };
}
const rr = K(), sr = (t) => {
  const a = {
    isValid: !0,
    hasError: !1
  }, F = I(
    async (o, d, i) => {
      if (!t) return new L([a]);
      const b = typeof t == "function" ? t(i)[d]?.[o] : t[d][o], e = i?.[d]?.[o];
      if (!b || !e) return new L([a]);
      const h = await b.asyncValidate(e).catch(rr.error);
      return h === !1 ? new L([
        {
          isValid: h,
          errorMessage: typeof b.errorMessage == "function" ? b.errorMessage(e) : b.errorMessage,
          hasError: !h
        }
      ]) : new L([a]);
    },
    [t]
  );
  return {
    triggerAsyncFormValidation: async (o, d, i) => {
      const b = i ?? d.requiredFields?.[o];
      if (!b?.length)
        return {
          isValid: !0
        };
      const e = await b.reduce(async (V, O) => {
        const r = await V, q = await F(
          O,
          o,
          d.data
        );
        return {
          ...r,
          [O]: q
        };
      }, Promise.resolve({})), h = Object.values(e).every(
        (V) => V.isValid
      );
      return {
        ...e,
        isValid: h
      };
    }
  };
}, tr = (t) => {
  const a = I(
    (g, o, d) => {
      const i = {
        validate: () => !0,
        modes: ["blur", "input"]
      };
      if (!o)
        return [i];
      const b = typeof t == "function" ? t(d)?.[o]?.[g] ?? [i] : t?.[o]?.[g] ?? [i];
      return Array.isArray(b) ? b : [b];
    },
    [t]
  );
  return {
    triggerStaticValidation: I(
      ({ key: g, value: o, mode: d = "blur", formName: i }, b, e) => {
        const h = a(g, i, b), V = !!e?.state.optionalFields?.includes(g), O = !!e?.state.obscuredFields?.includes(g);
        V || h.unshift(f);
        const r = h.map((q) => {
          const E = q.modes.includes(d) && !O, c = V && k(o) ? !0 : q.validate(o, e);
          return {
            isValid: c,
            errorMessage: typeof q.errorMessage == "function" ? q.errorMessage(o, e) : q.errorMessage,
            hasError: E && !c
          };
        });
        return new L(r);
      },
      [a]
    )
  };
};
function nr({
  defaultData: t,
  rules: a,
  asyncRules: F,
  fieldProblems: g,
  allFields: o,
  requiredFields: d,
  obscuredFields: i,
  optionalFields: b,
  trustedFields: e,
  formatters: h
}) {
  const { triggerStaticValidation: V } = tr(a), { triggerAsyncFormValidation: O } = sr(F), r = x(
    () => typeof d == "function" ? d : () => d,
    [d]
  ), q = x(() => r(t), [t, r]), E = I(
    () => m({
      staticValidate: V,
      defaultData: t,
      obscuredFields: i,
      optionalFields: b,
      trustedFields: e,
      formatters: h
    }),
    [
      V,
      i,
      b,
      e,
      h,
      t
    ]
  ), c = x(
    () => ({
      requiredFields: q,
      defaultData: t,
      fieldProblems: g,
      obscuredFields: i,
      optionalFields: b,
      trustedFields: e,
      formatters: h,
      staticValidate: V,
      data: t
    }),
    [
      q,
      t,
      g,
      i,
      b,
      e,
      h,
      V
    ]
  ), [n, M] = Z(E(), c, G), s = x(() => {
    const v = Object.keys(n.optionalFields ?? {}), u = Object.keys(n.requiredFields ?? {});
    return Array.from(
      /* @__PURE__ */ new Set([...v, ...u])
    ).reduce((C, H) => {
      const U = H, j = n.optionalFields?.[U] ?? [], J = n.requiredFields?.[U] ?? [], Q = [...j, ...J], W = n.valid[H] ?? {};
      return {
        ...C,
        [H]: Object.entries(W)?.every(
          ([X, Y]) => Q.includes(X) ? Y : !0
        )
      };
    }, {});
  }, [n.requiredFields, n.valid]), w = I(
    (v, u, P) => P?.target ? P.target.type === "checkbox" ? !n.data?.[u][v] : P.target.value : P,
    [n.data]
  ), p = I(
    (v, u, P) => {
      M({ type: "setErrors", key: v, value: u, formName: P });
    },
    []
  ), y = I(
    (v, u, P) => {
      M({ type: "setValid", key: v, value: u, formName: P });
    },
    []
  ), l = I(
    (v, u, P) => {
      M({ type: "setData", key: v, value: u, formName: P });
    },
    []
  ), T = I(
    (v, u) => {
      M({ type: "setFormData", formName: v, formData: u });
    },
    []
  ), A = I(
    (v, u, P = "blur") => (C) => {
      const H = w(v, u, C);
      M({
        type: "updateField",
        key: v,
        value: H,
        mode: P,
        formName: u
      });
    },
    [w]
  ), R = I(
    async (v, u) => {
      const P = await O(
        v,
        n,
        u
      );
      return M({
        type: "validateForm",
        formName: v,
        selectedSchema: u,
        asyncValidationResults: P
      }), P.isValid;
    },
    [n, O]
  ), S = I(() => {
    q && Object.entries(q).forEach(([v]) => {
      const u = v, P = t?.[u];
      q[u].forEach((C) => {
        if (!P || typeof P[C] < "u") {
          A(C, u)(P);
          return;
        }
        y(C, !1, u), p(C, null, u), l(C, P[C], u);
      });
    });
  }, [t, q, A, l, p, y]);
  return $(() => {
    const v = r(n.data);
    v && M({
      type: "setRequiredFields",
      requiredFields: v,
      defaultData: t,
      optionalFields: b
    });
  }, [r, n.data, t, b]), {
    setData: l,
    setFormData: T,
    setValid: y,
    setErrors: p,
    handleChangeFor: A,
    triggerValidation: R,
    resetToDefaultData: S,
    isValid: s,
    allFields: o,
    requiredFields: n.requiredFields,
    optionalFields: n.optionalFields,
    trustedFields: n.trustedFields,
    obscuredFields: n.obscuredFields,
    valid: n.valid,
    errors: n.errors,
    data: n.data,
    fieldProblems: n.fieldProblems,
    defaultData: t,
    asyncErrors: n.asyncErrors
  };
}
export {
  nr as u
};
