import $, { forwardRef as P, useCallback as w, useRef as k, Fragment as j, isValidElement as E, cloneElement as A, createElement as F } from "react";
import { classNames as O } from "./class-names.js";
import { match as S } from "./match.js";
var C = ((e) => (e[e.None = 0] = "None", e[e.RenderStrategy = 1] = "RenderStrategy", e[e.Static = 2] = "Static", e))(C || {}), H = ((e) => (e[e.Unmount = 0] = "Unmount", e[e.Hidden = 1] = "Hidden", e))(H || {});
function x() {
  let e = U();
  return w((n) => T({ mergeRefs: e, ...n }), [e]);
}
function T({ ourProps: e, theirProps: n, slot: t, defaultTag: r, features: l, visible: a = !0, name: s, mergeRefs: u }) {
  u = u ?? W;
  let o = R(n, e);
  if (a) return v(o, t, r, s, u);
  let p = l ?? 0;
  if (p & 2) {
    let { static: i = !1, ...f } = o;
    if (i) return v(f, t, r, s, u);
  }
  if (p & 1) {
    let { unmount: i = !0, ...f } = o;
    return S(i ? 0 : 1, { 0() {
      return null;
    }, 1() {
      return v({ ...f, hidden: !0, style: { display: "none" } }, t, r, s, u);
    } });
  }
  return v(o, t, r, s, u);
}
function v(e, n = {}, t, r, l) {
  let { as: a = t, children: s, refName: u = "ref", ...o } = N(e, ["unmount", "static"]), p = e.ref !== void 0 ? { [u]: e.ref } : {}, i = typeof s == "function" ? s(n) : s;
  "className" in o && o.className && typeof o.className == "function" && (o.className = o.className(n)), o["aria-labelledby"] && o["aria-labelledby"] === o.id && (o["aria-labelledby"] = void 0);
  let f = {};
  if (n) {
    let c = !1, d = [];
    for (let [m, h] of Object.entries(n)) typeof h == "boolean" && (c = !0), h === !0 && d.push(m.replace(/([A-Z])/g, (y) => `-${y.toLowerCase()}`));
    if (c) {
      f["data-headlessui-state"] = d.join(" ");
      for (let m of d) f[`data-${m}`] = "";
    }
  }
  if (a === j && (Object.keys(g(o)).length > 0 || Object.keys(g(f)).length > 0)) if (!E(i) || Array.isArray(i) && i.length > 1) {
    if (Object.keys(g(o)).length > 0) throw new Error(['Passing props on "Fragment"!', "", `The current component <${r} /> is rendering a "Fragment".`, "However we need to passthrough the following props:", Object.keys(g(o)).concat(Object.keys(g(f))).map((c) => `  - ${c}`).join(`
`), "", "You can apply a few solutions:", ['Add an `as="..."` prop, to ensure that we render an actual element instead of a "Fragment".', "Render a single element as the child so that we can forward the props onto that element."].map((c) => `  - ${c}`).join(`
`)].join(`
`));
  } else {
    let c = i.props, d = c == null ? void 0 : c.className, m = typeof d == "function" ? (...b) => O(d(...b), o.className) : O(d, o.className), h = m ? { className: m } : {}, y = R(i.props, g(N(o, ["ref"])));
    for (let b in f) b in y && delete f[b];
    return A(i, Object.assign({}, y, f, p, { ref: l(_(i), p.ref) }, h));
  }
  return F(a, Object.assign({}, N(o, ["ref"]), a !== j && p, a !== j && f), i);
}
function U() {
  let e = k([]), n = w((t) => {
    for (let r of e.current) r != null && (typeof r == "function" ? r(t) : r.current = t);
  }, []);
  return (...t) => {
    if (!t.every((r) => r == null)) return e.current = t, n;
  };
}
function W(...e) {
  return e.every((n) => n == null) ? void 0 : (n) => {
    for (let t of e) t != null && (typeof t == "function" ? t(n) : t.current = n);
  };
}
function R(...e) {
  if (e.length === 0) return {};
  if (e.length === 1) return e[0];
  let n = {}, t = {};
  for (let r of e) for (let l in r) l.startsWith("on") && typeof r[l] == "function" ? (t[l] != null || (t[l] = []), t[l].push(r[l])) : n[l] = r[l];
  if (n.disabled || n["aria-disabled"]) for (let r in t) /^(on(?:Click|Pointer|Mouse|Key)(?:Down|Up|Press)?)$/.test(r) && (t[r] = [(l) => {
    var a;
    return (a = l == null ? void 0 : l.preventDefault) == null ? void 0 : a.call(l);
  }]);
  for (let r in t) Object.assign(n, { [r](l, ...a) {
    let s = t[r];
    for (let u of s) {
      if ((l instanceof Event || (l == null ? void 0 : l.nativeEvent) instanceof Event) && l.defaultPrevented) return;
      u(l, ...a);
    }
  } });
  return n;
}
function M(...e) {
  if (e.length === 0) return {};
  if (e.length === 1) return e[0];
  let n = {}, t = {};
  for (let r of e) for (let l in r) l.startsWith("on") && typeof r[l] == "function" ? (t[l] != null || (t[l] = []), t[l].push(r[l])) : n[l] = r[l];
  for (let r in t) Object.assign(n, { [r](...l) {
    let a = t[r];
    for (let s of a) s == null || s(...l);
  } });
  return n;
}
function V(e) {
  var n;
  return Object.assign(P(e), { displayName: (n = e.displayName) != null ? n : e.name });
}
function g(e) {
  let n = Object.assign({}, e);
  for (let t in n) n[t] === void 0 && delete n[t];
  return n;
}
function N(e, n = []) {
  let t = Object.assign({}, e);
  for (let r of n) r in t && delete t[r];
  return t;
}
function _(e) {
  return $.version.split(".")[0] >= "19" ? e.props.ref : e.ref;
}
export {
  C as RenderFeatures,
  H as RenderStrategy,
  g as compact,
  V as forwardRefWithAs,
  M as mergeProps,
  x as useRender
};
