import { getText } from "../../locales/index.js";
import { _objectSpread2 } from "../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import IconFont from "../iconfont/index.js";
import usePisellConfig from "../pisell-config-provider/hooks/usePisellConfig.js";
import { withWrap } from "../../utils/hoc.js";
import BrowserSelect from "../browserSelect/index.js";
import React, { forwardRef, useCallback, useMemo, useRef, useState } from "react";
import { Checkbox, Divider, Input, Select } from "antd";
import classNames from "classnames";
import { useControllableValue, useMemoizedFn } from "ahooks";
import "./index.less";
//#region src/components/select/index.tsx
const prefix = "pisell-lowcode-";
const Select$1 = withWrap(forwardRef((props, ref) => {
	const { isShowDropdown, selectAll, mode, options, dropdownRender: propsDropdownRender, showSearch, onSearch, styleType, optionFilterProp = "value", popupClassName, onDropdownVisibleChange, isSearchServer = false } = props;
	const { platform } = usePisellConfig();
	const searchInputRef = useRef(null);
	const [state, setState] = useControllableValue(props);
	const [searchValue, setSearchValue] = useState("");
	const filteredOptions = 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 = useMemoizedFn((e) => {
		if (e.target.checked) setState(filteredOptions.map((item) => item.value), filteredOptions);
		else setState([], []);
	});
	const handleDropdownVisibleChange = 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 = useMemo(() => {
		return mode === "multiple" && isShowDropdown && selectAll;
	}, [
		mode,
		selectAll,
		isShowDropdown
	]);
	const checkboxChecked = 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 = useCallback(propsDropdownRender ? propsDropdownRender : (originNode) => {
		return /* @__PURE__ */ React.createElement(React.Fragment, null, showSearch && /* @__PURE__ */ React.createElement(Input, {
			ref: searchInputRef,
			className: "pisell-select-search-input",
			size: "middle",
			placeholder: 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.createElement(IconFont, {
				style: { color: "#98A2B3" },
				type: "pisell2-search-lg"
			})
		}), originNode, isShowDropdown && /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(Divider, { className: `${prefix}select-dropdown-render-divider` }), /* @__PURE__ */ React.createElement("div", { className: `${prefix}select-dropdown-render-options-wrap` }, isShowSelectAll && /* @__PURE__ */ React.createElement(Checkbox, {
			onChange: onSelectAllChange,
			checked: checkboxChecked
		}, getText("select-dropdown-render-select-all")))));
	}, [
		isShowDropdown,
		isShowSelectAll,
		checkboxChecked,
		propsDropdownRender,
		searchValue,
		showSearch,
		onSearch
	]);
	if (styleType === "system") return /* @__PURE__ */ React.createElement(BrowserSelect, props);
	return /* @__PURE__ */ React.createElement(Select, _objectSpread2(_objectSpread2({}, props), {}, {
		defaultActiveFirstOption: false,
		onDropdownVisibleChange: handleDropdownVisibleChange,
		showSearch: false,
		ref,
		value: state,
		onChange: (value, option) => setState(value, option),
		dropdownRender,
		options: filteredOptions,
		popupClassName: classNames("pisell-base-select-dropdown", popupClassName)
	}));
}));
Select$1.Option = Select.Option;
//#endregion
export { Select$1 as default };
