"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/Combobox/Listbox.tsx
var Listbox_exports = {};
__export(Listbox_exports, {
  Listbox: () => Listbox
});
module.exports = __toCommonJS(Listbox_exports);
var import_dedupe2 = __toESM(require("classnames/dedupe"));

// src/components/Combobox/Option.tsx
var import_react = require("react");
var import_dedupe = __toESM(require("classnames/dedupe"));

// src/components/Combobox/utils.tsx
function isOptionGroup(options) {
  if (!options) return false;
  return typeof options[0] === "object" && "options" in options[0] && options[0].options !== void 0 && "heading" in options[0] && options[0].heading !== void 0;
}
var getOptionValue = (option) => typeof option === "string" ? option : option?.value;
var getOptionLabel = (option) => typeof option === "string" ? option : option?.label;
var buildOptionTestId = (prefix, value) => `${prefix}-${value.trim().toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "")}`;

// src/components/Combobox/Option.tsx
var import_jsx_runtime = require("react/jsx-runtime");
var Option = ({
  option,
  isHighlighted,
  onOptionSelect,
  optionComponent: OptionComponent,
  optionTestIdPrefix,
  id
}) => {
  const optionRef = (0, import_react.useRef)(null);
  const optionValue = getOptionValue(option) || "";
  const testId = buildOptionTestId(
    optionTestIdPrefix || "combobox-option",
    optionValue
  );
  (0, import_react.useEffect)(() => {
    if (isHighlighted && optionRef.current && optionRef.current.scrollIntoView) {
      optionRef.current.scrollIntoView({ block: "nearest" });
    }
  }, [isHighlighted]);
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
    "li",
    {
      ref: optionRef,
      role: "option",
      "data-testid": testId,
      id,
      "aria-selected": isHighlighted,
      onMouseDown: () => onOptionSelect(option),
      className: (0, import_dedupe.default)("mobius-combobox__option", "has-pii", {
        "mobius-combobox__option--is-highlighted": isHighlighted
      }),
      children: OptionComponent ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(OptionComponent, { option, isHighlighted }) : getOptionLabel(option)
    },
    getOptionValue(option)
  );
};

// src/components/Combobox/Listbox.tsx
var import_jsx_runtime2 = require("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 = (0, import_dedupe2.default)("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__ */ (0, import_jsx_runtime2.jsx)("div", { role: "listbox", id, className: classes });
  }
  if (options && options.length === 0) {
    return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { role: "listbox", id, className: classes, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { className: "mobius-combobox__no-options", children: "No options" }) });
  }
  return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { role: "listbox", id, className: classes, children: isOptionGroup(options) ? options.map((option, groupIndex) => /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
    "ul",
    {
      role: "group",
      "aria-labelledby": `${id}-group-${groupIndex}`,
      className: "mobius-combobox__group",
      children: [
        /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
          "li",
          {
            role: "presentation",
            id: `${id}-group-${groupIndex}`,
            className: "mobius-combobox__group-label",
            children: option.heading
          }
        ),
        option.options.map((groupOption, index) => /* @__PURE__ */ (0, import_jsx_runtime2.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__ */ (0, import_jsx_runtime2.jsx)(
    Option,
    {
      option,
      isHighlighted: highlightedIndex === index,
      onOptionSelect,
      optionComponent,
      optionTestIdPrefix,
      id: getOptionId(option, 0, index)
    },
    getOptionKey(option, index)
  )) : null });
};
//# sourceMappingURL=Listbox.js.map
