import { _objectSpread2 } from "../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { EftposEnum, ModeEnum } from "./const.js";
import { Decimal as Decimal$1 } from "decimal.js";
//#region src/components/eftposPay/helper.ts
function getUuid() {
	return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxx".replace(/[xy]/g, (c) => {
		const r = Math.random() * 16 | 0;
		return (c === "x" ? r : r & 3 | 8).toString(16);
	});
}
/**
* @title: Promise化 setTimeout
* @param {number} time
* @Author: hongbing.wang
* @Date: 2024-08-20 13:03
*/
const sleep = (time) => new Promise((resove) => {
	setTimeout(() => {
		resove(void 0);
	}, time);
});
/**
* @title: 克隆
* @Author: hongbing.wang
* @Date: 2024-08-20 13:03
*/
function cloneDeep(source, hash = /* @__PURE__ */ new WeakMap()) {
	if (typeof source !== "object" || source === null) return source;
	if (hash.has(source)) return hash.get(source);
	const target = Array.isArray(source) ? [] : {};
	Reflect.ownKeys(source).forEach((key) => {
		const val = source[key];
		if (typeof val === "object" && val != null) target[key] = cloneDeep(val, hash);
		else target[key] = val;
	});
	return target;
}
/**
* @title: 获取支付回调参数
* @Author: hongbing.wang
* @Date: 2024-08-20 20:26
*/
const getPayParams = (data, statue) => {
	var _data$custom;
	if (statue === "fail") return {
		number: data.number,
		device: data.device,
		order_id: data.order_id
	};
	const receipt = (data === null || data === void 0 || (_data$custom = data.custom) === null || _data$custom === void 0 ? void 0 : _data$custom.receipt) || [];
	if (data.mode === ModeEnum.Pay || data.mode === ModeEnum.FullPay) {
		var _data$custom2, _data$device;
		const pay = data.pay;
		const card_reader_surcharge = ((_data$custom2 = data.custom) === null || _data$custom2 === void 0 ? void 0 : _data$custom2.card_reader_surcharge) || 0;
		const obj = pay || {};
		const cardReaderSurcharge = card_reader_surcharge;
		const surCardDeci = new Decimal$1(Number(cardReaderSurcharge));
		const surMoneyDeci = new Decimal$1(Number((obj === null || obj === void 0 ? void 0 : obj.surMoney) || 0));
		const surRateMoneyDeci = new Decimal$1(Number(obj === null || obj === void 0 ? void 0 : obj.surRateMoney));
		const surMoneyTotal = surMoneyDeci.add(surCardDeci);
		const surchargeDeci = surMoneyTotal.add(surRateMoneyDeci);
		const amount = new Decimal$1(Number(obj === null || obj === void 0 ? void 0 : obj.amount));
		const total = surchargeDeci.add(amount);
		return _objectSpread2(_objectSpread2({}, obj), {}, {
			cardReaderSurcharge,
			surMoney: surMoneyTotal.toFixed(2, Decimal$1.ROUND_HALF_UP),
			surcharge: surchargeDeci.toFixed(2, Decimal$1.ROUND_HALF_UP),
			total: total.toFixed(2, Decimal$1.ROUND_HALF_UP),
			number: data.number,
			uniquePaymentNumber: data.number,
			device: data.device,
			order_id: data.order_id,
			receipt: ((_data$device = data.device) === null || _data$device === void 0 || (_data$device = _data$device.setting) === null || _data$device === void 0 ? void 0 : _data$device.receipts) ? [] : receipt
		});
	}
	if (data.mode === ModeEnum.Refund) {
		var _data$device2;
		return {
			amount: data.amount,
			total: data.amount,
			number: data.number,
			device: data.device,
			order_id: data.order_id,
			receipt: ((_data$device2 = data.device) === null || _data$device2 === void 0 || (_data$device2 = _data$device2.setting) === null || _data$device2 === void 0 ? void 0 : _data$device2.receipts) ? [] : receipt
		};
	}
	return {
		amount: data.amount,
		total: data.amount,
		number: data.number,
		order_id: data.order_id
	};
};
const limitDecimals = (value) => {
	const reg = /^(\-)*(\d+)\.(\d\d).*$/;
	if (typeof value === "string") return !isNaN(Number(value)) ? value.replace(reg, "$1$2.$3") : "";
	if (typeof value === "number") return !isNaN(value) ? String(value).replace(reg, "$1$2.$3") : "";
	return "";
};
/**
* @title:  通用获取 金额信息
* @description: 在 amount 页面设置则使用 pay ,如果有 setting 则使用 setting 信息 ,都没有则设置0
* @param {State} store : 全局store数据管理
* @Author: hongbing.wang
* @Date: 2024-08-12 10:24
*/
function getAmonunt(store) {
	const { device, pay, name, symbol, amount, eftpos } = store;
	if (!device) throw Error("device is not defined");
	const setting = device.setting;
	if (pay) return {
		name,
		symbol,
		money: Number(amount),
		amount: Number(pay.amount),
		surRate: Number(pay.surRate),
		surMoney: Number(pay.surMoney),
		surcharge: (setting === null || setting === void 0 ? void 0 : setting.surcharge) || false,
		useOriginSurcharge: (pay === null || pay === void 0 ? void 0 : pay.useOriginSurcharge) || false
	};
	if (!setting) return {
		name,
		symbol,
		money: Number(amount),
		amount: Number(amount),
		surRate: 0,
		surMoney: 0,
		surcharge: false,
		useOriginSurcharge: false
	};
	const useOriginSurcharge = eftpos === EftposEnum.Tyro ? setting.useOriginSurcharge || false : false;
	const surRate = setting.constant_rate || 0;
	const surMoney = setting.constant_money || 0;
	const surcharge = setting.surcharge;
	return {
		name,
		symbol,
		money: Number(amount),
		amount: Number(amount),
		surRate: useOriginSurcharge ? 0 : surcharge ? surRate : 0,
		surMoney: useOriginSurcharge ? 0 : surcharge ? surMoney : 0,
		surcharge,
		useOriginSurcharge
	};
}
function getPayAmonunt({ money, amount, surRate, surMoney }, formatAmount) {
	const surRateDeci = new Decimal$1(surRate);
	const moneyDeci = new Decimal$1(amount);
	const surMoneyDeci = new Decimal$1(surMoney);
	const surRateMoney = moneyDeci.mul(surRateDeci.div(100)).toFixed(2, Decimal$1.ROUND_HALF_UP);
	const totalSurMoney = moneyDeci.mul(surRateDeci.div(100)).add(surMoneyDeci);
	const rate = new Decimal$1(surRate).div(100).toFixed(4, Decimal$1.ROUND_HALF_UP);
	const total = totalSurMoney.add(moneyDeci).toFixed(2, Decimal$1.ROUND_HALF_UP);
	return {
		sourceAmount: `${money}`,
		amount: `${amount}`,
		surcharge: `${totalSurMoney}`,
		total: `${total}`,
		surPercentRate: `${rate}`,
		surRate: `${surRate}`,
		surMoney: `${surMoney}`,
		surRateMoney: `${surRateMoney}`,
		totalUnit: formatAmount(total)
	};
}
function getBrowserVersion() {
	const Sys = {};
	const ua = navigator.userAgent.toLowerCase();
	let s;
	(s = ua.match(/rv:([\d.]+)\) like gecko/)) ? Sys.ie = s[1] : (s = ua.match(/msie ([\d\.]+)/)) ? Sys.ie = s[1] : (s = ua.match(/edge\/([\d\.]+)/)) ? Sys.edge = s[1] : (s = ua.match(/firefox\/([\d\.]+)/)) ? Sys.firefox = s[1] : (s = ua.match(/(?:opera|opr).([\d\.]+)/)) ? Sys.opera = s[1] : (s = ua.match(/chrome\/([\d\.]+)/)) ? Sys.chrome = s[1] : (s = ua.match(/version\/([\d\.]+).*safari/)) && (Sys.safari = s[1]);
	if (Sys.ie) return "IE: " + Sys.ie;
	if (Sys.edge) return "EDGE: " + Sys.edge;
	if (Sys.firefox) return "Firefox: " + Sys.firefox;
	if (Sys.chrome) return "Chrome: " + Sys.chrome;
	if (Sys.opera) return "Opera: " + Sys.opera;
	if (Sys.safari) return "Safari: " + Sys.safari;
	return "Unkonwn";
}
//#endregion
export { cloneDeep, getAmonunt, getBrowserVersion, getPayAmonunt, getPayParams, getUuid, limitDecimals, sleep };
