import * as n from "react";
import { useId as R } from "../../../../../../../@base-ui_utils@0.2.4_@types_react@18.3.27_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/@base-ui/utils/esm/useId.js";
import { useFloatingParentNodeId as S } from "../components/FloatingTree.js";
import { EMPTY_OBJECT as F } from "../../../../../../../@base-ui_utils@0.2.4_@types_react@18.3.27_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/@base-ui/utils/esm/empty.js";
import { getFloatingFocusElement as y } from "../utils/element.js";
const T = /* @__PURE__ */ new Map([["select", "listbox"], ["combobox", "listbox"], ["label", !1]]);
function k(c, x = {}) {
  const a = "rootStore" in c ? c.rootStore : c, l = a.useState("open"), d = a.useState("floatingId"), u = a.useState("domReferenceElement"), m = a.useState("floatingElement"), {
    enabled: p = !0,
    role: o = "dialog"
  } = x, M = R(), s = (u == null ? void 0 : u.id) || M, r = n.useMemo(() => {
    var t;
    return ((t = y(m)) == null ? void 0 : t.id) || d;
  }, [m, d]), e = T.get(o) ?? o, f = S() != null, i = n.useMemo(() => e === "tooltip" || o === "label" ? F : {
    "aria-haspopup": e === "alertdialog" ? "dialog" : e,
    "aria-expanded": "false",
    ...e === "listbox" && {
      role: "combobox"
    },
    ...e === "menu" && f && {
      role: "menuitem"
    },
    ...o === "select" && {
      "aria-autocomplete": "none"
    },
    ...o === "combobox" && {
      "aria-autocomplete": "list"
    }
  }, [e, f, o]), b = n.useMemo(() => e === "tooltip" || o === "label" ? {
    [`aria-${o === "label" ? "labelledby" : "describedby"}`]: l ? r : void 0
  } : {
    ...i,
    "aria-expanded": l ? "true" : "false",
    "aria-controls": l ? r : void 0,
    ...e === "menu" && {
      id: s
    }
  }, [e, r, l, s, o, i]), g = n.useMemo(() => {
    const t = {
      id: r,
      ...e && {
        role: e
      }
    };
    return e === "tooltip" || o === "label" ? t : {
      ...t,
      ...e === "menu" && {
        "aria-labelledby": s
      }
    };
  }, [e, r, s, o]), I = n.useCallback(({
    active: t,
    selected: E
  }) => {
    const P = {
      role: "option",
      ...t && {
        id: `${r}-fui-option`
      }
    };
    switch (o) {
      case "select":
      case "combobox":
        return {
          ...P,
          "aria-selected": E
        };
    }
    return {};
  }, [r, o]);
  return n.useMemo(() => p ? {
    reference: b,
    floating: g,
    item: I,
    trigger: i
  } : {}, [p, b, g, i, I]);
}
export {
  k as useRole
};
