const require_runtime = require("../../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_asyncToGenerator = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_index = require("../drawer/index.js");
const require_index$1 = require("../addButton/index.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let _pisell_utils = require("@pisell/utils");
let _pisell_icon_es_PlusCircle = require("@pisell/icon/es/PlusCircle");
_pisell_icon_es_PlusCircle = require_runtime.__toESM(_pisell_icon_es_PlusCircle);
let _pisell_icon_es_DotsVertical = require("@pisell/icon/es/DotsVertical");
_pisell_icon_es_DotsVertical = require_runtime.__toESM(_pisell_icon_es_DotsVertical);
let _pisell_icon_es_SearchLg = require("@pisell/icon/es/SearchLg");
_pisell_icon_es_SearchLg = require_runtime.__toESM(_pisell_icon_es_SearchLg);
let classnames = require("classnames");
classnames = require_runtime.__toESM(classnames);
require("./index.less");
//#region src/components/booking/components/customSelect/index.tsx
/**
* @title: 选择器
* @description:
* @param {CustomSelectProps} props
* @param {any} ref
* @return {*}
* @Author: zhiwei.Wang
* @Date: 2024-01-30 16:44
*/
const CustomSelect = (props, ref) => {
	const { loading, options, onValueChange, value, rowKey = "id", addButtonProps, drawerProps, createItemText = "", previewItemRender, previewItemTitleRender, addButtonRender, optionRender, filterItem, previewItemClassName, optionClassName, mode = "single", isPreview = true, name = "item", handleCreateItem, handleViewItem, virtualListHeight = window.innerHeight - 213 - 16, disabled = false, searchValue, searchChange, onVirtualListScroll, loadMore, modalOpen, closeCallback, autoFocus, showDropDown = true, handleOpenModal: propsHandleOpenModal, handleDayChange } = props;
	console.log("custom select");
	const searchRef = (0, react.useRef)();
	const [_value, set_value] = (0, react.useState)([]);
	const [search, setSearch] = (0, react.useState)("");
	const [open, setOpen] = (0, react.useState)(false);
	(0, react.useEffect)(() => {
		if (modalOpen === false) setOpen(false);
	}, [modalOpen]);
	(0, react.useEffect)(() => {
		initValue();
	}, [(0, react.useMemo)(() => {
		return JSON.stringify(value);
	}, [value])]);
	/**
	* @title: 转换value到内部
	* @description:
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-01-30 16:45
	*/
	const initValue = () => {
		let _val = value;
		if (!value) _val = [];
		else if (!Array.isArray(value)) _val = [value];
		set_value(_val);
	};
	/**
	* @title: 关闭弹窗
	* @description:
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-01-30 16:45
	*/
	const closeDrawer = (reset) => {
		setOpen(false);
		setSearch("");
		if (reset) initValue();
		closeCallback === null || closeCallback === void 0 || closeCallback();
	};
	/**
	* @title: 打开弹窗
	* @description:
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-01-30 16:45
	*/
	const handleOpenModal = (item) => {
		if (disabled) return;
		if (propsHandleOpenModal) {
			propsHandleOpenModal();
			return;
		}
		if (addButtonProps === null || addButtonProps === void 0 ? void 0 : addButtonProps.addCallback) addButtonProps === null || addButtonProps === void 0 || addButtonProps.addCallback();
		setOpen(true);
		initValue();
		if (autoFocus) setTimeout(() => {
			var _searchRef$current, _searchRef$current$fo;
			(_searchRef$current = searchRef.current) === null || _searchRef$current === void 0 || (_searchRef$current$fo = _searchRef$current.focus) === null || _searchRef$current$fo === void 0 || _searchRef$current$fo.call(_searchRef$current);
		}, 100);
	};
	/**
	* @title: value 发生变化传递到外部
	* @description:
	* @param {any} _val
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-01-30 16:46
	*/
	const _onValueChange = function() {
		var _ref = require_asyncToGenerator._asyncToGenerator(function* (_val) {
			yield onValueChange(_val);
		});
		return function _onValueChange(_x) {
			return _ref.apply(this, arguments);
		};
	}();
	/**
	* @title: 选择
	* @description:
	* @param {any} item
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-01-30 16:46
	*/
	const handleSelectItem = (item) => {
		let _val = (0, _pisell_utils.changeArray)(_value, item, rowKey);
		if (mode === "single") handleApply([item]);
		set_value(_val);
	};
	/**
	* @title: 预览事件
	* @description:
	* @param {any} item
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-01-30 16:46
	*/
	const _handleViewItem = (item) => {
		handleViewItem === null || handleViewItem === void 0 || handleViewItem(item);
	};
	/**
	* @title: 删除事件
	* @description:
	* @param {any} item
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-01-30 16:47
	*/
	const handleRemoveItem = (item) => {
		if (handleDayChange) handleDayChange(item);
		let _arr = (0, _pisell_utils.changeArray)(_value, item, rowKey);
		_onValueChange(_arr);
		set_value(_arr);
	};
	const menuChange = (e, item) => {
		({
			view: _handleViewItem,
			change: handleOpenModal,
			remove: handleRemoveItem
		})[e.key](item);
	};
	/**
	* @title: 渲染列表
	* @description:
	* @param {*} useMemo
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-01-30 16:47
	*/
	const renderList = (0, react.useMemo)(() => {
		let _list = [...options];
		if (search) {
			const isIncludes = (item) => {
				try {
					const q = search.toLowerCase();
					const numericKeys = Object.keys(item).filter((k) => /^\d+$/.test(k));
					const customerKeys = [
						"customer_nickname",
						"customer_first_name",
						"customer_last_name",
						"customer_phone",
						"customer_email"
					];
					return [...numericKeys, ...customerKeys].some((k) => {
						const val = item[k];
						if (val === null || val === void 0) return false;
						try {
							const str = JSON.stringify(val);
							return str.includes(q) || str.toLocaleLowerCase().includes(q);
						} catch (error) {
							return false;
						}
					});
				} catch (e) {
					return false;
				}
			};
			_list = _list.filter((d) => {
				if (filterItem) return filterItem(d, search);
				return isIncludes(d);
			});
		}
		return _list;
	}, [
		options,
		loading,
		search
	]);
	/**
	* @title: 是否展示添加其他
	* @description:
	* @param {*} useMemo
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-01-30 16:47
	*/
	const isMore = (0, react.useMemo)(() => {
		return _value.length > 0;
	}, [_value]);
	/**
	* @title: 确定按钮, 返回数据并关闭弹窗
	* @description:
	* @param {any} _val
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-01-30 16:47
	*/
	const handleApply = function() {
		var _ref2 = require_asyncToGenerator._asyncToGenerator(function* (_val) {
			yield _onValueChange(_val || _value);
			closeDrawer();
		});
		return function handleApply(_x2) {
			return _ref2.apply(this, arguments);
		};
	}();
	(0, react.useImperativeHandle)(ref, () => {
		return {
			open: handleOpenModal,
			setValue: set_value
		};
	});
	/**
	* @title: 菜单列表
	* @description:
	* @param {*} useMemo
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-01-30 16:48
	*/
	const menuItems = (0, react.useMemo)(() => {
		let list = [
			{
				key: "view",
				label: `${_pisell_utils.locales.getText("pisell2.text.view")} ${name}`
			},
			{
				key: "change",
				label: `${_pisell_utils.locales.getText("pisell2.text.change")} ${name}`
			},
			{
				key: "remove",
				label: `${_pisell_utils.locales.getText("pisell2.text.remove")} ${name}`
			}
		];
		if (disabled) list = list.filter((d) => d.key === "view");
		return list;
	}, [
		name,
		disabled,
		_pisell_utils.locales
	]);
	/**
	* @title: 是否展示 add
	* @description:
	* @param {*} useMemo
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-01-30 16:49
	*/
	const isShowAddButton = (0, react.useMemo)(() => {
		return (mode === "multiple" || mode === "single" && !isMore) && !disabled;
	}, [
		mode,
		isMore,
		disabled
	]);
	return /* @__PURE__ */ react.default.createElement("div", null, isPreview ? /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, /* @__PURE__ */ react.default.createElement("div", { className: "pisell-lowcode__custom-select-cards" }, _value.map((d, index) => {
		return /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, previewItemTitleRender === null || previewItemTitleRender === void 0 ? void 0 : previewItemTitleRender(d, index, _value), /* @__PURE__ */ react.default.createElement("div", {
			className: (0, classnames.default)("pisell-lowcode__custom-select-card", previewItemClassName),
			key: d[rowKey]
		}, previewItemRender === null || previewItemRender === void 0 ? void 0 : previewItemRender(d), showDropDown && /* @__PURE__ */ react.default.createElement("div", { onClick: (e) => {
			e.preventDefault();
			e.stopPropagation();
		} }, /* @__PURE__ */ react.default.createElement(antd.Dropdown, {
			menu: {
				items: menuItems,
				onClick: (e) => {
					menuChange(e, d);
				}
			},
			placement: "bottomRight"
		}, /* @__PURE__ */ react.default.createElement("div", { className: "pisell-lowcode__custom-select-card-menu-wrap" }, /* @__PURE__ */ react.default.createElement(_pisell_icon_es_DotsVertical.default, { className: "pisell-lowcode__custom-select-card-menu-icon" }))))));
	})), isShowAddButton ? addButtonRender ? addButtonRender() : /* @__PURE__ */ react.default.createElement(require_index$1.default, require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({ showSub: isMore }, addButtonProps), {}, { onClick: () => {
		if (addButtonProps.onClick) {
			addButtonProps.onClick();
			return;
		}
		handleOpenModal();
	} })) : disabled ? /* @__PURE__ */ react.default.createElement("div", { style: { height: 20 } }) : null) : null, /* @__PURE__ */ react.default.createElement(require_index.default, require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({
		open,
		onClose: () => {
			closeDrawer(true);
		},
		titleNoWrap: true,
		header: /* @__PURE__ */ react.default.createElement("div", null, /* @__PURE__ */ react.default.createElement(antd.Input, {
			ref: searchRef,
			prefix: /* @__PURE__ */ react.default.createElement(_pisell_icon_es_SearchLg.default, null),
			style: {
				marginTop: 16,
				height: 44
			},
			placeholder: _pisell_utils.locales.getText("pisell2.booking.text.search-placeholder"),
			value: search,
			allowClear: true,
			onChange: (e) => {
				setSearch(e.target.value);
				if (searchChange) searchChange(e.target.value);
			}
		}), /* @__PURE__ */ react.default.createElement("div", {
			className: "pisell-lowcode__custom-select-create",
			onClick: () => {
				handleCreateItem === null || handleCreateItem === void 0 || handleCreateItem();
			},
			style: { padding: createItemText ? 16 : "16px 0 0 0" }
		}, createItemText && /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, /* @__PURE__ */ react.default.createElement(_pisell_icon_es_PlusCircle.default, { className: "pisell-lowcode__custom-select-create-icon" }), /* @__PURE__ */ react.default.createElement("span", { className: "pisell-lowcode__custom-select-create-txt" }, createItemText))))
	}, drawerProps), {}, { footer: mode === "multiple" && /* @__PURE__ */ react.default.createElement("div", null, /* @__PURE__ */ react.default.createElement(antd.Button, {
		style: { width: "100%" },
		type: "primary",
		size: "large",
		onClick: () => {
			handleApply();
		}
	}, _pisell_utils.locales.getText("pisell2.text.apply"))) }), /* @__PURE__ */ react.default.createElement(antd.List, {
		itemLayout: "horizontal",
		dataSource: renderList,
		loading: {
			spinning: !!loading,
			delay: 500
		},
		loadMore,
		renderItem: (item) => /* @__PURE__ */ react.default.createElement(antd.List.Item, {
			className: (0, classnames.default)("pisell-lowcode__custom-select-card-list-item", optionClassName, { "pisell-lowcode__custom-select-card-list-item-active": (0, _pisell_utils.getItemByArray)(_value, item, rowKey) }),
			onClick: () => {
				if ((item === null || item === void 0 ? void 0 : item.status) === "expired") return;
				handleSelectItem(item);
			},
			id: item[rowKey]
		}, item.loading ? /* @__PURE__ */ react.default.createElement(antd.Skeleton, {
			active: true,
			round: true,
			loading: item.loading,
			avatar: true,
			paragraph: false
		}) : /* @__PURE__ */ react.default.createElement("div", { className: "pisell-lowcode__custom-select-card-list-item-content" }, mode === "multiple" && /* @__PURE__ */ react.default.createElement(antd.Checkbox, {
			disabled: (item === null || item === void 0 ? void 0 : item.status) === "expired",
			className: "custom-select-checkbox",
			checked: (0, _pisell_utils.getItemByArray)(_value, item, rowKey)
		}), optionRender(item)))
	})));
};
var customSelect_default = (0, react.forwardRef)(CustomSelect);
//#endregion
exports.default = customSelect_default;
