const require_runtime = require("../../../../../_virtual/_rolldown/runtime.js");
const require_asyncToGenerator = require("../../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_useEngineContext = require("../../../../../hooks/useEngineContext.js");
const require_utils = require("../../utils.js");
const require_payGroup = require("../../payGroup.js");
const require_model = require("../../model.js");
const require_index = require("../MWCreditCard/TDSPay/ThreedsLoading/index.js");
const require_usePaymentLogger = require("../../../../../hooks/usePaymentLogger.js");
const require_index$1 = require("../hooks/index.js");
const require_index$2 = require("../components/PaymentReturnContent/index.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_utils = require("@pisell/utils");
let _pisell_materials = require("@pisell/materials");
//#region src/components/pay/toC/PaymentMethods/GooglePay/index.tsx
/**
* @title: 谷歌支付
* @description:
* @param {GooglePayProps} props
* @param {React} ref
* @return {*}
* @Author: zhiwei.Wang
* @Date: 2024-08-16 11:01
*/
const GooglePay = (props, ref) => {
	var _context$appHelper, _context$appHelper2, _state$payGroup, _state$payGroup2, _state$payGroup4;
	const { state, dispatch } = (0, react.useContext)(require_model.Context);
	const [open, setOpen] = (0, react.useState)(false);
	const context = require_useEngineContext.default();
	const { config } = ((_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.constants) || {};
	const { interaction, token } = (_context$appHelper2 = context.appHelper) === null || _context$appHelper2 === void 0 ? void 0 : _context$appHelper2.utils;
	const paymentLogger = require_usePaymentLogger.default({
		orderId: ((_state$payGroup = state.payGroup) === null || _state$payGroup === void 0 ? void 0 : _state$payGroup.order_id) || "unknown",
		step: "2"
	});
	const { isReturned, handleRedirect, resetState } = require_index$1.usePaymentRedirect({
		orderId: (_state$payGroup2 = state.payGroup) === null || _state$payGroup2 === void 0 ? void 0 : _state$payGroup2.order_id,
		paymentType: "googlePay"
	});
	/**
	* @title: 初始化
	* @description:
	* @param {any} param1
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-08-16 11:01
	*/
	const init = function() {
		var _ref = require_asyncToGenerator._asyncToGenerator(function* ({ payGroup }) {
			var _utils$getStore, _interaction$utils, _interaction$utils$is, _interaction$utils2, _interaction$utils2$i;
			const isMiniWx = require_utils.isMiniProgram(interaction);
			paymentLogger.addLog({
				key: "GooglePay初始化开始",
				value: ""
			});
			setOpen(isMiniWx ? false : true);
			const utils = context.appHelper.utils || {};
			const dvaStore = utils === null || utils === void 0 || (_utils$getStore = utils.getStore) === null || _utils$getStore === void 0 || (_utils$getStore = _utils$getStore.call(utils)) === null || _utils$getStore === void 0 ? void 0 : _utils$getStore.getState();
			const values = require_payGroup.getPayGroupParamsFromStorage(payGroup.order_id);
			if (!values) {
				paymentLogger.addLog({
					key: "GooglePay获取支付参数失败",
					value: {
						title: `订单${payGroup === null || payGroup === void 0 ? void 0 : payGroup.order_id} 未找到存储的支付参数`,
						data: { payGroup }
					}
				});
				paymentLogger.sendLogs("GooglePay获取支付参数失败");
				setOpen(false);
				return;
			}
			paymentLogger.addLog({
				key: "GooglePay获取支付参数成功",
				value: ""
			});
			if ((interaction === null || interaction === void 0 || (_interaction$utils = interaction.utils) === null || _interaction$utils === void 0 || (_interaction$utils$is = _interaction$utils.isAppEnv) === null || _interaction$utils$is === void 0 ? void 0 : _interaction$utils$is.call(_interaction$utils)) && (interaction === null || interaction === void 0 || (_interaction$utils2 = interaction.utils) === null || _interaction$utils2 === void 0 || (_interaction$utils2$i = _interaction$utils2.isAndroid_ios) === null || _interaction$utils2$i === void 0 ? void 0 : _interaction$utils2$i.call(_interaction$utils2)) === "android") {
				paymentLogger.addLog({
					key: "GooglePay使用Android客户端支付",
					value: ""
				});
				paymentLogger.sendLogs("GooglePay客户端支付");
				return require_utils.clientPayment({
					order_id: payGroup.order_id,
					type: "google_pay",
					values,
					interaction
				});
			}
			try {
				var _dvaStore$core, _dvaStore$core2, _state$payGroup3;
				const locale = ((_dvaStore$core = dvaStore.core) === null || _dvaStore$core === void 0 ? void 0 : _dvaStore$core.translateLocale) || "en";
				const shopId = (_dvaStore$core2 = dvaStore.core) === null || _dvaStore$core2 === void 0 || (_dvaStore$core2 = _dvaStore$core2.core) === null || _dvaStore$core2 === void 0 || (_dvaStore$core2 = _dvaStore$core2.shop) === null || _dvaStore$core2 === void 0 ? void 0 : _dvaStore$core2.id;
				const currentURL = window.location.href;
				const linkElement = document.createElement("a");
				linkElement.href = currentURL;
				const shop_domain = linkElement.hostname;
				const _href = `${config.pay}${require_utils.formatFranchiseeRoute(`/google-pay?order_id=${(_state$payGroup3 = state.payGroup) === null || _state$payGroup3 === void 0 ? void 0 : _state$payGroup3.order_id}&shop_id=${shopId}&locale=${locale}&shop_domain=${shop_domain}&total_amount=${values.amount}&payment_group_id=${values.payment_group_id}&front_order_uuid=${state.front_order_uuid}&is_replace_router=1`, state.history)}`;
				paymentLogger.addLog({
					key: "GooglePay准备跳转",
					value: ""
				});
				paymentLogger.sendLogs("GooglePay跳转成功");
				if (isMiniWx) {
					var _token$get;
					yield require_payGroup.setPayNumber(payGroup === null || payGroup === void 0 ? void 0 : payGroup.order_id, "", token === null || token === void 0 || (_token$get = token.get) === null || _token$get === void 0 ? void 0 : _token$get.call(token));
					require_utils.openMiniProgramPaymentModal(_href, dispatch);
					setOpen(false);
					return;
				}
				yield handleRedirect(_href);
			} catch (err) {
				paymentLogger.addLog({
					key: "GooglePay处理错误",
					value: {
						title: `订单${payGroup === null || payGroup === void 0 ? void 0 : payGroup.order_id} GooglePay处理失败: ${(err === null || err === void 0 ? void 0 : err.message) || "未知错误"}`,
						data: {
							payGroup,
							values
						}
					}
				});
				paymentLogger.sendLogs("GooglePay处理失败", err);
				setOpen(false);
			}
		});
		return function init(_x) {
			return _ref.apply(this, arguments);
		};
	}();
	(0, react.useImperativeHandle)(ref, () => ({ init }));
	const handleReturn = () => {
		setOpen(false);
		resetState();
	};
	return /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellModal, {
		platform: "h5",
		open,
		footer: false,
		header: false,
		closable: false,
		bodyStyle: { padding: 0 }
	}, /* @__PURE__ */ react.default.createElement("div", { className: "" }, !isReturned && /* @__PURE__ */ react.default.createElement(require_index.default, { title: _pisell_utils.locales.getText("tocPay.text.connectingGooglePay") }), isReturned && /* @__PURE__ */ react.default.createElement(require_index$2.default, {
		handleReturn,
		state,
		orderId: (_state$payGroup4 = state.payGroup) === null || _state$payGroup4 === void 0 ? void 0 : _state$payGroup4.order_id,
		unique_payment_number: state.front_order_uuid
	})));
};
var GooglePay_default = (0, react.forwardRef)(GooglePay);
//#endregion
exports.default = GooglePay_default;
