import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { isTripartitePayment } from "./status.js";
import dayjs from "dayjs";
import { getUniqueId, isNumber } from "@pisell/utils";
import "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) && 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 === null || history === void 0 ? void 0 : 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;
};
/**
* @title: 支付跳转回调路径
* @description:
* @return {*}
* @Author: chensheng.gu
* @Date: 2024-10-29 15:33
*/
const getReturnUrl = (history) => {
	return `${location.origin}${formatFranchiseeRoute("/payment-finish", history)}`;
};
const getAvailableMaxAmount = (data) => {
	if (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: _objectSpread2({
			type,
			order_id
		}, values)
	});
};
/**
* 获取唯一幂等token
* @returns
*/
const getUniqueIdempotencyToken = () => {
	return `${dayjs().format("YYYYMMDDHHmmssSSS")}-${getUniqueId()}`;
};
const isMiniProgram = (interaction) => {
	try {
		var _interaction$utils2, _interaction$utils2$i, _interaction$utils3, _interaction$utils3$i;
		console.log("isMiniProgram", interaction === null || interaction === void 0 || (_interaction$utils2 = interaction.utils) === null || _interaction$utils2 === void 0 || (_interaction$utils2$i = _interaction$utils2.isMiniProgramEnv) === null || _interaction$utils2$i === void 0 ? void 0 : _interaction$utils2$i.call(_interaction$utils2));
		return interaction === null || interaction === void 0 || (_interaction$utils3 = interaction.utils) === null || _interaction$utils3 === void 0 || (_interaction$utils3$i = _interaction$utils3.isMiniProgramEnv) === null || _interaction$utils3$i === void 0 ? void 0 : _interaction$utils3$i.call(_interaction$utils3);
	} catch (error) {
		console.log("isMiniProgram_error", error);
		return false;
	}
};
/**
* @description: 打开小程序支付弹窗
* @param {any} url
* @return {*}
*/
const openMiniProgramPaymentModal = (url, dispatch) => {
	console.log("openMiniProgramPaymentModal", url);
	dispatch({
		type: "setMiniProgramPayment",
		payload: {
			open: true,
			paymentUrl: url,
			threeDSPayParams: null
		}
	});
};
/**
* @description: 通知小程序
* @return {*}
*/
const postMessageToMiniProgram = (type, params) => {
	try {
		var _wx, _wx2, _wx3;
		console.log("postMessageToMiniProgram", type, params, (_wx = window.wx) === null || _wx === void 0 ? void 0 : _wx.miniProgram);
		(_wx2 = window.wx) === null || _wx2 === void 0 || (_wx2 = _wx2.miniProgram) === null || _wx2 === void 0 || _wx2.postMessage({ data: _objectSpread2({ type }, params || {}) });
		(_wx3 = window.wx) === null || _wx3 === void 0 || (_wx3 = _wx3.miniProgram) === null || _wx3 === void 0 || _wx3.navigateBack({ delta: 1 });
	} catch (error) {
		console.error("postMessageToMiniProgram_error", error);
	}
};
//#endregion
export { clientPayment, formatFranchiseeRoute, formatWalletPassList2PreparePayments, formatWalletPassParams, getAvailableMaxAmount, getIsWalletPassAndTripartitePayment, getReturnUrl, getUniqueIdempotencyToken, isMiniProgram, openMiniProgramPaymentModal, postMessageToMiniProgram };
