"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/useComboboxOptions.ts
var useComboboxOptions_exports = {};
__export(useComboboxOptions_exports, {
  useComboboxOptions: () => useComboboxOptions
});
module.exports = __toCommonJS(useComboboxOptions_exports);
var import_mobius_hooks = require("@simplybusiness/mobius-hooks");
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;
}
var getOptionLabel = (option) => typeof option === "string" ? option : option?.label;
function filterOptions(options, inputValue) {
  if (isOptionGroup(options)) {
    return options.map((optionGroup) => ({
      ...optionGroup,
      options: optionGroup.options.filter(
        (option) => getOptionLabel(option).toLowerCase().includes(inputValue.toLowerCase())
      )
    })).filter((optionGroup) => optionGroup.options.length > 0);
  }
  return options.filter(
    (option) => getOptionLabel(option).toLowerCase().includes(inputValue.toLowerCase())
  );
}

// src/components/Combobox/useComboboxOptions.ts
function useComboboxOptions({
  options,
  asyncOptions,
  delay = 300,
  minSearchLength = 3,
  inputValue = "",
  skipNextDebounceRef,
  onSearched
}) {
  const [filteredOptions, setFilteredOptions] = (0, import_react.useState)(void 0);
  const debouncedInputValue = (0, import_mobius_hooks.useDebouncedValue)(
    inputValue,
    // Don't debounce synchronous options
    options ? 0 : delay
  );
  const [isLoading, setIsLoading] = (0, import_react.useState)(false);
  const [error, setError] = (0, import_react.useState)(null);
  const asyncOptionsRef = (0, import_react.useRef)(asyncOptions);
  asyncOptionsRef.current = asyncOptions;
  const onSearchedRef = (0, import_react.useRef)(onSearched);
  onSearchedRef.current = onSearched;
  (0, import_react.useEffect)(() => {
    const controller = new AbortController();
    const { signal } = controller;
    const fetchOptions = async () => {
      setIsLoading(true);
      setError(null);
      try {
        if (asyncOptionsRef.current) {
          if (debouncedInputValue.length < minSearchLength) {
            setFilteredOptions(void 0);
            return;
          }
          const result = await asyncOptionsRef.current(debouncedInputValue, {
            signal
          });
          setFilteredOptions(result);
          onSearchedRef.current?.(debouncedInputValue);
        } else if (options) {
          setFilteredOptions(filterOptions(options, debouncedInputValue));
        } else {
          setError(new Error("No options provided"));
        }
      } catch (e) {
        if (e instanceof DOMException && e.name === "AbortError") {
          return;
        }
        setError(e);
      } finally {
        setIsLoading(false);
      }
    };
    if (!skipNextDebounceRef?.current) {
      void fetchOptions();
    } else {
      skipNextDebounceRef.current = false;
    }
    return () => {
      controller.abort();
    };
  }, [
    debouncedInputValue,
    options,
    delay,
    minSearchLength,
    skipNextDebounceRef
  ]);
  function updateFilteredOptions(newOptions) {
    setIsLoading(true);
    return newOptions.then(setFilteredOptions).catch(setError).finally(() => setIsLoading(false));
  }
  return {
    filteredOptions,
    updateFilteredOptions,
    isLoading,
    error,
    isError: error != null
  };
}
//# sourceMappingURL=useComboboxOptions.js.map
