import $, { useContext as E, createContext as I, useState as F, useMemo as h, useId as M } from "react";
import { useEvent as L } from "../../hooks/use-event.js";
import { useIsoMorphicEffect as P } from "../../hooks/use-iso-morphic-effect.js";
import { useSyncRefs as S } from "../../hooks/use-sync-refs.js";
import { useDisabled as w } from "../../internal/disabled.js";
import { useProvidedId as B } from "../../internal/id.js";
import { forwardRefWithAs as R, useRender as j } from "../../utils/render.js";
let m = I(null);
m.displayName = "LabelContext";
function x() {
  let r = E(m);
  if (r === null) {
    let o = new Error("You used a <Label /> component, but it is not inside a relevant parent.");
    throw Error.captureStackTrace && Error.captureStackTrace(o, x), o;
  }
  return r;
}
function A(r) {
  var o, n, a;
  let e = (n = (o = E(m)) == null ? void 0 : o.value) != null ? n : void 0;
  return ((a = r == null ? void 0 : r.length) != null ? a : 0) > 0 ? [e, ...r].filter(Boolean).join(" ") : e;
}
function Y({ inherit: r = !1 } = {}) {
  let o = A(), [n, a] = F([]), e = r ? [o, ...n].filter(Boolean) : n;
  return [e.length > 0 ? e.join(" ") : void 0, h(() => function(t) {
    let p = L((u) => (a((c) => [...c, u]), () => a((c) => {
      let i = c.slice(), d = i.indexOf(u);
      return d !== -1 && i.splice(d, 1), i;
    }))), s = h(() => ({ register: p, slot: t.slot, name: t.name, props: t.props, value: t.value }), [p, t.slot, t.name, t.props, t.value]);
    return $.createElement(m.Provider, { value: s }, t.children);
  }, [a])];
}
let H = "label";
function D(r, o) {
  var n;
  let a = M(), e = x(), t = B(), p = w(), { id: s = `headlessui-label-${a}`, htmlFor: u = t ?? ((n = e.props) == null ? void 0 : n.htmlFor), passive: c = !1, ...i } = r, d = S(o);
  P(() => e.register(s), [s, e.register]);
  let y = L((b) => {
    let v = b.currentTarget;
    if (v instanceof HTMLLabelElement && b.preventDefault(), e.props && "onClick" in e.props && typeof e.props.onClick == "function" && e.props.onClick(b), v instanceof HTMLLabelElement) {
      let l = document.getElementById(v.htmlFor);
      if (l) {
        let C = l.getAttribute("disabled");
        if (C === "true" || C === "") return;
        let g = l.getAttribute("aria-disabled");
        if (g === "true" || g === "") return;
        (l instanceof HTMLInputElement && (l.type === "radio" || l.type === "checkbox") || l.role === "radio" || l.role === "checkbox" || l.role === "switch") && l.click(), l.focus({ preventScroll: !0 });
      }
    }
  }), k = p || !1, T = h(() => ({ ...e.slot, disabled: k }), [e.slot, k]), f = { ref: d, ...e.props, id: s, htmlFor: u, onClick: y };
  return c && ("onClick" in f && (delete f.htmlFor, delete f.onClick), "onClick" in i && delete i.onClick), j()({ ourProps: f, theirProps: i, slot: T, defaultTag: u ? H : "div", name: e.name || "Label" });
}
let K = R(D), q = Object.assign(K, {});
export {
  q as Label,
  x as useLabelContext,
  A as useLabelledBy,
  Y as useLabels
};
