require("../../../../_virtual/_rolldown/runtime.js");
const require_asyncToGenerator = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_hooks = require("../../hooks.js");
const require_types = require("../../types.js");
let react = require("react");
let _pisell_utils = require("@pisell/utils");
//#region src/components/ecocup/components/SearchBar/hooks.ts
/**
* @description: 搜索客户
*/
const useSearchCustomer = () => {
	const { get } = require_hooks.useRequest();
	const { goalShopId } = require_hooks.useGetGoalShopInfo();
	const run = (text) => {
		if (!text) return;
		return get("/shop/wallet/eco/customer", {
			search: text,
			with: ["tags"],
			goal_shop_id: goalShopId
		});
	};
	return { run };
};
/**
* @description: 搜索杯码
*/
const useSearchCup = () => {
	const { get } = require_hooks.useRequest();
	const { goalShopId } = require_hooks.useGetGoalShopInfo();
	const run = (text) => {
		if (!text) return;
		return get("/shop/wallet/eco", {
			type: "eco",
			code: text,
			with: [
				"customer:id,display_name,cover,phone,country_calling_code,nickname",
				"rentShop:id,name",
				"returnShop:id,name",
				"currentShop:id,name"
			],
			num: 1,
			goal_shop_id: goalShopId
		});
	};
	return { run };
};
/**
* 搜索
*/
const useSearch = () => {
	const searchCustomer = useSearchCustomer();
	const searchCup = useSearchCup();
	const [loading, setLoading] = (0, react.useState)(false);
	return {
		loading,
		run: function() {
			var _ref = require_asyncToGenerator._asyncToGenerator(function* (params) {
				const { searchType, searchText = "" } = params;
				const isCupCode = /^EC[a-zA-Z0-9]{8}$/.test(searchText);
				if (searchType === "cup" && !isCupCode) return {
					status: "failed",
					message: _pisell_utils.locales.getText("pisell2.text.ecocup.search-bar.search-tip")
				};
				const type = !!searchType ? searchType : isCupCode ? "cup" : "customer";
				const runFn = type === "cup" ? searchCup.run : searchCustomer.run;
				try {
					var _res$data;
					setLoading(true);
					const res = yield runFn(searchText);
					if (res === null || res === void 0 || (_res$data = res.data) === null || _res$data === void 0 ? void 0 : _res$data.list) {
						var _res$data2;
						return {
							status: "success",
							type,
							data: (res === null || res === void 0 || (_res$data2 = res.data) === null || _res$data2 === void 0 ? void 0 : _res$data2.list) || []
						};
					} else return {
						status: "failed",
						type,
						message: (res === null || res === void 0 ? void 0 : res.message) || ""
					};
				} catch (error) {
					return {
						status: "failed",
						type,
						message: (error === null || error === void 0 ? void 0 : error.message) || ""
					};
				} finally {
					setLoading(false);
				}
			});
			return function run(_x) {
				return _ref.apply(this, arguments);
			};
		}()
	};
};
/**
* 判定当前杯子的状态
*/
const useCupScanStatus = () => {
	const shopId = require_hooks.useGetShopId();
	const getCupScanStatus = (cup, customerId) => {
		if (!cup) return require_types.ECupScanStatus.NOT_FOUND;
		if ((cup === null || cup === void 0 ? void 0 : cup.status) == require_types.ECupStatus.IN_STOCK) {
			if ((cup === null || cup === void 0 ? void 0 : cup.current_shop_id) != shopId) return require_types.ECupScanStatus.OTHER_IN_STOCK;
		}
		if ((cup === null || cup === void 0 ? void 0 : cup.status) == require_types.ECupStatus.RENT) {
			if ((cup === null || cup === void 0 ? void 0 : cup.customer_id) != customerId) return require_types.ECupScanStatus.OTHER_RENT;
		}
		if ((cup === null || cup === void 0 ? void 0 : cup.status) == require_types.ECupStatus.MISSING) return require_types.ECupScanStatus.MISSING;
		if ((cup === null || cup === void 0 ? void 0 : cup.status) == require_types.ECupStatus.BROKEN) return require_types.ECupScanStatus.BROKEN;
	};
	return { getCupScanStatus };
};
//#endregion
exports.useCupScanStatus = useCupScanStatus;
exports.useSearch = useSearch;
