const require_runtime = require("../../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
let src_components_pay_toC_PaymentList_payment_visa_png = require("./payment/visa.png");
src_components_pay_toC_PaymentList_payment_visa_png = require_runtime.__toESM(src_components_pay_toC_PaymentList_payment_visa_png);
let src_components_pay_toC_PaymentList_payment_credit_card_png = require("./payment/credit_card.png");
src_components_pay_toC_PaymentList_payment_credit_card_png = require_runtime.__toESM(src_components_pay_toC_PaymentList_payment_credit_card_png);
//#region src/components/pay/toC/PaymentList/utils.ts
/**
* @description: visa/master手续费是否一致
* @param {any} creditCardConfig
* @return {*}
*/
const isCreditCardSurchargeSame = (creditCardConfig) => {
	try {
		const configMap = {};
		creditCardConfig.forEach((config) => {
			const key = `${(config === null || config === void 0 ? void 0 : config.credit_card_type) || "-"}_${(config === null || config === void 0 ? void 0 : config.area) || "-"}`;
			configMap[key] = config;
		});
		const visaDomestic = configMap["visa_domestic"];
		const visaWorld = configMap["visa_world"];
		const masterDomestic = configMap["master_domestic"];
		const masterWorld = configMap["master_world"];
		const formatFee = (config) => {
			var _ref, _config$percent, _config$amount;
			if (!config) return null;
			return {
				percentStr: parseFloat((_ref = (_config$percent = config.percent) !== null && _config$percent !== void 0 ? _config$percent : config.percentage) !== null && _ref !== void 0 ? _ref : "0"),
				amountStr: parseFloat((_config$amount = config.amount) !== null && _config$amount !== void 0 ? _config$amount : "0")
			};
		};
		const visaDomesticFee = formatFee(visaDomestic);
		const visaWorldFee = formatFee(visaWorld);
		const masterDomesticFee = formatFee(masterDomestic);
		const masterWorldFee = formatFee(masterWorld);
		const isFeeSame = (fee1, fee2) => {
			if (!fee1 || !fee2) return false;
			return fee1.percentStr === fee2.percentStr && fee1.amountStr === fee2.amountStr;
		};
		const isDomesticSame = isFeeSame(visaDomesticFee, masterDomesticFee);
		const isWorldSame = isFeeSame(visaWorldFee, masterWorldFee);
		return isDomesticSame && isWorldSame;
	} catch (error) {
		return false;
	}
};
/**
* @description: 计算手续费 百分比 * amount + 固定金额
* @param {any} config
* @param {number} amount
* @return {*}
*/
const totalFee = (config, amount) => {
	var _config$percent2;
	return parseFloat((_config$percent2 = config.percent) !== null && _config$percent2 !== void 0 ? _config$percent2 : config.percentage) * Number(amount) + parseFloat(config.amount);
};
/**
* @description: 格式化visa/master手续费
* @param {any} item 支付列表项
* @param {number} amount 订单金额
* @param {boolean} isSame 手续费是否一致
* @return {*}
*/
const formatCreditCardSurcharge = (item, amount, isSame) => {
	try {
		const { surcharge_config, currency_symbol } = item;
		const formatSurchargeList = (list) => {
			return list === null || list === void 0 ? void 0 : list.map((config) => {
				var _ref2, _config$percent3;
				const percent = (parseFloat((_ref2 = (_config$percent3 = config === null || config === void 0 ? void 0 : config.percent) !== null && _config$percent3 !== void 0 ? _config$percent3 : config === null || config === void 0 ? void 0 : config.percentage) !== null && _ref2 !== void 0 ? _ref2 : 0) * 100).toFixed(2);
				const amountFee = parseFloat(config === null || config === void 0 ? void 0 : config.amount).toFixed(2);
				const _totalFee = totalFee(config, amount).toFixed(2);
				const areaText = (config === null || config === void 0 ? void 0 : config.area) === "world" ? "tocPay.text.rest-of-world" : "tocPay.text.domestic";
				return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, config), {}, {
					area_text: areaText,
					surcharge_text: `${percent}% + ${currency_symbol}${amountFee}`,
					surcharge_fee: `${currency_symbol}${_totalFee}`
				});
			});
		};
		if (isSame) {
			const visaResult = surcharge_config.filter((config) => {
				return config.credit_card_type === "visa";
			});
			return [{
				icons: [src_components_pay_toC_PaymentList_payment_visa_png.default, src_components_pay_toC_PaymentList_payment_credit_card_png.default],
				name: "Visa / Mastercard",
				lists: formatSurchargeList(visaResult)
			}];
		} else {
			const visaResult = surcharge_config.filter((config) => {
				return config.credit_card_type === "visa";
			});
			const masterResult = surcharge_config.filter((config) => {
				return config.credit_card_type === "master";
			});
			return [{
				icons: [src_components_pay_toC_PaymentList_payment_visa_png.default],
				name: "Visa",
				lists: formatSurchargeList(visaResult)
			}, {
				icons: [src_components_pay_toC_PaymentList_payment_credit_card_png.default],
				name: "Mastercard",
				lists: formatSurchargeList(masterResult)
			}];
		}
	} catch (error) {
		console.error(error);
		return [];
	}
};
/**
* @description: 格式化visa/master手续费列表
* @param {any} item
* @param {number} amount
* @return {*}
*/
const formatCreditCardSurchargeList = (item, amount) => {
	const isSame = isCreditCardSurchargeSame((item === null || item === void 0 ? void 0 : item.surcharge_config) || []);
	return formatCreditCardSurcharge(item || {}, amount, isSame);
};
/**
* @description: 格式化visa/master手续费文案，只显示百分比区间
* @return {*}
*/
const formatCreditCardSurchargeText = (surcharge_config, surcharge_text) => {
	try {
		if (!Array.isArray(surcharge_config) || surcharge_config.length === 0) return surcharge_text;
		const percentList = surcharge_config.map((config) => {
			var _ref3, _config$percent4;
			const percent = parseFloat((_ref3 = (_config$percent4 = config.percent) !== null && _config$percent4 !== void 0 ? _config$percent4 : config.percentage) !== null && _ref3 !== void 0 ? _ref3 : "0") * 100;
			return Number(percent.toFixed(2));
		});
		const minPercent = Math.min(...percentList);
		const maxPercent = Math.max(...percentList);
		if (minPercent === maxPercent) return `${surcharge_text}${minPercent}%`;
		return `${surcharge_text}${minPercent}% ~ ${maxPercent}%`;
	} catch (error) {
		return surcharge_text;
	}
};
/**
* @title: 计算支付列表的手续费
* @description:
* @param {any} paymentList
* @param {number} amount
* @return {*}
* @Author: zhiwei.Wang
* @Date: 2024-08-18 09:30
*/
const calculateSurcharge = (paymentList, amount) => {
	return paymentList.map((payment) => {
		const { surcharge_config, currency_symbol, surcharge_text } = payment;
		if (!surcharge_config || surcharge_config.length === 0) {
			payment.surcharge = "";
			return payment;
		}
		const fees = surcharge_config.map((config) => {
			return totalFee(config, amount);
		});
		const minFee = Math.min(...fees);
		const maxFee = Math.max(...fees);
		if (minFee === 0 && maxFee === 0) payment.surcharge = "";
		else if (minFee === maxFee) payment.surcharge = `${surcharge_text}${currency_symbol}${minFee.toFixed(2)}`;
		else payment.surcharge = `${surcharge_text}${currency_symbol}${minFee.toFixed(2)} ~ ${currency_symbol}${maxFee.toFixed(2)}`;
		if (payment.service === "mw.credit_card") {
			payment.surcharge = formatCreditCardSurchargeText(surcharge_config, surcharge_text);
			payment.surcharge_config_result = formatCreditCardSurchargeList(payment, amount);
		}
		return payment;
	});
};
//#endregion
exports.calculateSurcharge = calculateSurcharge;
exports.formatCreditCardSurchargeList = formatCreditCardSurchargeList;
exports.formatCreditCardSurchargeText = formatCreditCardSurchargeText;
