import {
  Option
} from "./chunk-WNWHIFKH.js";
import {
  isOptionGroup
} from "./chunk-2JLZNSLY.js";

// src/components/Combobox/Listbox.tsx
import classNames from "classnames/dedupe";
import { jsx, jsxs } from "react/jsx-runtime";
var getOptionKey = (option, index, groupIndex) => {
  let key;
  if (typeof option === "object" && option.id) key = option.id;
  else if (typeof option === "object") key = `${option.value}::${option.label}`;
  else if (typeof option === "string") key = option;
  else key = String(index);
  return groupIndex !== void 0 ? `${groupIndex}:${key}` : key;
};
var Listbox = ({
  id,
  isOpen = false,
  options,
  highlightedIndex,
  highlightedGroupIndex,
  onOptionSelect,
  optionComponent,
  optionTestIdPrefix
}) => {
  const classes = classNames("mobius-combobox__list", {
    "mobius-combobox__list--hidden": !isOpen
  });
  function getOptionId(option, groupIndex, index) {
    if (typeof option === "object" && "id" in option && typeof option.id === "string") {
      return option.id;
    }
    return isOptionGroup(options) ? `${id}-option-${groupIndex}-${index}` : `${id}-option-${index}`;
  }
  if (!isOpen) {
    return /* @__PURE__ */ jsx("div", { role: "listbox", id, className: classes });
  }
  if (options && options.length === 0) {
    return /* @__PURE__ */ jsx("div", { role: "listbox", id, className: classes, children: /* @__PURE__ */ jsx("div", { className: "mobius-combobox__no-options", children: "No options" }) });
  }
  return /* @__PURE__ */ jsx("div", { role: "listbox", id, className: classes, children: isOptionGroup(options) ? options.map((option, groupIndex) => /* @__PURE__ */ jsxs(
    "ul",
    {
      role: "group",
      "aria-labelledby": `${id}-group-${groupIndex}`,
      className: "mobius-combobox__group",
      children: [
        /* @__PURE__ */ jsx(
          "li",
          {
            role: "presentation",
            id: `${id}-group-${groupIndex}`,
            className: "mobius-combobox__group-label",
            children: option.heading
          }
        ),
        option.options.map((groupOption, index) => /* @__PURE__ */ jsx(
          Option,
          {
            option: groupOption,
            isHighlighted: highlightedIndex === index && highlightedGroupIndex === groupIndex,
            onOptionSelect,
            optionComponent,
            optionTestIdPrefix,
            id: getOptionId(groupOption, groupIndex, index)
          },
          getOptionKey(groupOption, index, groupIndex)
        ))
      ]
    },
    option.heading
  )) : typeof options !== "undefined" ? options.map((option, index) => /* @__PURE__ */ jsx(
    Option,
    {
      option,
      isHighlighted: highlightedIndex === index,
      onOptionSelect,
      optionComponent,
      optionTestIdPrefix,
      id: getOptionId(option, 0, index)
    },
    getOptionKey(option, index)
  )) : null });
};

export {
  Listbox
};
//# sourceMappingURL=chunk-PQSPISME.js.map
