"use strict";
var import_Select = require("../Select.css");
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const jsxRuntime = require("react/jsx-runtime");
const antd = require("antd");
const React = require("react");
const kit_Icon = require("./Icon.cjs");
const kit_internal_Label = require("./internal/Label.cjs");
const kit_internal_useInputEscape = require("./internal/useInputEscape.cjs");
const kit_Theme_UI = require("./Theme/UI.cjs");
require("./Theme/themeUtils.cjs");
require("./Theme/themes.cjs");
const base = "Select_base_cde8b";
const disableTags = "Select_disableTags_cde8b";
const css = {
  base,
  disableTags
};
const { OptGroup, Option } = antd.Select;
const countOptions = (children, options) => {
  let count = 0;
  if (options)
    return options.length;
  if (!children)
    return count;
  if (Array.isArray(children)) {
    count += children.map((child) => countOptions(child, options)).reduce((acc, count2) => acc + count2, 0);
  }
  const childType = children.type;
  const childProps = children.props;
  const childList = childProps == null ? void 0 : childProps.children;
  if (childType === Option)
    count++;
  if (childType === OptGroup && childList)
    count += countOptions(childList, options);
  return count;
};
const Select = React.forwardRef(function Select2({
  attachDropdownToContainer,
  disabled,
  disableTags: disableTags2 = false,
  searchable = true,
  dropdownMatchSelectWidth = true,
  filterOption,
  label,
  loading,
  onSearch,
  options,
  width,
  value,
  children,
  onDropdownVisibleChange,
  ...passthrough
}, ref) {
  const {
    themeSettings: { className }
  } = kit_Theme_UI.useTheme();
  const [isOpen, setIsOpen] = React.useState(false);
  const classes = [css.base, className];
  const divRef = React.createRef();
  const { onBlur, onFocus, inputRef } = kit_internal_useInputEscape.useSelectEscape(divRef, isOpen, ref);
  if (disableTags2)
    classes.push(css.disableTags);
  const optionsCount = React.useMemo(() => countOptions(children, options), [children, options]);
  const [maxTagCount, maxTagPlaceholder] = React.useMemo(() => {
    if (!disableTags2)
      return [void 0, ""];
    const count = Array.isArray(value) ? value.length : value ? 1 : 0;
    const itemLabel = "selected";
    const placeholder = count === optionsCount ? "All" : `${count} ${itemLabel}`;
    return isOpen ? [0, ""] : [0, placeholder];
  }, [disableTags2, isOpen, optionsCount, value]);
  const handleDropdownVisibleChange = React.useCallback((open) => {
    setIsOpen(open);
  }, []);
  const handleFilter = React.useCallback((search, option) => {
    let label2 = null;
    if (!(option == null ? void 0 : option.children) && !(option == null ? void 0 : option.label))
      return false;
    if (Array.isArray(option.children)) {
      label2 = option.children.join(" ");
    } else if (option.label) {
      label2 = option.label.toString();
    } else if (typeof option.children === "string") {
      label2 = option.children;
    }
    return !!label2 && label2.toLocaleLowerCase().includes(search.toLocaleLowerCase());
  }, []);
  const getPopupContainer = (triggerNode) => {
    return attachDropdownToContainer && triggerNode.parentElement || document.body;
  };
  return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: classes.join(" "), ref: divRef, children: [
    label && /* @__PURE__ */ jsxRuntime.jsx(kit_internal_Label.default, { type: kit_internal_Label.LabelTypes.TextOnly, children: label }),
    /* @__PURE__ */ jsxRuntime.jsx(
      antd.Select,
      {
        disabled: disabled || loading,
        dropdownMatchSelectWidth,
        filterOption: filterOption === void 0 ? searchable ? handleFilter : true : filterOption,
        getPopupContainer,
        maxTagCount,
        maxTagPlaceholder,
        options,
        popupClassName: className,
        ref: inputRef,
        showSearch: !!onSearch || !!filterOption || searchable,
        style: { width },
        suffixIcon: !loading ? /* @__PURE__ */ jsxRuntime.jsx(kit_Icon.default, { name: "arrow-down", size: "tiny", title: "Open" }) : void 0,
        value,
        onBlur,
        onDropdownVisibleChange: handleDropdownVisibleChange,
        onFocus,
        onSearch,
        ...passthrough,
        children
      }
    )
  ] });
});
exports.OptGroup = OptGroup;
exports.Option = Option;
exports.default = Select;
