const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_objectWithoutProperties = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js");
const require_const = require("../const.js");
const require_helper = require("../helper.js");
const require_index = require("../store/index.js");
const require_index$1 = require("../component/step/index.js");
const require_hooks = require("../hooks.js");
const require_payLog = require("../utils/payLog.js");
const require_hooks$1 = require("./hooks.js");
const require_payment = require("./payment.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let _pisell_utils = require("@pisell/utils");
//#region src/components/eftposPay/payo/config.tsx
const _excluded = ["key"];
const POS = ({ api, onChange }) => {
	const { steps, custom } = require_index.useContextData();
	const { deviceRef, payRef, numRef, statusRef, modeRef, netRef, orderIdRef, amountRef, eftposRef, dataRef } = require_hooks.useStoreRef();
	const dispatch = require_index.useDispatch();
	const _steps$ = steps[0], { key } = _steps$, props = require_objectWithoutProperties._objectWithoutProperties(_steps$, _excluded);
	const fail = require_hooks.useFail(0);
	const failRef = (0, react.useRef)(fail);
	const controllerRef = (0, react.useRef)();
	const clearRef = (0, react.useRef)(null);
	const initRef = (0, react.useRef)(false);
	const unloadRef = (0, react.useRef)(false);
	const [check] = require_hooks$1.useValidate(1e3 * 60);
	const { run: paymentRun, clean: paymentClean } = require_payment.usePayment({
		payApi: api.pay,
		checkApi: api.check,
		shouldContinue: () => {
			return !(unloadRef.current || !netRef.current);
		}
	});
	(0, react.useEffect)(() => {
		failRef.current = fail;
	}, [fail]);
	/**
	* @title: 支付,退款,查询 成功 后续逻辑处理
	* @description:
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-01-30 16:34
	*/
	const success = (res) => {
		var _res$data, _res$data2, _res$data3, _res$data4;
		let receipt = Array.isArray(res === null || res === void 0 || (_res$data = res.data) === null || _res$data === void 0 ? void 0 : _res$data.receipt) ? res === null || res === void 0 || (_res$data2 = res.data) === null || _res$data2 === void 0 ? void 0 : _res$data2.receipt : [res === null || res === void 0 || (_res$data3 = res.data) === null || _res$data3 === void 0 ? void 0 : _res$data3.receipt];
		receipt = receipt.filter((item) => typeof item === "string").filter(Boolean);
		dispatch(require_index.updateNextStep());
		dispatch(require_index.updateCustom({
			card_reader_surcharge: (res === null || res === void 0 || (_res$data4 = res.data) === null || _res$data4 === void 0 ? void 0 : _res$data4.card_reader_surcharge) || 0,
			receipt
		}));
		clearRef.current = setTimeout(() => {
			var _res$data5, _deviceRef$current;
			onChange("success", require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, require_helper.getPayParams({
				mode: modeRef.current,
				amount: amountRef.current,
				order_id: orderIdRef.current,
				number: numRef.current,
				device: deviceRef.current,
				pay: payRef.current,
				custom: {
					card_reader_surcharge: (res === null || res === void 0 || (_res$data5 = res.data) === null || _res$data5 === void 0 ? void 0 : _res$data5.card_reader_surcharge) || 0,
					receipt
				}
			}, "success")), {}, { receipt: ((_deviceRef$current = deviceRef.current) === null || _deviceRef$current === void 0 || (_deviceRef$current = _deviceRef$current.setting) === null || _deviceRef$current === void 0 ? void 0 : _deviceRef$current.receipts) ? [] : receipt }));
		}, 1e3);
	};
	/**
	* @title: 检查支付状态
	* @Author: hongbing.wang
	* @Date: 2024-08-20 11:53
	*/
	const checkApi = () => {
		check(api.check, numRef.current).then((res) => {
			if (unloadRef.current || !netRef.current) return;
			if (res && `${res.code}` === "200") {
				success(res);
				return;
			}
			failRef.current(res, true);
		}, (res) => {
			if (unloadRef.current || !netRef.current) return;
			failRef.current(res, true);
		});
	};
	/**
	* @title: 支付,退款,查询 初始化逻辑处理
	* @Author: hongbing.wang
	* @Date: 2024-08-12 10:14
	*/
	(0, react.useEffect)(() => {
		var _deviceRef$current2, _deviceRef$current3;
		if (statusRef.current === require_const.StatusEnum.Success || statusRef.current === require_const.StatusEnum.Fail) return;
		if (initRef.current) return;
		initRef.current = true;
		if (numRef.current) {
			checkApi();
			return;
		}
		numRef.current = require_helper.getUuid();
		dispatch(require_index.updateState({ number: numRef.current }));
		controllerRef.current = new AbortController();
		const isPay = modeRef.current === require_const.ModeEnum.Pay || modeRef.current === require_const.ModeEnum.FullPay;
		const apiParams = isPay ? require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, (custom === null || custom === void 0 ? void 0 : custom.platform) ? {
			operator_id: custom.operator_id,
			operator_type: custom.operator_type,
			platform: custom.platform,
			custom_payment_id: custom.custom_payment_id
		} : {}), {}, {
			original_amount: payRef.current.amount,
			service_charge: {
				amount: payRef.current.surMoney,
				percentage: payRef.current.surPercentRate
			},
			order_id: orderIdRef.current,
			amount: payRef.current.total,
			card_reader_type: eftposRef.current,
			card_reader_id: (_deviceRef$current2 = deviceRef.current) === null || _deviceRef$current2 === void 0 ? void 0 : _deviceRef$current2.id,
			number: numRef.current,
			pay_param_base64: encodeURIComponent(btoa(encodeURIComponent(JSON.stringify(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, dataRef.current), {}, { number: numRef.current })))))
		}) : {
			order_id: orderIdRef.current,
			amount: amountRef.current,
			card_reader_type: eftposRef.current,
			card_reader_id: (_deviceRef$current3 = deviceRef.current) === null || _deviceRef$current3 === void 0 ? void 0 : _deviceRef$current3.id,
			number: numRef.current,
			original_payment_number: custom === null || custom === void 0 ? void 0 : custom.original_payment_number,
			pay_param_base64: encodeURIComponent(btoa(encodeURIComponent(JSON.stringify(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, dataRef.current), {}, { number: numRef.current })))))
		};
		require_payLog.default.setContext({ transactionNumber: numRef.current });
		if (isPay) {
			var _deviceRef$current4;
			require_payLog.default.log("Payo：发起支付", {
				isPay,
				number: numRef.current
			});
			paymentRun({
				paymentType: require_payment.ActionType.Pay,
				paymentNum: numRef.current,
				orderId: orderIdRef.current,
				deviceId: (_deviceRef$current4 = deviceRef.current) === null || _deviceRef$current4 === void 0 ? void 0 : _deviceRef$current4.id,
				params: apiParams,
				callbacks: {
					onSuccess: (res) => {
						require_payLog.default.log("Payo：支付成功", { code: res === null || res === void 0 ? void 0 : res.code });
						success(res);
					},
					onError: (err) => {
						require_payLog.default.log("Payo：支付失败", { code: err === null || err === void 0 ? void 0 : err.code });
						failRef.current(err, true);
					}
				}
			});
			return;
		} else {
			require_payLog.default.log("Payo：发起退款", {
				isPay,
				number: numRef.current
			});
			api.refund(apiParams, { signal: controllerRef.current.signal }).then((res) => {
				console.log("netRef.current111 = ", netRef.current);
				if (unloadRef.current || !netRef.current) {
					require_payLog.default.log("Payo：退款拦截", {
						isUnload: unloadRef.current,
						isNet: netRef.current
					});
					return;
				}
				if (res && `${res.code}` === "200") {
					require_payLog.default.log("Payo：退款成功", { res: JSON.stringify(res) });
					success(res);
					return;
				}
				return Promise.reject(res);
			}).catch((err) => {
				if (unloadRef.current || !netRef.current) {
					require_payLog.default.log("Payo：退款失败拦截", {
						isUnload: unloadRef.current,
						isNet: netRef.current
					});
					return;
				}
				if (!(err === null || err === void 0 ? void 0 : err.code) || `${err.code}` === require_const.PayStatus.Timeout) {
					require_payLog.default.log("Payo：退款超时，转查询");
					checkApi();
					return;
				}
				require_payLog.default.log("Payo：退款失败", {
					code: err === null || err === void 0 ? void 0 : err.code,
					message: err === null || err === void 0 ? void 0 : err.message
				});
				failRef.current(err, true);
			});
		}
	}, [
		api,
		check,
		dispatch,
		onChange
	]);
	(0, react.useEffect)(() => () => {
		var _controllerRef$curren;
		(_controllerRef$curren = controllerRef.current) === null || _controllerRef$curren === void 0 || _controllerRef$curren.abort();
		unloadRef.current = true;
		if (clearRef.current) clearTimeout(clearRef.current);
		paymentClean();
	}, []);
	return /* @__PURE__ */ react.default.createElement(require_index$1.default, props);
};
const Action = ({ onChange }) => {
	const dispatch = require_index.useDispatch();
	const { mode, client, order_id, number, device } = require_index.useContextData();
	const [isShowCancel, setIsShowCancel] = (0, react.useState)(false);
	(0, react.useEffect)(() => {
		const timer = setTimeout(() => {
			setIsShowCancel(true);
			require_payLog.default.log("Payo：30秒后显示取消按钮");
		}, 30 * 1e3);
		return () => {
			clearTimeout(timer);
		};
	}, []);
	/**
	* @title: 前往手动标记页
	* @Author: hongbing.wang
	* @Date: 2024-08-20 12:18
	*/
	const onManually = () => {
		require_payLog.default.log("Payo：点击手动标记");
		dispatch(require_index.updateState({ manual: {
			title: _pisell_utils.locales.getText("page.eftpos-pay.text.request.title")(mode),
			text: _pisell_utils.locales.getText("page.eftpos-pay.text.request.text")(mode)
		} }));
		dispatch(require_index.updateComponent("Manual", true));
	};
	const onCancel = () => {
		require_payLog.default.log("Payo：点击取消");
		onChange("fail", {
			number,
			uniquePaymentNumber: number,
			device,
			order_id
		});
	};
	return /* @__PURE__ */ react.default.createElement("div", { className: "pisell-lowcode__eft-pay-step-item-btns" }, client !== require_const.ClientEnum.Merchant && isShowCancel && /* @__PURE__ */ react.default.createElement(antd.Button, {
		block: true,
		className: "pisell-lowcode__eft-pay-step-item-btn",
		onClick: onCancel
	}, _pisell_utils.locales.getText("page.eftpos-pay.text.cancel")), client === require_const.ClientEnum.Merchant ? /* @__PURE__ */ react.default.createElement(antd.Button, {
		block: true,
		className: "pisell-lowcode__eft-pay-step-item-btn",
		onClick: onManually
	}, _pisell_utils.locales.getText("page.eftpos-pay.text.manual.marking")) : null);
};
//#endregion
exports.Action = Action;
exports.POS = POS;
