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