const require_runtime = require("../../_virtual/_rolldown/runtime.js");
const require_const = require("./const.js");
const require_helper = require("./helper.js");
const require_objectDestructuringEmpty = require("../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectDestructuringEmpty.js");
const require_extends = require("../../_virtual/_@oxc-project_runtime@0.122.0/helpers/extends.js");
const require_index = require("./store/index.js");
const require_payLog = require("./utils/payLog.js");
const require_index$1 = require("./payo/index.js");
const require_windcave = require("./windcave/windcave.js");
const require_index$2 = require("./tyro/index.js");
const require_index$3 = require("./linkly/index.js");
const require_index$4 = require("./mx51/index.js");
const require_index$5 = require("./huifu/index.js");
const require_index$6 = require("./adyenPos/index.js");
const require_index$7 = require("./adyenScan/index.js");
const require_index$8 = require("./component/header/index.js");
const require_index$9 = require("./component/fail/index.js");
const require_failCustom = require("./component/fail/failCustom.js");
const require_network = require("./component/fail/network.js");
const require_unknow = require("./component/fail/unknow.js");
const require_index$10 = require("./component/prompt/index.js");
const require_index$11 = require("./component/manual/index.js");
const require_index$12 = require("./component/signature/index.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_utils = require("@pisell/utils");
require("./index.less");
//#region src/components/eftposPay/pay.tsx
const Mapping = {
	Fail: require_index$9.default,
	Network: require_network.default,
	Manual: require_index$11.default,
	Signature: require_index$12.default,
	FailCustom: require_failCustom.default,
	UnknowFail: require_unknow.default,
	Prompt: require_index$10.default
};
/**
* @title: 支付组件
* @description:
* @Author: hongbing.wang
* @Date: 2024-08-20 14:59
*/
const Pay = ({ api, onChange: onChangeStatus, className, terminal, net, isMobile, tyroUrl, tyroVersion, formatAmount = (number, precision, symbol) => `${symbol}${number}`, client }) => {
	const data = require_index.useContextData();
	const dataRef = (0, react.useRef)(data);
	const dispatch = require_index.useDispatch();
	const ref = (0, react.useRef)(false);
	const statusRef = (0, react.useRef)(data.status);
	const comonentRef = (0, react.useRef)(data.component);
	const backupRef = (0, react.useRef)(data.backup);
	const clearRef = (0, react.useRef)(null);
	(0, react.useEffect)(() => {
		require_payLog.default.log("支付组件初始化");
	}, []);
	(0, react.useEffect)(() => {
		comonentRef.current = data.component;
	}, [data.component]);
	(0, react.useEffect)(() => {
		backupRef.current = data.backup;
	}, [data.backup]);
	(0, react.useEffect)(() => {
		dataRef.current = data;
	}, [data]);
	const Comm = Mapping[data.component];
	/**
	* @title: 获取组件状态
	* @Author: hongbing.wang
	* @Date: 2024-08-20 15:00
	*/
	const onChange = (0, react.useCallback)((status, params, other) => {
		if (status === require_const.StatusEnum.Success || status === require_const.StatusEnum.Fail) dispatch(require_index.updateStatus(status));
		onChangeStatus(status, params, other);
	}, [dispatch, onChangeStatus]);
	(0, react.useEffect)(() => {
		if (statusRef.current === "success") {
			const status = statusRef.current;
			const params = require_helper.getPayParams(dataRef.current, status);
			clearRef.current = setTimeout(() => {
				onChange(status, params);
			}, 1e3);
		}
	}, [onChange]);
	(0, react.useEffect)(() => {
		dispatch(require_index.updateState({ net }));
		if (!ref.current) {
			ref.current = true;
			if (backupRef.current && net) {
				require_payLog.default.log("网络恢复：从备份恢复", { hasBackup: true });
				const _backupRef$current = backupRef.current, data = require_extends._extends({}, (require_objectDestructuringEmpty._objectDestructuringEmpty(_backupRef$current), _backupRef$current));
				dispatch(require_index.backUpFree({ key: Date.now() }));
				dispatch(require_index.updateState({ net }));
				if (data.status === "success") {
					const status = data.status;
					const params = require_helper.getPayParams(dataRef.current, status);
					clearRef.current = setTimeout(() => {
						onChange(status, params);
					}, 1e3);
				}
			}
			return;
		}
		if (net) {
			require_payLog.default.log("网络恢复", { hasBackup: !!backupRef.current });
			const _backupRef$current2 = backupRef.current, data = require_extends._extends({}, (require_objectDestructuringEmpty._objectDestructuringEmpty(_backupRef$current2), _backupRef$current2));
			dispatch(require_index.backUpFree({ key: Date.now() }));
			dispatch(require_index.updateState({ net }));
			if (data.status === "success") {
				const status = data.status;
				const params = require_helper.getPayParams(dataRef.current, status);
				clearRef.current = setTimeout(() => {
					onChange(status, params);
				}, 1e3);
			}
			return;
		}
		require_payLog.default.log("网络断开，备份数据");
		dispatch(require_index.updateState({ form: "Network" }));
		dispatch(require_index.backUpProduction({}));
		dispatch(require_index.updateState({
			net,
			status: "warn"
		}));
		dispatch(require_index.updateComponent("Network"));
	}, [
		dispatch,
		net,
		onChange
	]);
	(0, react.useEffect)(() => {
		return () => {
			if (clearRef.current) {
				clearTimeout(clearRef.current);
				clearRef.current = null;
			}
		};
	}, []);
	console.log("data =>>>>>> ", data);
	const isShowHeader = (0, react.useMemo)(() => {
		let _isShow = true;
		if (data.status === require_const.StatusEnum.Init) _isShow = false;
		if ([require_const.EftposEnum.AdyenAlipay, require_const.EftposEnum.AdyenWechat].includes(data.eftpos) && ![require_const.StatusEnum.Success, require_const.StatusEnum.Fail].includes(data.status)) _isShow = false;
		if (data.eftpos === "mx51" && data.status === require_const.StatusEnum.Question) _isShow = false;
		return _isShow;
	}, [data.status, data.eftpos]);
	return /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, data.render ? /* @__PURE__ */ react.default.createElement("div", {
		className: `pisell-lowcode__eft-pay-box ${className}`,
		style: data.type === "step" ? {} : { display: "none" }
	}, isShowHeader && /* @__PURE__ */ react.default.createElement(require_index$8.default, {
		status: data.status,
		title: data.title,
		subTitle: data.subTitle
	}), data.eftpos === "payo" ? /* @__PURE__ */ react.default.createElement(require_index$1.default, {
		api,
		onChange
	}, data.warn && data.status !== require_const.StatusEnum.Success && client === require_const.ClientEnum.Merchant ? /* @__PURE__ */ react.default.createElement("div", { className: "pisell-lowcode__eft-pay-step-item pisell-lowcode__eft-pay-step-item-warn" }, data.warn) : null, data.status === require_const.StatusEnum.Success ? /* @__PURE__ */ react.default.createElement("div", { className: "pisell-lowcode__eft-pay-step-item pisell-lowcode__eft-pay-step-item-tip" }, _pisell_utils.locales.getText("page.eftpos-pay.text.transaction.end")) : null) : null, data.eftpos === "tyro" ? /* @__PURE__ */ react.default.createElement(require_index$2.default, {
		api,
		onChange,
		key: data.key,
		url: tyroUrl,
		version: tyroVersion
	}) : null, data.eftpos === "windcave" ? /* @__PURE__ */ react.default.createElement(require_windcave.default, {
		api,
		onChange,
		key: data.key,
		formatAmount
	}) : null, data.eftpos === "linkly" ? /* @__PURE__ */ react.default.createElement(require_index$3.default, {
		key: data.key,
		api,
		onChange
	}) : null, data.eftpos === "mx51" ? /* @__PURE__ */ react.default.createElement(require_index$4.default, {
		key: data.key,
		onChange
	}) : null, data.eftpos === "huifu" ? /* @__PURE__ */ react.default.createElement(require_index$5.default, {
		key: data.key,
		onChange,
		isNativeApp: isMobile
	}) : null, data.eftpos === require_const.EftposEnum.AdyenPos ? /* @__PURE__ */ react.default.createElement(require_index$6.default, {
		key: data.key,
		onChange,
		isNativeApp: isMobile
	}) : null, [require_const.EftposEnum.AdyenAlipay, require_const.EftposEnum.AdyenWechat].includes(data.eftpos) ? /* @__PURE__ */ react.default.createElement(require_index$7.default, {
		key: data.key,
		onChange,
		isNativeApp: isMobile
	}) : null) : null, data.type === require_const.PayRouteEnum.Unset && Comm ? /* @__PURE__ */ react.default.createElement(Comm, {
		onChange,
		api,
		className,
		isMobile,
		formatAmount
	}) : null);
};
//#endregion
exports.default = Pay;
