var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/select/index.tsx
var select_exports = {};
__export(select_exports, {
  default: () => select_default
});
module.exports = __toCommonJS(select_exports);
var import_react = __toESM(require("react"));
var import_antd = require("antd");
var import_ahooks = require("ahooks");
var import_classnames = __toESM(require("classnames"));
var import_iconfont = __toESM(require("../iconfont"));
var import_locales = require("../../locales");
var import_hoc = require("../../utils/hoc");
var import_browserSelect = __toESM(require("../browserSelect"));
var import_usePisellConfig = __toESM(require("../pisell-config-provider/hooks/usePisellConfig"));
var import_index = require("./index.less");
var prefix = "pisell-lowcode-";
var SelectBase = (0, import_react.forwardRef)((props, ref) => {
  const {
    isShowDropdown,
    selectAll,
    mode,
    options,
    dropdownRender: propsDropdownRender,
    showSearch,
    onSearch,
    styleType,
    optionFilterProp = "value",
    popupClassName,
    onDropdownVisibleChange,
    isSearchServer = false
  } = props;
  const { platform } = (0, import_usePisellConfig.default)();
  const searchInputRef = (0, import_react.useRef)(null);
  const [state, setState] = (0, import_ahooks.useControllableValue)(props);
  const [searchValue, setSearchValue] = (0, import_react.useState)("");
  const filteredOptions = (0, import_react.useMemo)(() => {
    if (!searchValue || isSearchServer) return options;
    return (options || []).filter(
      (option) => {
        var _a;
        const optionValue = ((_a = option[optionFilterProp]) == null ? void 0 : _a.toString().toLowerCase()) || "";
        return optionValue.includes(searchValue.toLowerCase());
      }
    );
  }, [options, searchValue, optionFilterProp, isSearchServer]);
  const onSelectAllChange = (0, import_ahooks.useMemoizedFn)((e) => {
    if (e.target.checked) {
      setState(
        filteredOptions.map((item) => item.value),
        filteredOptions
      );
    } else {
      setState([], []);
    }
  });
  const handleDropdownVisibleChange = (0, import_ahooks.useMemoizedFn)((visible) => {
    if (!visible) {
      setSearchValue("");
    } else {
      if (platform === "pc" && showSearch) {
        setTimeout(() => {
          var _a;
          (_a = searchInputRef.current) == null ? void 0 : _a.focus();
        }, 200);
      }
    }
    onDropdownVisibleChange == null ? void 0 : onDropdownVisibleChange(visible);
  });
  const isShowSelectAll = (0, import_react.useMemo)(() => {
    return mode === "multiple" && isShowDropdown && selectAll;
  }, [mode, selectAll, isShowDropdown]);
  const checkboxChecked = (0, import_react.useMemo)(() => {
    var _a, _b, _c;
    if (mode !== "multiple") return false;
    return (state == null ? void 0 : state.length) === (filteredOptions == null ? void 0 : filteredOptions.length) && ((_a = state == null ? void 0 : state.sort()) == null ? void 0 : _a.join("")) === ((_c = (_b = filteredOptions == null ? void 0 : filteredOptions.map((item) => item.value)) == null ? void 0 : _b.sort()) == null ? void 0 : _c.join(""));
  }, [state, filteredOptions]);
  const dropdownRender = (0, import_react.useCallback)(
    propsDropdownRender ? propsDropdownRender : (originNode) => {
      return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, showSearch && /* @__PURE__ */ import_react.default.createElement(
        import_antd.Input,
        {
          ref: searchInputRef,
          className: "pisell-select-search-input",
          size: "middle",
          placeholder: (0, import_locales.getText)("select-dropdown-render-search"),
          value: searchValue,
          onChange: (e) => {
            onSearch == null ? void 0 : onSearch(e.target.value);
            setSearchValue(e.target.value);
          },
          bordered: false,
          allowClear: true,
          prefix: /* @__PURE__ */ import_react.default.createElement(import_iconfont.default, { style: { color: "#98A2B3" }, type: "pisell2-search-lg" })
        }
      ), originNode, isShowDropdown && /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement(
        import_antd.Divider,
        {
          className: `${prefix}select-dropdown-render-divider`
        }
      ), /* @__PURE__ */ import_react.default.createElement(
        "div",
        {
          className: `${prefix}select-dropdown-render-options-wrap`
        },
        isShowSelectAll && /* @__PURE__ */ import_react.default.createElement(
          import_antd.Checkbox,
          {
            onChange: onSelectAllChange,
            checked: checkboxChecked
          },
          (0, import_locales.getText)("select-dropdown-render-select-all")
        )
      )));
    },
    [
      isShowDropdown,
      isShowSelectAll,
      checkboxChecked,
      propsDropdownRender,
      searchValue,
      showSearch,
      onSearch
    ]
  );
  if (styleType === "system") {
    return /* @__PURE__ */ import_react.default.createElement(import_browserSelect.default, { ...props });
  }
  return /* @__PURE__ */ import_react.default.createElement(
    import_antd.Select,
    {
      ...props,
      defaultActiveFirstOption: false,
      onDropdownVisibleChange: handleDropdownVisibleChange,
      showSearch: false,
      ref,
      value: state,
      onChange: (value, option) => setState(value, option),
      dropdownRender,
      options: filteredOptions,
      popupClassName: (0, import_classnames.default)("pisell-base-select-dropdown", popupClassName)
    }
  );
});
var Select = (0, import_hoc.withWrap)(SelectBase);
Select.Option = import_antd.Select.Option;
var select_default = Select;
