import { _asyncToGenerator } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import { useGetGoalShopInfo, useGetShopId, useRequest } from "../../hooks.js";
import { ECupScanStatus, ECupStatus } from "../../types.js";
import { useState } from "react";
import { locales } from "@pisell/utils";
//#region src/components/ecocup/components/SearchBar/hooks.ts
/**
* @description: 搜索客户
*/
const useSearchCustomer = () => {
	const { get } = useRequest();
	const { goalShopId } = 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 } = useRequest();
	const { goalShopId } = 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] = useState(false);
	return {
		loading,
		run: function() {
			var _ref = _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: 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 = useGetShopId();
	const getCupScanStatus = (cup, customerId) => {
		if (!cup) return ECupScanStatus.NOT_FOUND;
		if ((cup === null || cup === void 0 ? void 0 : cup.status) == ECupStatus.IN_STOCK) {
			if ((cup === null || cup === void 0 ? void 0 : cup.current_shop_id) != shopId) return ECupScanStatus.OTHER_IN_STOCK;
		}
		if ((cup === null || cup === void 0 ? void 0 : cup.status) == ECupStatus.RENT) {
			if ((cup === null || cup === void 0 ? void 0 : cup.customer_id) != customerId) return ECupScanStatus.OTHER_RENT;
		}
		if ((cup === null || cup === void 0 ? void 0 : cup.status) == ECupStatus.MISSING) return ECupScanStatus.MISSING;
		if ((cup === null || cup === void 0 ? void 0 : cup.status) == ECupStatus.BROKEN) return ECupScanStatus.BROKEN;
	};
	return { getCupScanStatus };
};
//#endregion
export { useCupScanStatus, useSearch };
