import { useState as v, useRef as H, useEffect as y } from "react";
import { IconNames as C } from "./index-IconNames.js";
const j = (d, u, I, O, B = !0) => {
  const [i, E] = v([]), N = H(0), [x, V] = v([]), [F, k] = v(null), b = H({}), [o, R] = v(null), L = (t) => {
    const e = t, s = l(i ?? [], e.name) ?? -1, f = l(x ?? [], e.name) ?? -1, n = i == null ? void 0 : i.filter((r) => !r.isHidden), a = b.current[e.name], m = (a == null ? void 0 : a.positive) !== void 0, S = (a == null ? void 0 : a.negative) !== void 0;
    return typeof e.autoNext > "u" && (e.autoNext = !0), typeof e.autoBack > "u" && (e.autoBack = !0), e.footerOptions || (e.footerOptions = {}), m && (e.autoNext = !1), S && (e.autoBack = !1), e.footerOptions.positive === void 0 && (e.footerOptions.positive = {
      disabled: e.nextDisabled ?? !1,
      isLoading: e.nextLoading ?? !1,
      autogen: !0,
      text: f !== (n == null ? void 0 : n.length) ? "Next" : "Finish",
      icon: f !== (n == null ? void 0 : n.length) ? C.arrowRight : void 0,
      cta: () => {
      }
    }), e.footerOptions.negative === void 0 && (e.footerOptions.negative = {
      autogen: !0,
      disabled: e.previousDisabled ?? !1,
      isLoading: e.previousLoading ?? !1,
      icon: f === 1 ? C.caretRight : void 0,
      text: f === 1 ? "Cancel" : "Back",
      cta: () => {
      }
    }), e.autoNext && e.footerOptions.positive && (e.footerOptions.positive.cta = () => {
      let r = -1;
      if (e.isHidden && i)
        for (let c = s - 1; c <= (i == null ? void 0 : i.length); c++)
          i[c].isHidden || r === -1 && (r = c + 1);
      else
        n && f + 1 <= n.length && p(f + 1, !1);
      p(r);
    }), e.autoBack && e.footerOptions.negative && (e.footerOptions.negative.cta = () => {
      let r = -1;
      if (e.isHidden && i)
        for (let c = s - 1; c >= 0; c--)
          i[c].isHidden || r === -1 && (r = c + 1);
      else
        n && f - 1 >= 0 && p(f - 1, !1);
      p(r);
    }), e;
  }, l = (t, e) => {
    const s = t.find((f) => f.name === e);
    return s ? t.indexOf(s) + 1 : -1;
  }, A = (t, e = !1) => {
    if (!i)
      return;
    let s = i;
    e || (s = i.filter((a) => !a.isHidden));
    let n = [...s][t - 1];
    n && (n = L(n), R(n), k(t));
  }, p = (t, e = !1) => {
    A(t, e);
  }, w = (t, e = !1) => {
    if (i && i.length > 0) {
      const s = l(i, t);
      if (s > -1)
        A(s, e);
      else
        throw new Error(`Step ${t} does not exist!`);
    }
  }, h = (t, e = !1) => {
    typeof t == "string" && w(t, e), typeof t == "number" && p(t, e);
  }, T = (t) => {
    t && (u ? h(u, !0) : h(1, !1));
  }, D = (t) => {
    if (!t) return;
    const e = t.filter((s) => !s.isHidden);
    V(e);
  }, G = (t) => {
    var s, f, n, a;
    if (!t) return;
    const e = {};
    for (const m of t)
      e[m.name] = {
        positive: (f = (s = m.footerOptions) == null ? void 0 : s.positive) == null ? void 0 : f.cta,
        negative: (a = (n = m.footerOptions) == null ? void 0 : n.negative) == null ? void 0 : a.cta
      };
    b.current = e;
  };
  y(() => {
    G(d), d && (D(d), E(d));
  }, [d]), y(() => {
    i && i.length > 0 && T(i);
  }, [i]), y(() => {
    u && h(u, !0);
  }, [u]);
  let g = 0;
  return o != null && o.isHidden ? g = N.current ?? x.length : i && o ? g = l(x, o == null ? void 0 : o.name) : g = 1, N.current = g, y(() => {
    if (I && o) {
      const t = l(i, o.name);
      t && (B && Array.from(document.getElementsByClassName("modal-content")).forEach((e) => {
        e == null || e.scroll(0, 0);
      }), I(t, o.name, o));
    }
    if (O && o) {
      const t = l(i, o.name);
      t && (B && Array.from(document.getElementsByClassName("modal-content")).forEach((e) => {
        e == null || e.scroll(0, 0);
      }), O(t, o.name, o));
    }
  }, [o == null ? void 0 : o.name]), {
    activeStepInStepper: g,
    innerVisibleSteps: x,
    currentStepOpts: o
  };
};
export {
  j as useStepper
};
