import { o as R } from "./rolldown-runtime.js";
import { i as S } from "./vendor/react.js";
import { r as _ } from "./useRenderElement.js";
import { n as v } from "./useBaseUiId.js";
import { t as F } from "./element.js";
import { _ as P } from "./popupStateMapping.js";
var r = /* @__PURE__ */ R(S()), y = /* @__PURE__ */ new Map([
  ["select", "listbox"],
  ["combobox", "listbox"],
  ["label", !1]
]);
function k(c, g = {}) {
  const a = "rootStore" in c ? c.rootStore : c, l = a.useState("open"), m = a.useState("floatingId"), M = a.useState("domReferenceElement"), u = a.useState("floatingElement"), { role: o = "dialog" } = g, x = v(), n = M?.id || x, t = r.useMemo(() => F(u)?.id || m, [u, m]), e = y.get(o) ?? o, d = P() != null, i = r.useMemo(() => e === "tooltip" || o === "label" ? _ : {
    "aria-haspopup": e === "alertdialog" ? "dialog" : e,
    "aria-expanded": "false",
    ...e === "listbox" && { role: "combobox" },
    ...e === "menu" && d && { role: "menuitem" },
    ...o === "select" && { "aria-autocomplete": "none" },
    ...o === "combobox" && { "aria-autocomplete": "list" }
  }, [
    e,
    d,
    o
  ]), f = r.useMemo(() => e === "tooltip" || o === "label" ? { [`aria-${o === "label" ? "labelledby" : "describedby"}`]: l ? t : void 0 } : {
    ...i,
    "aria-expanded": l ? "true" : "false",
    "aria-controls": l ? t : void 0,
    ...e === "menu" && { id: n }
  }, [
    e,
    t,
    l,
    n,
    o,
    i
  ]), p = r.useMemo(() => {
    const s = {
      id: t,
      ...e && { role: e }
    };
    return e === "tooltip" || o === "label" ? s : {
      ...s,
      ...e === "menu" && { "aria-labelledby": n }
    };
  }, [
    e,
    t,
    n,
    o
  ]), b = r.useCallback(({ active: s, selected: E }) => {
    const I = {
      role: "option",
      ...s && { id: `${t}-fui-option` }
    };
    switch (o) {
      case "select":
      case "combobox":
        return {
          ...I,
          "aria-selected": E
        };
      default:
    }
    return {};
  }, [t, o]);
  return r.useMemo(() => ({
    reference: f,
    floating: p,
    item: b,
    trigger: i
  }), [
    f,
    p,
    i,
    b
  ]);
}
export {
  k as t
};

//# sourceMappingURL=useRole.js.map