import { useState as p, useRef as R, useCallback as F, useMemo as A } from "../../../../../preact@10.29.0/node_modules/preact/hooks/dist/hooks.module.js";
import { m as B, z as _ } from "./StoreProvider-CBB99upB.js";
import { s as x } from "./Summary-C5uRwUKn.js";
import { t as g } from "./trackNavigation-db_h46BT.js";
import { jsx as w } from "../../../../../preact@10.29.0/node_modules/preact/jsx-runtime/dist/jsxRuntime.module.js";
import { F as q } from "./ErrorPanel-D4kO8XQ8.js";
import { f as C } from "./debugStore-BvbyN1Oi.js";
try {
  let t = typeof window < "u" ? window : typeof global < "u" ? global : typeof globalThis < "u" ? globalThis : typeof self < "u" ? self : {}, o = new t.Error().stack;
  o && (t._sentryDebugIds = t._sentryDebugIds || {}, t._sentryDebugIds[o] = "3a7c2924-5e59-4741-bc49-2ee0557bdd36", t._sentryDebugIdIdentifier = "sentry-dbid-3a7c2924-5e59-4741-bc49-2ee0557bdd36");
} catch {
}
const j = (t, o) => {
  if (o && Object.keys(o).length > 0) {
    const e = Object.values(o).flat().filter((r) => r.forms?.length > 0);
    if (e.length === 1 && e[0]?.forms.length === 1) {
      const r = t.find((s) => s?.formId === e[0]?.forms[0]);
      if (r) return r;
    }
    return t[t.length - 1];
  }
  return t[0];
}, W = ({
  problems: t,
  navigationTrackingParams: o,
  forms: e,
  externalBackClick: r,
  onSubmit: s,
  triggerValidation: d
}) => {
  const i = _(), [f, n] = p(!1), [a, c] = p(e[0]), h = (l) => l.formId === x.formId, m = ((l) => e.findIndex((u) => u.formId === l))(a.formId), b = e.length, v = m === b - 1, D = m === 0, y = e[0]?.formId;
  i.updateSharedEventProperties({ page: a?.formName });
  const [V, T] = p(e);
  V !== e && (c(
    (l) => e.find(({ formId: u }) => u === l.formId) ?? l
  ), T(e));
  const [E, N] = p(void 0);
  E !== y && (c(j(e, t?.remediationActions)), N(y));
  const k = (l, u) => {
    c(u?.[l] ?? e[l]);
  }, I = () => {
    g({
      userEvents: i,
      actionType: "submit",
      label: "submit",
      additionalTrackingParams: o
    });
  };
  return {
    handleBackClick: D ? r : () => {
      if (m > 0) {
        const l = e[m - 1];
        c(l), g({
          userEvents: i,
          actionType: "back",
          toForm: l?.formName,
          label: "back",
          additionalTrackingParams: o
        });
      }
    },
    handleNextClick: async (l) => {
      const u = l ?? e;
      if (h(a)) {
        I(), s();
        return;
      }
      const O = await d?.(a.formId) ?? !0;
      if (!a.isValid || !O) {
        n(!0), g({
          userEvents: i,
          actionType: "next",
          label: "next",
          returnValue: "validation error",
          additionalTrackingParams: o
        });
        return;
      }
      if (v) {
        I(), s();
        return;
      }
      n(!1);
      const S = m + 1;
      k(S, u), g({
        userEvents: i,
        actionType: "next",
        toForm: u[S]?.formName,
        label: "next",
        returnValue: "success",
        additionalTrackingParams: o
      });
    },
    gotoFormByFormIndex: k,
    activeForm: a,
    shouldValidate: f,
    setShouldValidate: n,
    steps: { current: m, total: b }
  };
}, L = B();
function Y({ children: t, forms: o, setFormIndex: e }) {
  const r = R({}), s = F((n, a) => {
    r.current[n] = a;
  }, []), d = F((n) => {
    delete r.current[n];
  }, []), i = F((n) => r.current[n], []), f = A(
    () => ({
      setFormIndex: e,
      goToFormByFieldName: (n) => {
        n === "idDocument" && (n = "idVerificationMethod");
        const a = o.findIndex(
          (c) => c?.fields?.some((h) => h === n)
        );
        a > -1 ? e(a) : L.error("No form was found to have that field so form navigation failed.");
      },
      registerFieldAction: s,
      unregisterFieldAction: d,
      getFieldAction: i
    }),
    [o, e, s, d, i]
  );
  return /* @__PURE__ */ w(q.Provider, { value: f, children: t });
}
const Z = (t, o, e, r) => {
  const s = [], d = Object.values(t).filter(({ formId: i }) => {
    if (o || e) {
      const f = !!o?.[i]?.length, n = !!e?.[i]?.length, a = f || n, c = [];
      return f && c.push("has required fields"), n && c.push("has optional fields"), a ? s.push(`Form '${i}': INCLUDED (${c.join(" & ")})`) : s.push(`Form '${i}': EXCLUDED (no required or optional fields)`), a;
    }
    return s.push(`Form '${i}': INCLUDED (no required/optional fields check)`), !0;
  });
  return C.value = {
    ...C.value,
    __FORMS__: {
      status: "INFO",
      reasons: s
    }
  }, [...d, x];
}, ee = (t, o, e) => t.map(({ formId: r, formName: s, fields: d }) => ({
  formId: r,
  formName: s,
  fields: d,
  isValid: o?.[r] ?? !1,
  hasServerValidationErrors: !!e?.validationErrors?.[r]
}));
export {
  Y as F,
  ee as a,
  Z as g,
  W as u
};
