import { _objectSpread2 } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { _asyncToGenerator } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import { _objectWithoutProperties } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js";
import { useBookingTicket } from "../../../ticketBooking/hooks/pisellos/bookingTicket.js";
import SearchSection from "../../../../pro/comprehensiveSearch/components/searchSection/index.js";
import CartClientCard from "../../../booking/info2/cartClientCard/index.js";
import React, { useEffect, useMemo, useRef, useState } from "react";
import { locales } from "@pisell/utils";
import { Icon, useToast } from "@pisell/materials";
import { useDebounceFn } from "ahooks";
import "./index.less";
//#region src/components/checkout/components/SearchAndClientModule/index.tsx
const _excluded = ["customer"];
const SearchAndClientModule = (props) => {
	const { disabledSwitch, onSearch, onClientChange } = props;
	const [value, setValue] = useState("");
	const [loading, setLoading] = useState(false);
	const clientCardRef = useRef(null);
	const bookingTicket = useBookingTicket();
	const handleSearch = useDebounceFn(function() {
		var _ref = _asyncToGenerator(function* (keyword) {
			if (!!keyword) {
				setLoading(true);
				try {
					const res = yield onSearch === null || onSearch === void 0 ? void 0 : onSearch(keyword);
					if (!(res === null || res === void 0 ? void 0 : res.data) || Array.isArray(res.data) && res.data.length === 0) {
						var _Toast$info;
						Toast === null || Toast === void 0 || (_Toast$info = Toast.info) === null || _Toast$info === void 0 || _Toast$info.call(Toast, locales.getText("pisell2.checkout.wallet-pass-module.no-result"));
						return;
					}
					if ("type" in res && res.type == "walletCode") {
						var _clientCardRef$curren, _clientCardRef$curren2;
						const _res$data = res.data, { customer } = _res$data, rest = _objectWithoutProperties(_res$data, _excluded);
						if (!customer) {
							var _Toast$info2;
							Toast === null || Toast === void 0 || (_Toast$info2 = Toast.info) === null || _Toast$info2 === void 0 || _Toast$info2.call(Toast, locales.getText("pisell2.checkout.wallet-pass-module.no-result"));
							return;
						}
						let _customer = _objectSpread2(_objectSpread2({}, customer), {}, { latest_wallet_detail: rest });
						(_clientCardRef$curren = clientCardRef.current) === null || _clientCardRef$curren === void 0 || (_clientCardRef$curren2 = _clientCardRef$curren.handleSetClient) === null || _clientCardRef$curren2 === void 0 || _clientCardRef$curren2.call(_clientCardRef$curren, _customer, { sameClientTip: locales.getText("pisell2.checkout.client-card.tip.same-client") });
					}
					setValue("");
				} catch (error) {} finally {
					setLoading(false);
				}
			}
		});
		return function(_x) {
			return _ref.apply(this, arguments);
		};
	}(), {
		wait: 300,
		leading: true
	});
	const uniqueId = () => {
		return Date.now().toString().slice(-4);
	};
	useEffect(() => {
		const actions = bookingTicket === null || bookingTicket === void 0 ? void 0 : bookingTicket.scanUniversalListener((data) => {
			console.log("handle scan universal resp", data);
			if (data.status === "failed") {
				Toast.fail(locales.getText("pisell2.text.scan-global-failed"));
				return;
			}
			if (data.scanCode) {
				setValue(data.scanCode);
				setTimeout(() => {
					handleSearch.run(data.scanCode);
				});
			}
		}, `wallet_scan_${uniqueId()}`);
		return () => {
			var _actions$remove;
			return actions === null || actions === void 0 || (_actions$remove = actions.remove) === null || _actions$remove === void 0 ? void 0 : _actions$remove.call(actions);
		};
	}, [bookingTicket, handleSearch]);
	const Toast = useToast();
	const dropdownProps = useMemo(() => {
		return { buttonProps: {
			icon: /* @__PURE__ */ React.createElement(Icon, {
				type: "pisell2-switch-horizontal-01",
				size: 24,
				style: { color: "#667085" }
			}),
			size: "default"
		} };
	}, [Toast, disabledSwitch]);
	return /* @__PURE__ */ React.createElement("div", { style: {
		display: "flex",
		gap: 8
	} }, /* @__PURE__ */ React.createElement(SearchSection, {
		value,
		onChange: setValue,
		loading,
		style: { flex: 1 },
		placeholder: locales.getText("pisell2.checkout.search-section.placeholder"),
		searchButtonProps: {
			placement: "inside",
			style: {
				height: 36,
				padding: 8,
				minWidth: 76
			}
		},
		cameraButtonProps: { icon: ["pisell2-camera-01", "pisell2-qr-code-02"] },
		onSearch: () => handleSearch.run(value)
	}), /* @__PURE__ */ React.createElement(CartClientCard, {
		ref: clientCardRef,
		onClientChange: (client) => {
			onClientChange === null || onClientChange === void 0 || onClientChange();
		},
		clientCardProps: {
			style: {
				width: 326,
				minHeight: 48,
				height: 48,
				padding: "4px 8px",
				borderColor: "#D0D5DD"
			},
			avatar: { size: "sm" },
			dropdown: dropdownProps
		}
	}));
};
//#endregion
export { SearchAndClientModule as default };
