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_serve = require("./serve.js");
const require_3dsPayment = require("./3dsPayment.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_utils = require("@pisell/utils");
let _pisell_materials = require("@pisell/materials");
require("./index.less");
//#region src/components/pay/toC/PaymentMethods/components/MiniProgramWaitingPaymentModal/index.tsx
const maxRetryCount = 200;
/**
* 微信小程序内 H5：三方支付需复制链接到外部浏览器时的全屏等待页（PisellContainer + 移动端 Modal）
*/
const MiniProgramWaitingPaymentModal = (props, ref) => {
	var _context$appHelper, _utils$getStore;
	const { onAnotherPayment } = props;
	const { state, dispatch } = (0, react.useContext)(require_model.Context);
	const [open, setOpen] = (0, react.useState)(false);
	const [orderId, setOrderId] = (0, react.useState)("");
	const [paymentUrl, setPaymentUrl] = (0, react.useState)("");
	const [whyDetailOpen, setWhyDetailOpen] = (0, react.useState)(false);
	const context = require_useEngineContext.default();
	const utils = context.appHelper.utils || {};
	const { config } = ((_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.constants) || {};
	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 refreshNumber = (0, react.useRef)(0);
	const isPolling = (0, react.useRef)(true);
	const queryPaymentResult = (0, react.useCallback)(function() {
		var _ref = require_asyncToGenerator._asyncToGenerator(function* (orderId, paymentNumber) {
			if (!isPolling.current) return;
			try {
				const res = yield require_serve.getPaymentResult({
					orderId,
					paymentNumber
				});
				if ((res === null || res === void 0 ? void 0 : res.payment_status) === "paid") {
					var _state$payCallback;
					(_state$payCallback = state.payCallback) === null || _state$payCallback === void 0 || _state$payCallback.call(state, { key: "success" });
					refreshNumber.current = 0;
					return;
				}
				if (refreshNumber.current >= maxRetryCount) {
					var _state$payCallback2;
					(_state$payCallback2 = state.payCallback) === null || _state$payCallback2 === void 0 || _state$payCallback2.call(state, { key: "fail" });
					return;
				}
				refreshNumber.current++;
				setTimeout(() => {
					queryPaymentResult(orderId, paymentNumber);
				}, 3e3);
			} catch (err) {
				console.log("miniProgramQueryPaymentResult_error", err);
				setTimeout(() => {
					queryPaymentResult(orderId, paymentNumber);
				}, 3e3);
			}
		});
		return function(_x, _x2) {
			return _ref.apply(this, arguments);
		};
	}(), [state.payCallback]);
	const getStashDataResult = (0, react.useCallback)(function() {
		var _ref2 = require_asyncToGenerator._asyncToGenerator(function* (orderId) {
			if (!isPolling.current) return;
			try {
				const { data } = yield require_serve.getStashData(require_payGroup.buildPayGroupStashToken(orderId));
				if (data === null || data === void 0 ? void 0 : data.pay_number) {
					refreshNumber.current = 0;
					queryPaymentResult(orderId, data.pay_number);
					return;
				}
				setTimeout(() => {
					getStashDataResult(orderId);
				}, 3e3);
			} catch (err) {
				console.log("miniProgramGetStashDataResult_error", err);
				setTimeout(() => {
					getStashDataResult(orderId);
				}, 3e3);
			}
		});
		return function(_x3) {
			return _ref2.apply(this, arguments);
		};
	}(), [queryPaymentResult]);
	const startPaymentPolling = (0, react.useCallback)((orderId) => {
		const payGroup = require_payGroup.getPayGroupFromStorage(orderId);
		const payNumber = payGroup === null || payGroup === void 0 ? void 0 : payGroup.pay_number;
		if (payNumber) queryPaymentResult(orderId, payNumber);
		else getStashDataResult(orderId);
	}, [queryPaymentResult, getStashDataResult]);
	const init = (0, react.useCallback)(function() {
		var _ref3 = require_asyncToGenerator._asyncToGenerator(function* (val) {
			if (!!(val === null || val === void 0 ? void 0 : val.open)) {
				isPolling.current = true;
				refreshNumber.current = 0;
				setOpen(!!val.open);
				setPaymentUrl(val.paymentUrl || "");
				setOrderId(val.orderId || "");
				const orderId = val.orderId;
				if (orderId) startPaymentPolling(orderId);
			}
		});
		return function(_x4) {
			return _ref3.apply(this, arguments);
		};
	}(), [startPaymentPolling]);
	const handleCopy = (0, react.useCallback)(require_asyncToGenerator._asyncToGenerator(function* () {
		if (!paymentUrl) return;
		try {
			var _state$miniProgramPay;
			let resultUrl = paymentUrl;
			const threeDSPayParams = (_state$miniProgramPay = state.miniProgramPayment) === null || _state$miniProgramPay === void 0 ? void 0 : _state$miniProgramPay.threeDSPayParams;
			if (threeDSPayParams) {
				const res = yield require_3dsPayment.handleMiniProgram3DS(paymentUrl, {
					threeDSPayParams,
					utils,
					locales: _pisell_utils.locales,
					config,
					dvaStore,
					startPaymentPolling
				});
				if (res) resultUrl = res;
			}
			yield (0, _pisell_utils.copyToClipboard)(resultUrl);
			(0, _pisell_materials.PisellToast)({ content: _pisell_utils.locales.getText("tocPay.text.miniProgram.waitingPay.copySuccess") });
		} catch (_unused) {}
	}), [
		paymentUrl,
		state,
		utils,
		config,
		dvaStore,
		startPaymentPolling
	]);
	const handleClose = (0, react.useCallback)(() => {
		isPolling.current = false;
		setOpen(false);
		dispatch({
			type: "setMiniProgramPayment",
			payload: { open: false }
		});
	}, []);
	const goHome = (0, react.useCallback)(() => {
		require_utils.postMessageToMiniProgram("goHome");
	}, []);
	const handleHavePaid = (0, react.useCallback)(() => {
		(0, _pisell_materials.PisellToast)({ content: _pisell_utils.locales.getText("tocPay.text.miniProgram.waitingPay.checkStatus") });
	}, []);
	const handleAnother = (0, react.useCallback)(() => {
		isPolling.current = false;
		(0, _pisell_materials.PisellToast)({
			content: _pisell_utils.locales.getText("tocPay.text.miniProgram.waitingPay.redirect"),
			duration: 2
		});
		setTimeout(() => {
			handleClose();
			onAnotherPayment && (onAnotherPayment === null || onAnotherPayment === void 0 || onAnotherPayment());
		}, 2e3);
	}, []);
	(0, react.useEffect)(() => {
		return () => {
			isPolling.current = false;
		};
	}, []);
	(0, react.useImperativeHandle)(ref, () => ({ init }), [init]);
	const headerRender = (0, react.useMemo)(() => {
		return /* @__PURE__ */ react.default.createElement("div", { className: "mini-program-waiting-pay-header" }, /* @__PURE__ */ react.default.createElement("div", {
			className: "header-icon",
			onClick: goHome
		}, /* @__PURE__ */ react.default.createElement("svg", {
			width: "22",
			height: "22",
			viewBox: "0 0 24 24",
			fill: "none",
			stroke: "currentColor",
			"stroke-width": "2",
			"stroke-linecap": "round",
			"stroke-linejoin": "round"
		}, /* @__PURE__ */ react.default.createElement("path", { d: "M3 12l9-9 9 9M5 10v10h4v-6h6v6h4V10" }))), /* @__PURE__ */ react.default.createElement("span", { className: "header-title" }, _pisell_utils.locales.getText("tocPay.text.miniProgram.waitingPay.title")));
	}, []);
	if (!open) return null;
	return /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellContainer, {
		renderType: "modal",
		props: {
			platform: "h5",
			open,
			footer: false,
			closable: false,
			maskClosable: false,
			bodyStyle: {
				padding: "0 20px",
				height: "100%"
			},
			onCancel: handleClose,
			forceRender: true,
			title: headerRender,
			cancelVisible: false,
			wrapClassName: "mini-program-waiting-pay-modal-wrap"
		}
	}, /* @__PURE__ */ react.default.createElement("div", { className: "mini-program-waiting-pay-content" }, /* @__PURE__ */ react.default.createElement("div", { className: "hero" }, /* @__PURE__ */ react.default.createElement("svg", {
		className: "hero-icon",
		viewBox: "0 0 64 64",
		fill: "none",
		stroke: "currentColor",
		"stroke-width": "3",
		"stroke-linecap": "round",
		"stroke-linejoin": "round"
	}, /* @__PURE__ */ react.default.createElement("rect", {
		x: "8",
		y: "12",
		width: "48",
		height: "40",
		rx: "5"
	}), /* @__PURE__ */ react.default.createElement("line", {
		x1: "8",
		y1: "22",
		x2: "56",
		y2: "22"
	}), /* @__PURE__ */ react.default.createElement("circle", {
		cx: "14",
		cy: "17",
		r: "1.2",
		fill: "currentColor",
		stroke: "none"
	}), /* @__PURE__ */ react.default.createElement("circle", {
		cx: "18",
		cy: "17",
		r: "1.2",
		fill: "currentColor",
		stroke: "none"
	}), /* @__PURE__ */ react.default.createElement("circle", {
		cx: "22",
		cy: "17",
		r: "1.2",
		fill: "currentColor",
		stroke: "none"
	}), /* @__PURE__ */ react.default.createElement("path", { d: "M20 34 L28 42 L44 28" })), /* @__PURE__ */ react.default.createElement("h1", { className: "hero-title" }, _pisell_utils.locales.getText("tocPay.text.miniProgram.waitingPay.finishTip")), /* @__PURE__ */ react.default.createElement("p", { className: "hero-subtitle" }, _pisell_utils.locales.getText("tocPay.text.miniProgram.waitingPay.finishDesc"))), /* @__PURE__ */ react.default.createElement("div", { className: "action-block" }, /* @__PURE__ */ react.default.createElement("div", { className: "link-preview" }, /* @__PURE__ */ react.default.createElement("svg", {
		className: "link-preview-icon",
		viewBox: "0 0 24 24",
		fill: "none",
		stroke: "currentColor",
		"stroke-width": "2",
		"stroke-linecap": "round",
		"stroke-linejoin": "round"
	}, /* @__PURE__ */ react.default.createElement("path", { d: "M10 13a5 5 0 007.54.54l3-3a5 5 0 00-7.07-7.07l-1.72 1.71" }), /* @__PURE__ */ react.default.createElement("path", { d: "M14 11a5 5 0 00-7.54-.54l-3 3a5 5 0 007.07 7.07l1.71-1.71" })), /* @__PURE__ */ react.default.createElement("span", { className: "link-preview-text" }, paymentUrl)), /* @__PURE__ */ react.default.createElement(_pisell_materials.Button, {
		type: "primary",
		onClick: handleCopy,
		className: "btn-primary"
	}, /* @__PURE__ */ react.default.createElement("svg", {
		id: "copyBtnIcon",
		width: "20",
		height: "20",
		viewBox: "0 0 24 24",
		fill: "none",
		stroke: "currentColor",
		"stroke-width": "2",
		"stroke-linecap": "round",
		"stroke-linejoin": "round"
	}, /* @__PURE__ */ react.default.createElement("rect", {
		x: "9",
		y: "9",
		width: "13",
		height: "13",
		rx: "2",
		ry: "2"
	}), /* @__PURE__ */ react.default.createElement("path", { d: "M5 15H4a2 2 0 01-2-2V4a2 2 0 012-2h9a2 2 0 012 2v1" })), /* @__PURE__ */ react.default.createElement("span", null, _pisell_utils.locales.getText("tocPay.text.miniProgram.waitingPay.copy")))), /* @__PURE__ */ react.default.createElement("div", { className: "status-bar-waiting" }, /* @__PURE__ */ react.default.createElement("div", { class: "spinner" }), /* @__PURE__ */ react.default.createElement("span", { className: "status-bar-text" }, _pisell_utils.locales.getText("tocPay.text.miniProgram.waitingPay.statusText"))), /* @__PURE__ */ react.default.createElement("div", { className: "footer-help" }, /* @__PURE__ */ react.default.createElement("div", { className: "footer-help-links" }, /* @__PURE__ */ react.default.createElement("a", { onClick: handleHavePaid }, _pisell_utils.locales.getText("tocPay.text.miniProgram.waitingPay.havePaid")), /* @__PURE__ */ react.default.createElement("a", { onClick: handleAnother }, _pisell_utils.locales.getText("tocPay.text.miniProgram.waitingPay.another"))), /* @__PURE__ */ react.default.createElement("div", { className: "why-toggle" }, /* @__PURE__ */ react.default.createElement("span", {
		className: "why-toggle-link",
		onClick: () => setWhyDetailOpen(!whyDetailOpen)
	}, _pisell_utils.locales.getText("tocPay.text.miniProgram.waitingPay.why")), whyDetailOpen && /* @__PURE__ */ react.default.createElement("div", { className: "why-detail" }, _pisell_utils.locales.getText("tocPay.text.miniProgram.waitingPay.whyDetail"))))));
};
var MiniProgramWaitingPaymentModal_default = (0, react.forwardRef)(MiniProgramWaitingPaymentModal);
//#endregion
exports.default = MiniProgramWaitingPaymentModal_default;
