import n, { useState as p, useEffect as m, useContext as E, createContext as F } from "react";
import { createPortal as y } from "react-dom";
import { useDisposables as h } from "../hooks/use-disposables.js";
import { objectToFormEntries as H } from "../utils/form.js";
import { compact as b } from "../utils/render.js";
import { Hidden as d, HiddenFeatures as i } from "./hidden.js";
let g = F(null);
function v({ children: r }) {
  let e = E(g);
  if (!e) return n.createElement(n.Fragment, null, r);
  let { target: t } = e;
  return t ? y(n.createElement(n.Fragment, null, r), t) : null;
}
function k({ data: r, form: e, disabled: t, onReset: a, overrides: s }) {
  let [l, u] = p(null), f = h();
  return m(() => {
    if (a && l) return f.addEventListener(l, "reset", a);
  }, [l, e, a]), n.createElement(v, null, n.createElement(x, { setForm: u, formId: e }), H(r).map(([o, c]) => n.createElement(d, { features: i.Hidden, ...b({ key: o, as: "input", type: "hidden", hidden: !0, readOnly: !0, form: e, disabled: t, name: o, value: c, ...s }) })));
}
function x({ setForm: r, formId: e }) {
  return m(() => {
    if (e) {
      let t = document.getElementById(e);
      t && r(t);
    }
  }, [r, e]), e ? null : n.createElement(d, { features: i.Hidden, as: "input", type: "hidden", hidden: !0, readOnly: !0, ref: (t) => {
    if (!t) return;
    let a = t.closest("form");
    a && r(a);
  } });
}
export {
  k as FormFields,
  v as HoistFormFields
};
