"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/Combobox/useComboboxHighlight.tsx
var useComboboxHighlight_exports = {};
__export(useComboboxHighlight_exports, {
  useComboboxHighlight: () => useComboboxHighlight
});
module.exports = __toCommonJS(useComboboxHighlight_exports);
var import_react = require("react");

// 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;
}

// src/components/Combobox/useComboboxHighlight.tsx
function useComboboxHighlight(options) {
  const [highlightedIndex, setHighlightedIndex] = (0, import_react.useState)(
    options && options.length ? 0 : -1
  );
  const [highlightedGroupIndex, setHighlightedGroupIndex] = (0, import_react.useState)(0);
  function highlightNextOption() {
    const isGroup = isOptionGroup(options);
    if (!options) {
      return;
    }
    if (isGroup) {
      const group = options[highlightedGroupIndex];
      if (highlightedIndex === group.options.length - 1) {
        if (highlightedGroupIndex === options.length - 1) {
          return;
        }
        setHighlightedIndex(0);
        setHighlightedGroupIndex(highlightedGroupIndex + 1);
      } else {
        setHighlightedIndex(highlightedIndex + 1);
      }
    } else {
      if (highlightedIndex === options.length - 1) {
        return;
      }
      setHighlightedIndex(highlightedIndex + 1);
    }
  }
  function highlightPreviousOption() {
    const isGroup = isOptionGroup(options);
    if (highlightedIndex === 0 && highlightedGroupIndex === 0) {
      return;
    }
    if (isGroup) {
      if (highlightedIndex === 0) {
        const prevGroupIndex = highlightedGroupIndex - 1;
        const prevGroup = options[prevGroupIndex];
        setHighlightedGroupIndex(prevGroupIndex);
        setHighlightedIndex(prevGroup.options.length - 1);
      } else {
        setHighlightedIndex(highlightedIndex - 1);
      }
    } else {
      setHighlightedIndex(highlightedIndex - 1);
    }
  }
  function highlightFirstOption() {
    setHighlightedIndex(0);
    setHighlightedGroupIndex(0);
  }
  function highlightLastOption() {
    const isGroup = isOptionGroup(options);
    if (!options) {
      return;
    }
    if (isGroup) {
      const lastGroupIndex = options.length - 1;
      const lastGroup = options[lastGroupIndex];
      setHighlightedGroupIndex(lastGroupIndex);
      setHighlightedIndex(lastGroup.options.length - 1);
    } else {
      setHighlightedIndex(options.length - 1);
    }
  }
  const clearHighlight = () => {
    setHighlightedIndex(
      typeof options === "undefined" || options.length ? 0 : -1
    );
    setHighlightedGroupIndex(0);
  };
  return {
    highlightedIndex,
    highlightedGroupIndex,
    highlightPreviousOption,
    highlightNextOption,
    highlightFirstOption,
    highlightLastOption,
    clearHighlight
  };
}
//# sourceMappingURL=useComboboxHighlight.js.map
