import { useState as m, useEffect as u, useMemo as p } from "react";
import { useFormContext as F, useWatch as h } from "react-hook-form";
import { EMPTY_DATA as d } from "../../constants.js";
import { evaluateConditionalRule as b } from "../../helpers/evaluateConditionalRule/evaluateConditionalRule.js";
function C(t) {
  const s = t.fields ?? d.fields, n = t.conditionalRules ?? d.conditionalRules, { control: c } = F(), [a, r] = m(() => {
    const i = /* @__PURE__ */ new Set();
    return s.forEach((e) => {
      !e.isConditional && e.isVisible !== !1 && i.add(e.name);
    }), i;
  }), l = h({ control: c });
  return u(() => {
    const i = /* @__PURE__ */ new Set();
    s.forEach((e) => {
      !e.isConditional && e.isVisible !== !1 && i.add(e.name);
    }), n.filter((e) => e.isActive).forEach((e) => {
      const f = l[e.sourceFieldName], o = b(e, f);
      o && e.actionType === "show" ? i.add(e.targetFieldName) : !o && e.actionType === "show" || o && e.actionType === "hide" || !o && e.actionType === "hide" && i.add(e.targetFieldName);
    }), r(i);
  }, [l, n, s]), {
    visibleFields: p(() => s.filter((i) => a.has(i.name)).sort((i, e) => i.displayOrder - e.displayOrder), [s, a])
  };
}
export {
  C as useDynamicFormConditionals
};
