const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_objectWithoutProperties = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_utils = require("@pisell/utils");
let antd = require("antd");
let _ant_design_icons = require("@ant-design/icons");
let classnames = require("classnames");
classnames = require_runtime.__toESM(classnames);
//#region src/components/pisellSingleSelect/components/EditView.tsx
/**
* PisellSingleSelect - EditView (编辑态视图)
* 正常可编辑状态下的渲染
*/
const _excluded = [
	"value",
	"options",
	"label",
	"helperText",
	"error",
	"errorText",
	"required",
	"placeholder",
	"showSearch",
	"allowClear",
	"size",
	"className",
	"style",
	"onSearch",
	"filterOption",
	"onChange"
];
const EditView = (0, react.forwardRef)((props, ref) => {
	const { value, options, label, helperText, error, errorText, required, placeholder = "请选择", showSearch = false, allowClear = false, size = "middle", className, style, onSearch, filterOption, onChange } = props, restProps = require_objectWithoutProperties._objectWithoutProperties(props, _excluded);
	const antdOptions = (0, react.useMemo)(() => options.map((opt) => ({
		label: opt.label,
		value: opt.value,
		disabled: opt.disabled
	})), [options]);
	const handleChange = (newValue) => {
		const selectedOption = options.find((opt) => opt.value === newValue);
		onChange === null || onChange === void 0 || onChange(newValue !== null && newValue !== void 0 ? newValue : null, selectedOption !== null && selectedOption !== void 0 ? selectedOption : null);
	};
	const [searchKeyword, setSearchKeyword] = (0, react.useState)("");
	const [, setSearchInput] = (0, _pisell_utils.useDebouncedSearch)("", 300, (text) => {
		onSearch === null || onSearch === void 0 || onSearch(text);
	});
	const handleSearchInput = (e) => {
		const text = e.target.value;
		setSearchKeyword(text);
		setSearchInput(text);
	};
	const filteredAntdOptions = (0, react.useMemo)(() => {
		if (!showSearch || !searchKeyword) return antdOptions;
		if (filterOption) return antdOptions.filter((opt) => {
			const selectOption = options.find((o) => o.value === opt.value);
			return selectOption ? filterOption(searchKeyword, selectOption) : false;
		});
		return antdOptions.filter((opt) => opt.label.toLowerCase().includes(searchKeyword.toLowerCase()));
	}, [
		antdOptions,
		searchKeyword,
		showSearch,
		filterOption,
		options
	]);
	const wrapperRef = (0, react.useRef)(null);
	const menuItemSelectedIcon = /* @__PURE__ */ react.default.createElement(_ant_design_icons.CheckOutlined, { style: {
		color: "#7F56D9",
		fontSize: 16
	} });
	const dropdownRender = (0, react.useCallback)((menu) => {
		if (!showSearch) return menu;
		return /* @__PURE__ */ react.default.createElement("div", null, /* @__PURE__ */ react.default.createElement("div", { className: "pisell-select-search-wrapper" }, /* @__PURE__ */ react.default.createElement(antd.Input, {
			placeholder: "Search",
			prefix: /* @__PURE__ */ react.default.createElement(_ant_design_icons.SearchOutlined, { style: {
				color: "#98A2B3",
				fontSize: 16
			} }),
			value: searchKeyword,
			onChange: handleSearchInput,
			className: "pisell-select-search-input",
			bordered: false,
			autoFocus: true
		})), menu);
	}, [
		showSearch,
		searchKeyword,
		handleSearchInput
	]);
	return /* @__PURE__ */ react.default.createElement("div", {
		ref: wrapperRef,
		className: (0, classnames.default)("pisell-single-select-wrapper", className),
		style
	}, label && /* @__PURE__ */ react.default.createElement("label", { className: (0, classnames.default)("pisell-single-select-label", { "pisell-single-select-label-required": required }) }, label), /* @__PURE__ */ react.default.createElement(antd.Select, require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, restProps), {}, {
		ref,
		value: value !== null && value !== void 0 ? value : void 0,
		options: filteredAntdOptions,
		placeholder,
		showSearch: false,
		allowClear,
		menuItemSelectedIcon,
		dropdownRender,
		onChange: handleChange,
		size,
		popupClassName: "pisell-single-select-dropdown",
		getPopupContainer: () => {
			var _wrapperRef$current;
			return (_wrapperRef$current = wrapperRef.current) !== null && _wrapperRef$current !== void 0 ? _wrapperRef$current : document.body;
		},
		className: (0, classnames.default)("pisell-single-select", { "pisell-single-select-error": error })
	})), helperText && !error && /* @__PURE__ */ react.default.createElement("div", { className: "pisell-single-select-helper-text" }, helperText), error && errorText && /* @__PURE__ */ react.default.createElement("div", { className: "pisell-single-select-error-text" }, errorText));
});
EditView.displayName = "PisellSingleSelectEditView";
//#endregion
exports.default = EditView;
