import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { _objectWithoutProperties } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js";
import React, { forwardRef, useCallback, useMemo, useRef, useState } from "react";
import { useDebouncedSearch } from "@pisell/utils";
import { Input, Select } from "antd";
import { CheckOutlined, SearchOutlined } from "@ant-design/icons";
import classNames from "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 = 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 = _objectWithoutProperties(props, _excluded);
	const antdOptions = 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] = useState("");
	const [, setSearchInput] = useDebouncedSearch("", 300, (text) => {
		onSearch === null || onSearch === void 0 || onSearch(text);
	});
	const handleSearchInput = (e) => {
		const text = e.target.value;
		setSearchKeyword(text);
		setSearchInput(text);
	};
	const filteredAntdOptions = 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 = useRef(null);
	const menuItemSelectedIcon = /* @__PURE__ */ React.createElement(CheckOutlined, { style: {
		color: "#7F56D9",
		fontSize: 16
	} });
	const dropdownRender = useCallback((menu) => {
		if (!showSearch) return menu;
		return /* @__PURE__ */ React.createElement("div", null, /* @__PURE__ */ React.createElement("div", { className: "pisell-select-search-wrapper" }, /* @__PURE__ */ React.createElement(Input, {
			placeholder: "Search",
			prefix: /* @__PURE__ */ React.createElement(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.createElement("div", {
		ref: wrapperRef,
		className: classNames("pisell-single-select-wrapper", className),
		style
	}, label && /* @__PURE__ */ React.createElement("label", { className: classNames("pisell-single-select-label", { "pisell-single-select-label-required": required }) }, label), /* @__PURE__ */ React.createElement(Select, _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: classNames("pisell-single-select", { "pisell-single-select-error": error })
	})), helperText && !error && /* @__PURE__ */ React.createElement("div", { className: "pisell-single-select-helper-text" }, helperText), error && errorText && /* @__PURE__ */ React.createElement("div", { className: "pisell-single-select-error-text" }, errorText));
});
EditView.displayName = "PisellSingleSelectEditView";
//#endregion
export { EditView as default };
