"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxRuntime = require("react/jsx-runtime");
const clsx = require("clsx");
const CheckIcon = require("../../Checkbox/CheckIcon.cjs");
const ScrollArea = require("../../ScrollArea/ScrollArea.cjs");
const Combobox = require("../Combobox.cjs");
const defaultOptionsFilter = require("./default-options-filter.cjs");
const isEmptyComboboxData = require("./is-empty-combobox-data.cjs");
const isOptionsGroup = require("./is-options-group.cjs");
const validateOptions = require("./validate-options.cjs");
const Combobox_module_css = require("../Combobox.module.css.cjs");
function isValueChecked(value, optionValue) {
  return Array.isArray(value) ? value.includes(optionValue) : value === optionValue;
}
function Option({
  data,
  withCheckIcon,
  value,
  checkIconPosition,
  unstyled,
  renderOption
}) {
  if (!isOptionsGroup.isOptionsGroup(data)) {
    const checked = isValueChecked(value, data.value);
    const check = withCheckIcon && checked && /* @__PURE__ */ jsxRuntime.jsx(CheckIcon.CheckIcon, { className: Combobox_module_css.default.optionsDropdownCheckIcon });
    const defaultContent = /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
      checkIconPosition === "left" && check,
      /* @__PURE__ */ jsxRuntime.jsx("span", { children: data.label }),
      checkIconPosition === "right" && check
    ] });
    return /* @__PURE__ */ jsxRuntime.jsx(
      Combobox.Combobox.Option,
      {
        value: data.value,
        disabled: data.disabled,
        className: clsx({ [Combobox_module_css.default.optionsDropdownOption]: !unstyled }),
        "data-reverse": checkIconPosition === "right" || void 0,
        "data-checked": checked || void 0,
        "aria-selected": checked,
        active: checked,
        children: typeof renderOption === "function" ? renderOption({ option: data, checked }) : defaultContent
      }
    );
  }
  const options = data.items.map((item) => /* @__PURE__ */ jsxRuntime.jsx(
    Option,
    {
      data: item,
      value,
      unstyled,
      withCheckIcon,
      checkIconPosition,
      renderOption
    },
    item.value
  ));
  return /* @__PURE__ */ jsxRuntime.jsx(Combobox.Combobox.Group, { label: data.group, children: options });
}
function OptionsDropdown({
  data,
  hidden,
  hiddenWhenEmpty,
  filter,
  search,
  limit,
  maxDropdownHeight,
  withScrollArea = true,
  filterOptions = true,
  withCheckIcon = false,
  value,
  checkIconPosition,
  nothingFoundMessage,
  unstyled,
  labelId,
  renderOption,
  scrollAreaProps,
  "aria-label": ariaLabel
}) {
  validateOptions.validateOptions(data);
  const shouldFilter = typeof search === "string";
  const filteredData = shouldFilter ? (filter || defaultOptionsFilter.defaultOptionsFilter)({
    options: data,
    search: filterOptions ? search : "",
    limit: limit ?? Infinity
  }) : data;
  const isEmpty = isEmptyComboboxData.isEmptyComboboxData(filteredData);
  const options = filteredData.map((item) => /* @__PURE__ */ jsxRuntime.jsx(
    Option,
    {
      data: item,
      withCheckIcon,
      value,
      checkIconPosition,
      unstyled,
      renderOption
    },
    isOptionsGroup.isOptionsGroup(item) ? item.group : item.value
  ));
  return /* @__PURE__ */ jsxRuntime.jsx(Combobox.Combobox.Dropdown, { hidden: hidden || hiddenWhenEmpty && isEmpty, children: /* @__PURE__ */ jsxRuntime.jsxs(Combobox.Combobox.Options, { labelledBy: labelId, "aria-label": ariaLabel, children: [
    withScrollArea ? /* @__PURE__ */ jsxRuntime.jsx(
      ScrollArea.ScrollArea.Autosize,
      {
        mah: maxDropdownHeight ?? 220,
        type: "scroll",
        scrollbarSize: "var(--combobox-padding)",
        offsetScrollbars: "y",
        ...scrollAreaProps,
        children: options
      }
    ) : options,
    isEmpty && nothingFoundMessage && /* @__PURE__ */ jsxRuntime.jsx(Combobox.Combobox.Empty, { children: nothingFoundMessage })
  ] }) });
}
exports.OptionsDropdown = OptionsDropdown;
