import { _asyncToGenerator } from "../../../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import { setPayNumber } from "../../../payGroup.js";
import { fastPayNew } from "../../MWCreditCard/List/serve.js";
import { preCheckEnrollment } from "../../MWCreditCard/tds2.js";
import { getStashData } from "./serve.js";
//#region src/components/pay/toC/PaymentMethods/components/MiniProgramWaitingPaymentModal/3dsPayment.tsx
/**
* 生成 payCode token
*/
function getPayCodeToken(code) {
	return `pay_code_${code.replace(/:/g, "")}`;
}
/**
* 3DS 回调地址拼接
*/
function creditCardPayCallback(res, order_id, config, dvaStore) {
	try {
		var _dvaStore$core;
		if (!(res === null || res === void 0 ? void 0 : res.pay_code) || !(config === null || config === void 0 ? void 0 : config.domain)) return "";
		const locale = (dvaStore === null || dvaStore === void 0 || (_dvaStore$core = dvaStore.core) === null || _dvaStore$core === void 0 ? void 0 : _dvaStore$core.translateLocale) || "en";
		const url = `/${locale}/miniapp-payment/${res.pay_code}?locale=${locale}&order_id=${order_id}`;
		return res.type === "3ds" ? `${config.domain}${url}` : "";
	} catch (err) {
		return "";
	}
}
/**
* 添加卡 + 3DS 验证
*/
function addCard3DS(_x) {
	return _addCard3DS.apply(this, arguments);
}
function _addCard3DS() {
	_addCard3DS = _asyncToGenerator(function* (options) {
		const { threeDSPayParams, utils, locales, config, dvaStore, startPaymentPolling } = options;
		const { order_id, params } = threeDSPayParams || {};
		if (!order_id || !params) return "";
		try {
			var _utils$Toast, _utils$Toast$loading, _utils$Toast2, _utils$Toast2$hide;
			utils === null || utils === void 0 || (_utils$Toast = utils.Toast) === null || _utils$Toast === void 0 || (_utils$Toast$loading = _utils$Toast.loading) === null || _utils$Toast$loading === void 0 || _utils$Toast$loading.call(_utils$Toast, locales.getText("tocPay.text.copyCardInfo"), 0, void 0, true);
			const res = yield preCheckEnrollment(order_id, params);
			utils === null || utils === void 0 || (_utils$Toast2 = utils.Toast) === null || _utils$Toast2 === void 0 || (_utils$Toast2$hide = _utils$Toast2.hide) === null || _utils$Toast2$hide === void 0 || _utils$Toast2$hide.call(_utils$Toast2);
			if (res) {
				var _utils$token, _utils$token$get;
				yield setPayNumber(order_id, res === null || res === void 0 ? void 0 : res.pay_number, utils === null || utils === void 0 || (_utils$token = utils.token) === null || _utils$token === void 0 || (_utils$token$get = _utils$token.get) === null || _utils$token$get === void 0 ? void 0 : _utils$token$get.call(_utils$token));
				startPaymentPolling(order_id);
				return creditCardPayCallback(res, order_id, config, dvaStore);
			}
		} catch (err) {
			var _utils$Toast3, _utils$Toast3$hide;
			utils === null || utils === void 0 || (_utils$Toast3 = utils.Toast) === null || _utils$Toast3 === void 0 || (_utils$Toast3$hide = _utils$Toast3.hide) === null || _utils$Toast3$hide === void 0 || _utils$Toast3$hide.call(_utils$Toast3);
		}
		return "";
	});
	return _addCard3DS.apply(this, arguments);
}
/**
* 快捷支付 3DS
*/
function fastPay3DS(_x2) {
	return _fastPay3DS.apply(this, arguments);
}
function _fastPay3DS() {
	_fastPay3DS = _asyncToGenerator(function* (options) {
		const { threeDSPayParams, utils, locales, config, dvaStore, startPaymentPolling } = options;
		const { order_id } = threeDSPayParams || {};
		if (!order_id) return "";
		try {
			var _utils$Toast4, _utils$Toast4$loading, _utils$Toast5, _utils$Toast5$hide;
			utils === null || utils === void 0 || (_utils$Toast4 = utils.Toast) === null || _utils$Toast4 === void 0 || (_utils$Toast4$loading = _utils$Toast4.loading) === null || _utils$Toast4$loading === void 0 || _utils$Toast4$loading.call(_utils$Toast4, locales.getText("tocPay.text.copyCardInfo"), 0, void 0, true);
			const { data } = yield fastPayNew(threeDSPayParams);
			utils === null || utils === void 0 || (_utils$Toast5 = utils.Toast) === null || _utils$Toast5 === void 0 || (_utils$Toast5$hide = _utils$Toast5.hide) === null || _utils$Toast5$hide === void 0 || _utils$Toast5$hide.call(_utils$Toast5);
			if (data) {
				var _utils$token2, _utils$token2$get;
				yield setPayNumber(order_id, data === null || data === void 0 ? void 0 : data.pay_number, utils === null || utils === void 0 || (_utils$token2 = utils.token) === null || _utils$token2 === void 0 || (_utils$token2$get = _utils$token2.get) === null || _utils$token2$get === void 0 ? void 0 : _utils$token2$get.call(_utils$token2));
				startPaymentPolling(order_id);
				return creditCardPayCallback(data, order_id, config, dvaStore);
			}
		} catch (err) {
			var _utils$Toast6, _utils$Toast6$hide;
			utils === null || utils === void 0 || (_utils$Toast6 = utils.Toast) === null || _utils$Toast6 === void 0 || (_utils$Toast6$hide = _utils$Toast6.hide) === null || _utils$Toast6$hide === void 0 || _utils$Toast6$hide.call(_utils$Toast6);
		}
		return "";
	});
	return _fastPay3DS.apply(this, arguments);
}
/**
* 检查 stash 状态
*/
function checkCoreStash(_x3, _x4, _x5) {
	return _checkCoreStash.apply(this, arguments);
}
function _checkCoreStash() {
	_checkCoreStash = _asyncToGenerator(function* (code, isAddCard, options) {
		try {
			var _data$isUsed, _data$data;
			const { data } = yield getStashData(getPayCodeToken(code));
			if ((_data$isUsed = data === null || data === void 0 ? void 0 : data.isUsed) !== null && _data$isUsed !== void 0 ? _data$isUsed : data === null || data === void 0 || (_data$data = data.data) === null || _data$data === void 0 ? void 0 : _data$data.isUsed) return isAddCard ? yield addCard3DS(options) : yield fastPay3DS(options);
		} catch (err) {}
		return "";
	});
	return _checkCoreStash.apply(this, arguments);
}
/**
* 3DS 主入口（给页面调用的唯一方法）
*/
function handleMiniProgram3DS(_x6, _x7) {
	return _handleMiniProgram3DS.apply(this, arguments);
}
function _handleMiniProgram3DS() {
	_handleMiniProgram3DS = _asyncToGenerator(function* (paymentUrl, options) {
		var _options$threeDSPayPa;
		const payCodeMatch = paymentUrl.match(/pay:\w+/);
		if (!(payCodeMatch === null || payCodeMatch === void 0 ? void 0 : payCodeMatch[0])) return "";
		const isAddCard = (_options$threeDSPayPa = options.threeDSPayParams) === null || _options$threeDSPayPa === void 0 ? void 0 : _options$threeDSPayPa.isAddCard;
		return checkCoreStash(payCodeMatch[0], isAddCard, options);
	});
	return _handleMiniProgram3DS.apply(this, arguments);
}
//#endregion
export { handleMiniProgram3DS };
