import { useRef as v, useState as T, createRef as X, useCallback as u, useMemo as b } from "react";
function $(Y, L = 0, t) {
  var V;
  const g = v(Y), [S, B] = T(L), [R, i] = T((t == null ? void 0 : t.initialData) ?? {}), [k, H] = T({}), C = v(/* @__PURE__ */ new Map()), Z = v(X()), I = v(R);
  I.current = R;
  const f = v(!1), d = u((e) => Math.max(0, Math.min(e, Math.max(g.current.length - 1, 0))), []), h = u(
    (e) => {
      var r;
      return (r = g.current[e]) == null ? void 0 : r.id;
    },
    []
  ), x = u((e) => g.current.findIndex((r) => r.id === e), []), y = u((e, r) => {
  }, []);
  v(!1);
  const m = Math.max(0, Math.min(S, g.current.length - 1)), N = m === 0, j = g.current.length === 0 || m === g.current.length - 1, F = (V = g.current[m]) == null ? void 0 : V.id, O = u(
    (e) => {
      if (x(e) === -1)
        return y("getStepRef", e), Z.current;
      const r = C.current.get(e);
      if (r) return r;
      const n = X();
      return C.current.set(e, n), n;
    },
    [x, y]
  ), P = u(
    (e, r) => {
      if (x(e) === -1) {
        y("setStepStatus", e);
        return;
      }
      H((n) => ({ ...n, [e]: r }));
    },
    [x, y]
  ), U = u(
    (e, r) => {
      var c;
      const n = { ...I.current, [e]: r };
      i(n), (c = t == null ? void 0 : t.onDataChange) == null || c.call(t, n);
    },
    [t]
  ), q = u(
    (e) => I.current[e],
    []
  ), z = u(() => ({ ...I.current }), []), D = u(async () => {
    var e, r, n;
    if (!f.current) {
      f.current = !0;
      try {
        const c = d(S), a = h(c), s = a ? C.current.get(a) : void 0;
        if ((e = s == null ? void 0 : s.current) != null && e.onBeforeLeave && await s.current.onBeforeLeave({ type: "next" }) === !1)
          return;
        const l = g.current[c];
        if ((l == null ? void 0 : l.nextStep) !== void 0) {
          const W = typeof l.nextStep == "function" ? l.nextStep(I.current) : l.nextStep, A = x(W);
          if (A !== -1) {
            B(A), (r = t == null ? void 0 : t.onStepChange) == null || r.call(t, A);
            return;
          }
        }
        const w = d(S + 1);
        B(w), (n = t == null ? void 0 : t.onStepChange) == null || n.call(t, w);
      } finally {
        f.current = !1;
      }
    }
  }, [d, S, h, x, t]), M = u(async () => {
    var e, r;
    if (!f.current) {
      f.current = !0;
      try {
        const n = d(S), c = h(n), a = c ? C.current.get(c) : void 0;
        if ((e = a == null ? void 0 : a.current) != null && e.onBeforeLeave && await a.current.onBeforeLeave({ type: "back" }) === !1)
          return;
        const s = d(S - 1);
        B(s), (r = t == null ? void 0 : t.onStepChange) == null || r.call(t, s);
      } finally {
        f.current = !1;
      }
    }
  }, [d, S, h, t]), E = u(
    async (e) => {
      var r, n;
      if (!f.current) {
        f.current = !0;
        try {
          const c = x(e);
          if (c === -1) {
            y("goTo", e);
            return;
          }
          const a = d(S), s = h(a), l = s ? C.current.get(s) : void 0;
          if ((r = l == null ? void 0 : l.current) != null && r.onBeforeLeave && await l.current.onBeforeLeave({ type: "goto", target: e }) === !1)
            return;
          B(c), (n = t == null ? void 0 : t.onStepChange) == null || n.call(t, c);
        } finally {
          f.current = !1;
        }
      }
    },
    [x, t, y, d, S, h]
  ), G = u(() => {
    var e;
    B(L), i((t == null ? void 0 : t.initialData) ?? {}), H({}), (e = t == null ? void 0 : t.onStepChange) == null || e.call(t, L);
  }, [L, t]), J = b(
    () => g.current.map((e) => ({
      id: e.id,
      title: e.title,
      description: e.description,
      completedTitle: e.completedTitle,
      completedBadgeLabel: e.completedBadgeLabel,
      completedBadgeIcon: e.completedBadgeIcon,
      contentHeader: e.contentHeader,
      backAction: e.backAction,
      nextAction: e.nextAction,
      status: k[e.id] ?? e.status
    })),
    [k]
  ), K = b(() => {
    const e = g.current, r = e.map(
      (c, a) => ({
        initialData: c,
        storedData: R[c.id],
        index: a,
        status: k[c.id] ?? c.status
      })
    ), n = e.reduce(
      (c, a, s) => (c[a.id] = r[s], c),
      {}
    );
    return Object.assign([...r], n);
  }, [R, k]), Q = b(
    () => ({
      steps: J,
      activeStep: m,
      onNext: D,
      onBack: M
    }),
    [J, m, D, M]
  );
  return b(
    () => ({
      layoutProps: Q,
      activeStep: m,
      activeStepId: F,
      isFirst: N,
      isLast: j,
      steps: K,
      goNext: D,
      goBack: M,
      goTo: E,
      reset: G,
      setStepStatus: P,
      setStepData: U,
      getStepData: q,
      getAllData: z,
      getStepRef: O
    }),
    [
      m,
      F,
      N,
      j,
      K,
      D,
      M,
      E,
      G,
      P,
      U,
      q,
      z,
      O,
      Q
    ]
  );
}
export {
  $ as default
};
//# sourceMappingURL=useStepper.js.map
