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/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_util = require("../_util");
var import_hooks = require("../_util/hooks");
var import_ahooks = require("ahooks");
var import_context = require("../config-provider/context");
var import_utils = require("../locale/utils");
var { Option } = import_antd.Select;
var FIELD_NAMES = {
  id: "id",
  name: "name"
};
var EXTRA_VALUE = "";
var RefSelect = (props) => {
  const componentContext = (0, import_react.useContext)(import_context.ConfigContext);
  const { uiLocale } = componentContext;
  const { extra, children, list, fieldNames, maxCount, value, defaultValue, render, searchFn, onSearch, onChange, ...rest } = props;
  const alias = { ...FIELD_NAMES, ...fieldNames };
  const [currValue, setCurrValue] = (0, import_react.useState)(value || defaultValue);
  const [optionList, setOptionList] = (0, import_react.useState)(list || []);
  const [loading, setLoading] = (0, import_react.useState)(false);
  (0, import_ahooks.useUpdateEffect)(() => {
    setCurrValue(value);
  }, [value]);
  (0, import_ahooks.useUpdateEffect)(() => {
    setOptionList(list || []);
  }, [list]);
  (0, import_react.useEffect)(() => {
    if (!searchFn)
      return;
    setLoading(true);
    searchFn().then((data) => {
      if (!data)
        return;
      setOptionList(data);
    }).finally(() => setLoading(false));
  }, []);
  const _onChange = (value2, option) => {
    const _isArray = (0, import_util.isArray)(value2);
    if (maxCount && _isArray && value2.length > maxCount) {
      import_antd.message.warning((0, import_utils.replaceMessage)(uiLocale.Select.countWarning, { count: `${maxCount}` }));
      return;
    }
    let item = (optionList || []).filter(
      (item2) => _isArray ? value2.includes(item2[alias.id]) : (0, import_util.isObject)(value2) ? item2[alias.id] === value2.value : item2[alias.id] === value2
    );
    setCurrValue(value2);
    onChange && onChange(value2, option, _isArray ? item : item[0]);
  };
  return (0, import_hooks.useConfig)(
    /* @__PURE__ */ import_react.default.createElement(
      import_antd.Select,
      {
        placeholder: uiLocale.global.placeholder,
        loading,
        notFoundContent: loading ? /* @__PURE__ */ import_react.default.createElement(import_antd.Spin, { size: "small" }) : uiLocale.Empty.description,
        getPopupContainer: (triggerNode) => triggerNode.parentNode ? triggerNode.parentNode : document.body,
        ...rest,
        style: { width: "100%", ...rest.style || {} },
        value: currValue,
        onChange: _onChange
      },
      extra && /* @__PURE__ */ import_react.default.createElement(Option, { value: EXTRA_VALUE, key: extra }, extra),
      children ? children : render ? render(optionList) : optionList.map((item) => /* @__PURE__ */ import_react.default.createElement(Option, { key: item[alias.id], value: item[alias.id], title: item[alias.name] }, item[alias.name]))
    )
  );
};
RefSelect.Option = Option;
var select_default = RefSelect;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {});
