import "../Select.css";
import { jsxs, jsx } from "react/jsx-runtime";
import { Select as Select$1 } from "antd";
import React__default, { forwardRef, useState, useMemo, useCallback } from "react";
import Icon from "./Icon.js";
import Label, { LabelTypes } from "./internal/Label.js";
import { useSelectEscape } from "./internal/useInputEscape.js";
import { useTheme } from "./Theme/UI.js";
import "./Theme/themeUtils.js";
import "./Theme/themes.js";
const base = "Select_base_cde8b";
const disableTags = "Select_disableTags_cde8b";
const css = {
  base,
  disableTags
};
const { OptGroup, Option } = Select$1;
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 = 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 }
  } = useTheme();
  const [isOpen, setIsOpen] = useState(false);
  const classes = [css.base, className];
  const divRef = React__default.createRef();
  const { onBlur, onFocus, inputRef } = useSelectEscape(divRef, isOpen, ref);
  if (disableTags2)
    classes.push(css.disableTags);
  const optionsCount = useMemo(() => countOptions(children, options), [children, options]);
  const [maxTagCount, maxTagPlaceholder] = 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 = useCallback((open) => {
    setIsOpen(open);
  }, []);
  const handleFilter = 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__ */ jsxs("div", { className: classes.join(" "), ref: divRef, children: [
    label && /* @__PURE__ */ jsx(Label, { type: LabelTypes.TextOnly, children: label }),
    /* @__PURE__ */ jsx(
      Select$1,
      {
        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__ */ jsx(Icon, { name: "arrow-down", size: "tiny", title: "Open" }) : void 0,
        value,
        onBlur,
        onDropdownVisibleChange: handleDropdownVisibleChange,
        onFocus,
        onSearch,
        ...passthrough,
        children
      }
    )
  ] });
});
export {
  OptGroup,
  Option,
  Select as default
};
