import { useRef as p, useCallback as m, useSyncExternalStore as v, useMemo as h } from "react";
import { useFormContext as w } from "react-hook-form";
import { useStepper as x } from "../../../hooks/useStepper/index.js";
import { evaluateVisibilityStepCondition as C } from "../../../helpers/evaluateVisibilityStepCondition/index.js";
import { deepShallow as K } from "../../../../../utils/deepShallow.js";
function b(e, t, n) {
  return e.filter(
    (r) => C(r, t || {}, n || {})
  );
}
function y(e) {
  return e.map((t) => t.key).join(",");
}
function k() {
  const e = w(), {
    steps: t,
    visibilityData: n
  } = x(
    (s) => ({
      steps: s.steps,
      visibilityData: s.visibilityData
    }),
    K
  ), r = p(null), u = p([]), c = p(/* @__PURE__ */ new Set()), V = m((s) => {
    c.current.add(s);
    const { unsubscribe: i } = e.watch((o) => {
      const f = b(
        t,
        o,
        n
      ), S = y(f);
      S !== r.current && (r.current = S, u.current = f, c.current.forEach((d) => d()));
    });
    return () => {
      c.current.delete(s), i();
    };
  }, [e, t, n]), l = m(() => {
    const s = e.getValues(), i = b(
      t,
      s,
      n
    ), o = y(i);
    return r.current === null && (r.current = o, u.current = i), o === r.current ? u.current : (r.current = o, u.current = i, i);
  }, [e, t, n]), a = v(
    V,
    l,
    l
  );
  return h(() => ({
    visibleSteps: a
  }), [a]);
}
export {
  k as useVisibileSteps
};
