const require_runtime = require("../../_virtual/_rolldown/runtime.js");
const require_index = require("../../locales/index.js");
const require_objectSpread2 = require("../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_index$1 = require("../iconfont/index.js");
const require_usePisellConfig = require("../pisell-config-provider/hooks/usePisellConfig.js");
const require_hoc = require("../../utils/hoc.js");
const require_index$2 = require("../browserSelect/index.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let classnames = require("classnames");
classnames = require_runtime.__toESM(classnames);
let ahooks = require("ahooks");
require("./index.less");
//#region src/components/select/index.tsx
const prefix = "pisell-lowcode-";
const Select = require_hoc.withWrap((0, react.forwardRef)((props, ref) => {
	const { isShowDropdown, selectAll, mode, options, dropdownRender: propsDropdownRender, showSearch, onSearch, styleType, optionFilterProp = "value", popupClassName, onDropdownVisibleChange, isSearchServer = false } = props;
	const { platform } = require_usePisellConfig.default();
	const searchInputRef = (0, react.useRef)(null);
	const [state, setState] = (0, ahooks.useControllableValue)(props);
	const [searchValue, setSearchValue] = (0, react.useState)("");
	const filteredOptions = (0, react.useMemo)(() => {
		if (!searchValue || isSearchServer) return options;
		return (options || []).filter((option) => {
			var _option$optionFilterP;
			return (((_option$optionFilterP = option[optionFilterProp]) === null || _option$optionFilterP === void 0 ? void 0 : _option$optionFilterP.toString().toLowerCase()) || "").includes(searchValue.toLowerCase());
		});
	}, [
		options,
		searchValue,
		optionFilterProp,
		isSearchServer
	]);
	const onSelectAllChange = (0, ahooks.useMemoizedFn)((e) => {
		if (e.target.checked) setState(filteredOptions.map((item) => item.value), filteredOptions);
		else setState([], []);
	});
	const handleDropdownVisibleChange = (0, ahooks.useMemoizedFn)((visible) => {
		if (!visible) setSearchValue("");
		else if (platform === "pc") setTimeout(() => {
			var _searchInputRef$curre;
			(_searchInputRef$curre = searchInputRef.current) === null || _searchInputRef$curre === void 0 || _searchInputRef$curre.focus();
		}, 200);
		onDropdownVisibleChange === null || onDropdownVisibleChange === void 0 || onDropdownVisibleChange(visible);
	});
	const isShowSelectAll = (0, react.useMemo)(() => {
		return mode === "multiple" && isShowDropdown && selectAll;
	}, [
		mode,
		selectAll,
		isShowDropdown
	]);
	const checkboxChecked = (0, react.useMemo)(() => {
		var _state$sort, _filteredOptions$map;
		if (mode !== "multiple") return false;
		return (state === null || state === void 0 ? void 0 : state.length) === (filteredOptions === null || filteredOptions === void 0 ? void 0 : filteredOptions.length) && (state === null || state === void 0 || (_state$sort = state.sort()) === null || _state$sort === void 0 ? void 0 : _state$sort.join("")) === (filteredOptions === null || filteredOptions === void 0 || (_filteredOptions$map = filteredOptions.map((item) => item.value)) === null || _filteredOptions$map === void 0 || (_filteredOptions$map = _filteredOptions$map.sort()) === null || _filteredOptions$map === void 0 ? void 0 : _filteredOptions$map.join(""));
	}, [state, filteredOptions]);
	const dropdownRender = (0, react.useCallback)(propsDropdownRender ? propsDropdownRender : (originNode) => {
		return /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, showSearch && /* @__PURE__ */ react.default.createElement(antd.Input, {
			ref: searchInputRef,
			className: "pisell-select-search-input",
			size: "middle",
			placeholder: require_index.getText("select-dropdown-render-search"),
			value: searchValue,
			onChange: (e) => {
				onSearch === null || onSearch === void 0 || onSearch(e.target.value);
				setSearchValue(e.target.value);
			},
			bordered: false,
			allowClear: true,
			prefix: /* @__PURE__ */ react.default.createElement(require_index$1.default, {
				style: { color: "#98A2B3" },
				type: "pisell2-search-lg"
			})
		}), originNode, isShowDropdown && /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, /* @__PURE__ */ react.default.createElement(antd.Divider, { className: `${prefix}select-dropdown-render-divider` }), /* @__PURE__ */ react.default.createElement("div", { className: `${prefix}select-dropdown-render-options-wrap` }, isShowSelectAll && /* @__PURE__ */ react.default.createElement(antd.Checkbox, {
			onChange: onSelectAllChange,
			checked: checkboxChecked
		}, require_index.getText("select-dropdown-render-select-all")))));
	}, [
		isShowDropdown,
		isShowSelectAll,
		checkboxChecked,
		propsDropdownRender,
		searchValue,
		showSearch,
		onSearch
	]);
	if (styleType === "system") return /* @__PURE__ */ react.default.createElement(require_index$2.default, props);
	return /* @__PURE__ */ react.default.createElement(antd.Select, require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, props), {}, {
		defaultActiveFirstOption: false,
		onDropdownVisibleChange: handleDropdownVisibleChange,
		showSearch: false,
		ref,
		value: state,
		onChange: (value, option) => setState(value, option),
		dropdownRender,
		options: filteredOptions,
		popupClassName: (0, classnames.default)("pisell-base-select-dropdown", popupClassName)
	}));
}));
Select.Option = antd.Select.Option;
//#endregion
exports.default = Select;
