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