import { useRef as m, useCallback as b, useSyncExternalStore as g } from "react";
import { useStepper as y } from "../../../../../hooks/useStepper/index.js";
import { useFormContext as V } from "react-hook-form";
import { deepShallow as v } from "../../../../../../../utils/deepShallow.js";
import { evaluateVisibilityStepCondition as a } from "../../../../../helpers/evaluateVisibilityStepCondition/index.js";
function C(c, t, o, r, i) {
  const s = t.find((e) => e.key === c);
  if (!s || !a(
    s,
    r || {},
    i || {}
  ))
    return !1;
  const u = t[o];
  if (!u)
    return !1;
  if (a(u, r || {}, i || {}))
    return c === u.key;
  const l = [];
  for (let e = 0; e <= o; e++) {
    const n = t[e];
    a(n, r || {}, i || {}) && l.push(n);
  }
  if (l.length === 0) {
    for (let e = o + 1; e < t.length; e++) {
      const n = t[e];
      if (a(n, r || {}, i || {}))
        return c === n.key;
    }
    return !1;
  }
  const p = l[l.length - 1];
  return c === p.key;
}
function F(c) {
  const { stepKey: t } = c, o = V(), { currentStep: r, steps: i, visibilityData: s } = y((e) => ({
    currentStep: e.currentStep,
    steps: e.steps,
    visibilityData: e.visibilityData
  }), v), f = m(null), u = m(/* @__PURE__ */ new Set()), l = b((e) => {
    u.current.add(e);
    const { unsubscribe: n } = o.watch((d) => {
      const S = C(
        t,
        i,
        r,
        d,
        s
      );
      S !== f.current && (f.current = S, u.current.forEach((h) => h()));
    });
    return () => {
      u.current.delete(e), n();
    };
  }, [o, t, i, r, s]), p = b(() => {
    const e = o.getValues(), n = C(
      t,
      i,
      r,
      e,
      s
    );
    return f.current === null && (f.current = n), n;
  }, [o, t, i, r, s]);
  return g(l, p, p);
}
export {
  F as useIsStepVisible
};
