import { _objectSpread2 } from "../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { _objectWithoutProperties } from "../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js";
import useEngineContext from "../../hooks/useEngineContext.js";
import { request } from "../../utils/index.js";
import { ClientEnum, RestoreEumn, RouteEnum } from "./const.js";
import { updateClient, updateState, useContextData, useDispatch } from "./store/index.js";
import payLog from "./utils/payLog.js";
import amount_default from "./amount.js";
import device_default from "./device.js";
import Pay from "./pay.js";
import React, { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { useNetwork } from "ahooks";
//#region src/components/eftposPay/app.tsx
const _excluded = [
	"action",
	"api",
	"device_number",
	"terminal",
	"tyroUrl",
	"tyroVersion",
	"onChangeParams",
	"isMobile",
	"channel",
	"formatAmount"
];
const App = (_ref) => {
	let { action, api, device_number, terminal, tyroUrl, tyroVersion, onChangeParams, isMobile, channel, formatAmount = (number, precision, symbol) => `${symbol}${number}` } = _ref, props = _objectWithoutProperties(_ref, _excluded);
	const data = useContextData();
	const dispatch = useDispatch();
	const [current, setCurrent] = useState(action);
	const utils = useEngineContext().appHelper.utils || {};
	request.setRequest(utils === null || utils === void 0 ? void 0 : utils.request);
	const statusRef = useRef(data.status);
	const network = useMemo(() => typeof terminal.network === "boolean" ? terminal.network : true, [terminal === null || terminal === void 0 ? void 0 : terminal.network]);
	const { online } = useNetwork();
	const netRef = useRef([network, online]);
	const net = useMemo(() => {
		let networkChange = false;
		if (netRef.current[0] !== network) {
			netRef.current[0] = network;
			networkChange = true;
		}
		if (netRef.current[1] !== online) netRef.current[1] = online;
		if (networkChange) return network;
		return online;
	}, [network, online]);
	useEffect(() => {
		statusRef.current = data.status;
	}, [data.status]);
	const client = useMemo(() => {
		if (channel === "kiosk") return ClientEnum.User;
		return ClientEnum.Merchant;
	}, [channel]);
	useEffect(() => {
		dispatch(updateClient(client));
	}, [client]);
	/**
	* @title: 缓存状态数据 可以用于闪退,成功or失败时清除缓存
	* @Author: hongbing.wang
	* @Date: 2024-08-20 10:27
	*/
	useEffect(() => {
		onChangeParams && onChangeParams(_objectSpread2(_objectSpread2(_objectSpread2({}, data), data.eftpos === "tyro" ? { custom: _objectSpread2(_objectSpread2({}, data.custom), {}, {
			btnList: [],
			list: []
		}) } : {}), {}, { form: RestoreEumn.Restore }));
	}, [data, onChangeParams]);
	/**
	* @title: 支付状态切换页
	* @Author: hongbing.wang
	* @Date: 2024-08-20 13:10
	*/
	const onChange = useCallback((status, params, other) => {
		if (status === "page" && typeof params === "string") {
			if ([
				"amount",
				"deviceList",
				"pay"
			].includes(params)) {
				setCurrent(params);
				dispatch(updateState({ action: params }));
			}
		}
		props.onChangeStatus(status, params, other);
	}, []);
	/**
	* @title: 支付失败
	* @Author: hongbing.wang
	* @Date: 2024-08-20 13:09
	*/
	const onClose = () => {
		payLog.log("关闭支付");
		onChange("fail", {
			number: data.number,
			uniquePaymentNumber: data.number,
			device: data.device,
			order_id: data.order_id
		});
	};
	if (current === RouteEnum.DeviceList) {
		var _props$styles, _props$params;
		return /* @__PURE__ */ React.createElement(device_default, {
			api,
			onChange,
			onClose,
			isMobile,
			className: isMobile ? "pisell-lowcode__eft-pay-terminal" : "",
			net,
			device_number,
			formatAmount,
			client,
			containerStyle: ((_props$styles = props.styles) === null || _props$styles === void 0 ? void 0 : _props$styles.deviceContainer) || {},
			customSurcharge: props === null || props === void 0 || (_props$params = props.params) === null || _props$params === void 0 ? void 0 : _props$params.customSurcharge
		});
	}
	if (current === RouteEnum.Amount) return /* @__PURE__ */ React.createElement(amount_default, {
		onChange,
		onClose,
		formatAmount,
		isMobile,
		className: isMobile ? "pisell-lowcode__eft-pay-terminal" : "",
		net,
		client
	});
	return /* @__PURE__ */ React.createElement(Pay, {
		api,
		tyroUrl,
		tyroVersion,
		onChange,
		className: isMobile ? "pisell-lowcode__eft-pay-terminal" : "",
		terminal,
		isMobile,
		net,
		formatAmount,
		client
	});
};
//#endregion
export { App as default };
