const require_runtime = require("../../../../_virtual/_rolldown/runtime.js");
const require_index = require("../../../../pro/comprehensiveSearch/components/searchSection/index.js");
const require_hooks = require("../../hooks.js");
const require_const = require("./const.js");
const require_types = require("../../types.js");
const require_hooks$1 = require("./hooks.js");
const require_index$1 = require("../CupStatusModal/index.js");
let dayjs = require("dayjs");
dayjs = require_runtime.__toESM(dayjs);
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_utils = require("@pisell/utils");
let _pisell_materials = require("@pisell/materials");
let ahooks = require("ahooks");
require("./index.less");
//#region src/components/ecocup/components/SearchBar/index.tsx
const SearchBar = (props) => {
	const { scanListenerKey, size = "large", placeholder, onSearch, searchType, onSearchResult, customerId, onCupStatusValidate } = props;
	const [value, setValue] = (0, react.useState)("");
	const [searchResult, setSearchResult] = (0, react.useState)(null);
	const containerRef = (0, react.useRef)(null);
	const { addListener, removeListener, setActiveListener } = require_hooks.useScanListener();
	const searchHook = require_hooks$1.useSearch();
	const { getCupScanStatus } = require_hooks$1.useCupScanStatus();
	const cupStatusModal = require_hooks.useModal("ecocup-cup-status-modal");
	const shopId = require_hooks.useGetShopId();
	const onSelectResult = (0, ahooks.useMemoizedFn)((type, item) => {
		setSearchResult(null);
		setValue("");
		onSearchResult === null || onSearchResult === void 0 || onSearchResult({
			type,
			data: item
		});
	});
	const scanSearchCallback = (0, ahooks.useMemoizedFn)((str) => {
		console.log("scanSearchCallback>>>>>>>", str);
		if (onSearch) {
			onSearch(str || "");
			return;
		}
		if (!str) {
			setValue("");
			setSearchResult(null);
			return;
		}
		searchHook.run({
			searchType,
			searchText: str
		}).then((res) => {
			if ((res === null || res === void 0 ? void 0 : res.status) === "success") {
				var _res$data2;
				if ((res === null || res === void 0 ? void 0 : res.type) === "cup") {
					var _res$data;
					const _cupData = (res === null || res === void 0 || (_res$data = res.data) === null || _res$data === void 0 ? void 0 : _res$data[0]) || null;
					const scanStatus = getCupScanStatus(_cupData, customerId);
					console.log(scanStatus, _cupData, shopId, "sjdhfjj");
					if (onCupStatusValidate) {
						if (scanStatus && scanStatus === require_types.ECupScanStatus.OTHER_RENT) {
							onCupStatusValidate === null || onCupStatusValidate === void 0 || onCupStatusValidate({ data: _cupData || { code: str } });
							setValue("");
							return;
						}
					}
					if (scanStatus) {
						cupStatusModal.open({ data: {
							scanStatus,
							cupData: _cupData || { code: str }
						} });
						return;
					}
				}
				const dataLength = (res === null || res === void 0 || (_res$data2 = res.data) === null || _res$data2 === void 0 ? void 0 : _res$data2.length) || 0;
				if (dataLength === 1) {
					var _res$data3;
					setValue("");
					setSearchResult(null);
					onSelectResult(res === null || res === void 0 ? void 0 : res.type, res === null || res === void 0 || (_res$data3 = res.data) === null || _res$data3 === void 0 ? void 0 : _res$data3[0]);
					return;
				}
				if (dataLength === 0 && (res === null || res === void 0 ? void 0 : res.type) === "customer") {
					(0, _pisell_materials.PisellToast)({ content: _pisell_utils.locales.getText("pisell2.text.ecocup.toast.fail-no-match-customer") });
					setSearchResult(null);
					return;
				}
				setSearchResult(res === null || res === void 0 ? void 0 : res.data);
			} else {
				if (res === null || res === void 0 ? void 0 : res.message) (0, _pisell_materials.PisellToast)({ content: res === null || res === void 0 ? void 0 : res.message });
				setSearchResult(null);
			}
		});
	});
	(0, react.useEffect)(() => {
		addListener(scanListenerKey, (result) => {
			setValue("");
			setSearchResult(null);
			scanSearchCallback((result === null || result === void 0 ? void 0 : result.value) || "");
		});
		return () => {
			removeListener(scanListenerKey);
		};
	}, [scanListenerKey, scanSearchCallback]);
	(0, react.useEffect)(() => {
		/**
		* 点击非搜索区域时隐藏结果面板
		*/
		const handleClickOutside = (event) => {
			const target = event.target;
			if (!containerRef.current || !target) return;
			if (!containerRef.current.contains(target) && (searchResult === null || searchResult === void 0 ? void 0 : searchResult.length) > 1) setSearchResult(null);
		};
		document.addEventListener("mousedown", handleClickOutside);
		return () => {
			document.removeEventListener("mousedown", handleClickOutside);
		};
	}, [searchResult]);
	return /* @__PURE__ */ react.default.createElement("div", {
		ref: containerRef,
		className: "ecocup-cup-search-bar"
	}, /* @__PURE__ */ react.default.createElement("div", {
		style: { flex: 1 },
		onClick: () => {
			console.log("onClick>>>>>>>", scanListenerKey);
			setActiveListener(scanListenerKey);
		}
	}, /* @__PURE__ */ react.default.createElement(require_index.default, {
		value,
		onChange: setValue,
		loading: searchHook.loading,
		style: {
			flex: 1,
			height: size === "large" ? "60px" : "48px",
			padding: 0
		},
		placeholder: placeholder || _pisell_utils.locales.getText("pisell2.text.cup-project.search-bar.placeholder"),
		searchButtonProps: {
			placement: "inside",
			style: size === "large" ? {
				height: 48,
				padding: 8,
				minWidth: 160
			} : {
				height: 32,
				padding: 8,
				minWidth: 120
			}
		},
		cameraButtonProps: { icon: ["pisell2-camera-01", "pisell2-qr-code-02"] },
		onSearch: () => scanSearchCallback(value)
	})), (searchResult === null || searchResult === void 0 ? void 0 : searchResult.length) > 1 ? /* @__PURE__ */ react.default.createElement("div", { className: "ecocup-cup-search-bar-result" }, /* @__PURE__ */ react.default.createElement("div", { className: "ecocup-cup-search-bar-result-header" }, require_const.SEARCH_BAR_RESULT_HEADER_CONFIG === null || require_const.SEARCH_BAR_RESULT_HEADER_CONFIG === void 0 ? void 0 : require_const.SEARCH_BAR_RESULT_HEADER_CONFIG.map((item) => /* @__PURE__ */ react.default.createElement("div", { key: item.key }, _pisell_utils.locales.getText(item.label)))), /* @__PURE__ */ react.default.createElement("div", { className: "ecocup-cup-search-bar-result-content" }, searchResult === null || searchResult === void 0 ? void 0 : searchResult.map((item) => {
		var _tags$find, _tags$find2;
		const { nickname, display_name, create_time, country_calling_code, phone, email, tags } = item;
		return /* @__PURE__ */ react.default.createElement("div", {
			key: item.id,
			className: "ecocup-cup-search-bar-result-content-item",
			onClick: () => onSelectResult("customer", item)
		}, /* @__PURE__ */ react.default.createElement("div", null, display_name || nickname || "-"), /* @__PURE__ */ react.default.createElement("div", null, create_time ? (0, dayjs.default)(create_time).format("DD/MM/YYYY") : "-"), /* @__PURE__ */ react.default.createElement("div", null, phone ? `(${country_calling_code}) ${phone}` : "-"), /* @__PURE__ */ react.default.createElement("div", null, email || "-"), /* @__PURE__ */ react.default.createElement("div", null, /* @__PURE__ */ react.default.createElement("span", { className: "ecocup-cup-search-bar-result-content-item-membership" }, (tags === null || tags === void 0 || (_tags$find = tags.find) === null || _tags$find === void 0 ? void 0 : _tags$find.call(tags, (v) => (v === null || v === void 0 ? void 0 : v.name) == "admin")) ? "Admin" : (tags === null || tags === void 0 || (_tags$find2 = tags.find) === null || _tags$find2 === void 0 ? void 0 : _tags$find2.call(tags, (v) => (v === null || v === void 0 ? void 0 : v.name) == "business")) ? "Business" : "Regular")));
	}))) : null, cupStatusModal.visible && /* @__PURE__ */ react.default.createElement(require_index$1.default, cupStatusModal.modalProps));
};
//#endregion
exports.default = SearchBar;
