import { ClientEnum, EftposEnum, PayRouteEnum, StatusEnum } from "./const.js";
import { getPayParams } from "./helper.js";
import { _objectDestructuringEmpty } from "../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectDestructuringEmpty.js";
import { _extends } from "../../_virtual/_@oxc-project_runtime@0.122.0/helpers/extends.js";
import { backUpFree, backUpProduction, updateComponent, updateState, updateStatus, useContextData, useDispatch } from "./store/index.js";
import payLog from "./utils/payLog.js";
import payo_default from "./payo/index.js";
import windcave_default from "./windcave/windcave.js";
import tyro_default from "./tyro/index.js";
import Linkly from "./linkly/index.js";
import AggregatePayment from "./mx51/index.js";
import Huifu from "./huifu/index.js";
import AdyenPos from "./adyenPos/index.js";
import AdyenScan from "./adyenScan/index.js";
import header_default from "./component/header/index.js";
import fail_default from "./component/fail/index.js";
import failCustom_default from "./component/fail/failCustom.js";
import network_default from "./component/fail/network.js";
import unknow_default from "./component/fail/unknow.js";
import prompt_default from "./component/prompt/index.js";
import manual_default from "./component/manual/index.js";
import signature_default from "./component/signature/index.js";
import React, { useCallback, useEffect, useMemo, useRef } from "react";
import { locales } from "@pisell/utils";
import "./index.less";
//#region src/components/eftposPay/pay.tsx
const Mapping = {
	Fail: fail_default,
	Network: network_default,
	Manual: manual_default,
	Signature: signature_default,
	FailCustom: failCustom_default,
	UnknowFail: unknow_default,
	Prompt: prompt_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 = useContextData();
	const dataRef = useRef(data);
	const dispatch = useDispatch();
	const ref = useRef(false);
	const statusRef = useRef(data.status);
	const comonentRef = useRef(data.component);
	const backupRef = useRef(data.backup);
	const clearRef = useRef(null);
	useEffect(() => {
		payLog.log("支付组件初始化");
	}, []);
	useEffect(() => {
		comonentRef.current = data.component;
	}, [data.component]);
	useEffect(() => {
		backupRef.current = data.backup;
	}, [data.backup]);
	useEffect(() => {
		dataRef.current = data;
	}, [data]);
	const Comm = Mapping[data.component];
	/**
	* @title: 获取组件状态
	* @Author: hongbing.wang
	* @Date: 2024-08-20 15:00
	*/
	const onChange = useCallback((status, params, other) => {
		if (status === StatusEnum.Success || status === StatusEnum.Fail) dispatch(updateStatus(status));
		onChangeStatus(status, params, other);
	}, [dispatch, onChangeStatus]);
	useEffect(() => {
		if (statusRef.current === "success") {
			const status = statusRef.current;
			const params = getPayParams(dataRef.current, status);
			clearRef.current = setTimeout(() => {
				onChange(status, params);
			}, 1e3);
		}
	}, [onChange]);
	useEffect(() => {
		dispatch(updateState({ net }));
		if (!ref.current) {
			ref.current = true;
			if (backupRef.current && net) {
				payLog.log("网络恢复：从备份恢复", { hasBackup: true });
				const _backupRef$current = backupRef.current, data = _extends({}, (_objectDestructuringEmpty(_backupRef$current), _backupRef$current));
				dispatch(backUpFree({ key: Date.now() }));
				dispatch(updateState({ net }));
				if (data.status === "success") {
					const status = data.status;
					const params = getPayParams(dataRef.current, status);
					clearRef.current = setTimeout(() => {
						onChange(status, params);
					}, 1e3);
				}
			}
			return;
		}
		if (net) {
			payLog.log("网络恢复", { hasBackup: !!backupRef.current });
			const _backupRef$current2 = backupRef.current, data = _extends({}, (_objectDestructuringEmpty(_backupRef$current2), _backupRef$current2));
			dispatch(backUpFree({ key: Date.now() }));
			dispatch(updateState({ net }));
			if (data.status === "success") {
				const status = data.status;
				const params = getPayParams(dataRef.current, status);
				clearRef.current = setTimeout(() => {
					onChange(status, params);
				}, 1e3);
			}
			return;
		}
		payLog.log("网络断开，备份数据");
		dispatch(updateState({ form: "Network" }));
		dispatch(backUpProduction({}));
		dispatch(updateState({
			net,
			status: "warn"
		}));
		dispatch(updateComponent("Network"));
	}, [
		dispatch,
		net,
		onChange
	]);
	useEffect(() => {
		return () => {
			if (clearRef.current) {
				clearTimeout(clearRef.current);
				clearRef.current = null;
			}
		};
	}, []);
	console.log("data =>>>>>> ", data);
	const isShowHeader = useMemo(() => {
		let _isShow = true;
		if (data.status === StatusEnum.Init) _isShow = false;
		if ([EftposEnum.AdyenAlipay, EftposEnum.AdyenWechat].includes(data.eftpos) && ![StatusEnum.Success, StatusEnum.Fail].includes(data.status)) _isShow = false;
		if (data.eftpos === "mx51" && data.status === StatusEnum.Question) _isShow = false;
		return _isShow;
	}, [data.status, data.eftpos]);
	return /* @__PURE__ */ React.createElement(React.Fragment, null, data.render ? /* @__PURE__ */ React.createElement("div", {
		className: `pisell-lowcode__eft-pay-box ${className}`,
		style: data.type === "step" ? {} : { display: "none" }
	}, isShowHeader && /* @__PURE__ */ React.createElement(header_default, {
		status: data.status,
		title: data.title,
		subTitle: data.subTitle
	}), data.eftpos === "payo" ? /* @__PURE__ */ React.createElement(payo_default, {
		api,
		onChange
	}, data.warn && data.status !== StatusEnum.Success && client === ClientEnum.Merchant ? /* @__PURE__ */ React.createElement("div", { className: "pisell-lowcode__eft-pay-step-item pisell-lowcode__eft-pay-step-item-warn" }, data.warn) : null, data.status === StatusEnum.Success ? /* @__PURE__ */ React.createElement("div", { className: "pisell-lowcode__eft-pay-step-item pisell-lowcode__eft-pay-step-item-tip" }, locales.getText("page.eftpos-pay.text.transaction.end")) : null) : null, data.eftpos === "tyro" ? /* @__PURE__ */ React.createElement(tyro_default, {
		api,
		onChange,
		key: data.key,
		url: tyroUrl,
		version: tyroVersion
	}) : null, data.eftpos === "windcave" ? /* @__PURE__ */ React.createElement(windcave_default, {
		api,
		onChange,
		key: data.key,
		formatAmount
	}) : null, data.eftpos === "linkly" ? /* @__PURE__ */ React.createElement(Linkly, {
		key: data.key,
		api,
		onChange
	}) : null, data.eftpos === "mx51" ? /* @__PURE__ */ React.createElement(AggregatePayment, {
		key: data.key,
		onChange
	}) : null, data.eftpos === "huifu" ? /* @__PURE__ */ React.createElement(Huifu, {
		key: data.key,
		onChange,
		isNativeApp: isMobile
	}) : null, data.eftpos === EftposEnum.AdyenPos ? /* @__PURE__ */ React.createElement(AdyenPos, {
		key: data.key,
		onChange,
		isNativeApp: isMobile
	}) : null, [EftposEnum.AdyenAlipay, EftposEnum.AdyenWechat].includes(data.eftpos) ? /* @__PURE__ */ React.createElement(AdyenScan, {
		key: data.key,
		onChange,
		isNativeApp: isMobile
	}) : null) : null, data.type === PayRouteEnum.Unset && Comm ? /* @__PURE__ */ React.createElement(Comm, {
		onChange,
		api,
		className,
		isMobile,
		formatAmount
	}) : null);
};
//#endregion
export { Pay as default };
