const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_status = require("./status.js");
let dayjs = require("dayjs");
dayjs = require_runtime.__toESM(dayjs);
let _pisell_utils = require("@pisell/utils");
require("decimal.js");
//#region src/components/pay/toC/utils.ts
/**
* 将walletPass列表数据转换为prepare_payments需要的数据格式
* @param list
*/
const formatWalletPassList2PreparePayments = (list) => {
	return (list || []).map((item) => {
		return {
			voucher_id: item.id || 0,
			amount: Number(getAvailableMaxAmount(item)) || 0,
			tag: item.tag || ""
		};
	});
};
/**
*  walletPass支付params
* @param payGroup
*/
const formatWalletPassParams = (payGroup) => {
	const { order_id, payment_group_id, walletPass } = payGroup;
	return {
		order_id,
		payment_group_id,
		payments: (walletPass === null || walletPass === void 0 ? void 0 : walletPass.cards.map((item) => {
			var _item$metadata;
			return {
				amount: getAvailableMaxAmount(item),
				wallet_pass_use_value: item.recommended_usage_par_value,
				voucher_id: item.id,
				tag: item.tag,
				wallet_pass_usage_unit: item.wallet_pass_usage_unit,
				metadata: { unique_payment_number: (_item$metadata = item.metadata) === null || _item$metadata === void 0 ? void 0 : _item$metadata.unique_payment_number }
			};
		})) || []
	};
};
/**
* @title: 获取是否walletPass和三方支付的组合支付
* @description: 三方支付 (stripe  支付宝 微信  苹果 谷歌)
* @return: *
* @Author: shengjie.zuo
* @Date: 2024-08-30 10:06:17
*/
const getIsWalletPassAndTripartitePayment = (payGroup) => {
	var _payGroup$walletPass, _payGroup$otherPaymen;
	return (payGroup === null || payGroup === void 0 || (_payGroup$walletPass = payGroup.walletPass) === null || _payGroup$walletPass === void 0 || (_payGroup$walletPass = _payGroup$walletPass.cards) === null || _payGroup$walletPass === void 0 ? void 0 : _payGroup$walletPass.length) && require_status.isTripartitePayment(payGroup === null || payGroup === void 0 || (_payGroup$otherPaymen = payGroup.otherPayment) === null || _payGroup$otherPaymen === void 0 ? void 0 : _payGroup$otherPaymen.service);
};
const isFranchiseeShop = (path, history) => {
	const { pathname } = history.location;
	const _pathname = path || pathname;
	return _pathname && _pathname.includes("/franchisee");
};
/**
* @title: 获取当前连锁加盟店铺shopid
* @description:
* @param {any} param1
* @return {*}
* @Author: chensheng.gu
* @Date: 2024-08-07 10:04
*/
const getFranchiseeShopId = (path, history) => {
	const { pathname } = history.location;
	const match = (path || pathname).match(/\/shop_(\d+)\//);
	const shopNumber = match ? match[1] : null;
	return Number(shopNumber);
};
const formatFranchiseeRoute = (url, history) => {
	if (isFranchiseeShop(null, history)) return `/franchisee/shop/shop_${getFranchiseeShopId(null, history)}${url}`;
	return url;
};
const getAvailableMaxAmount = (data) => {
	if ((0, _pisell_utils.isNumber)(data._available_max_amount)) return data._available_max_amount;
	return data.tag !== "point_card" ? data.available_max_amount : data === null || data === void 0 ? void 0 : data.recommended_usage_amount;
};
const clientPayment = (params) => {
	var _interaction$utils, _interaction$utils$po;
	const { interaction, type, order_id, values } = params;
	interaction === null || interaction === void 0 || (_interaction$utils = interaction.utils) === null || _interaction$utils === void 0 || (_interaction$utils$po = _interaction$utils.postMessageToApp) === null || _interaction$utils$po === void 0 || _interaction$utils$po.call(_interaction$utils, {
		module: "global",
		key: "paymentClient",
		data: require_objectSpread2._objectSpread2({
			type,
			order_id
		}, values)
	});
};
/**
* 获取唯一幂等token
* @returns
*/
const getUniqueIdempotencyToken = () => {
	return `${(0, dayjs.default)().format("YYYYMMDDHHmmssSSS")}-${(0, _pisell_utils.getUniqueId)()}`;
};
//#endregion
exports.clientPayment = clientPayment;
exports.formatFranchiseeRoute = formatFranchiseeRoute;
exports.formatWalletPassList2PreparePayments = formatWalletPassList2PreparePayments;
exports.formatWalletPassParams = formatWalletPassParams;
exports.getAvailableMaxAmount = getAvailableMaxAmount;
exports.getIsWalletPassAndTripartitePayment = getIsWalletPassAndTripartitePayment;
exports.getUniqueIdempotencyToken = getUniqueIdempotencyToken;
