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_useEngineContext = require("../../hooks/useEngineContext.js");
const require_index = require("../../utils/index.js");
const require_const = require("./const.js");
const require_index$1 = require("./store/index.js");
const require_payLog = require("./utils/payLog.js");
const require_amount = require("./amount.js");
const require_device = require("./device.js");
const require_pay = require("./pay.js");
let react = require("react");
react = require_runtime.__toESM(react);
let ahooks = require("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 = require_objectWithoutProperties._objectWithoutProperties(_ref, _excluded);
	const data = require_index$1.useContextData();
	const dispatch = require_index$1.useDispatch();
	const [current, setCurrent] = (0, react.useState)(action);
	const utils = require_useEngineContext.default().appHelper.utils || {};
	require_index.request.setRequest(utils === null || utils === void 0 ? void 0 : utils.request);
	const statusRef = (0, react.useRef)(data.status);
	const network = (0, react.useMemo)(() => typeof terminal.network === "boolean" ? terminal.network : true, [terminal === null || terminal === void 0 ? void 0 : terminal.network]);
	const { online } = (0, ahooks.useNetwork)();
	const netRef = (0, react.useRef)([network, online]);
	const net = (0, react.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]);
	(0, react.useEffect)(() => {
		statusRef.current = data.status;
	}, [data.status]);
	const client = (0, react.useMemo)(() => {
		if (channel === "kiosk") return require_const.ClientEnum.User;
		return require_const.ClientEnum.Merchant;
	}, [channel]);
	(0, react.useEffect)(() => {
		dispatch(require_index$1.updateClient(client));
	}, [client]);
	/**
	* @title: 缓存状态数据 可以用于闪退,成功or失败时清除缓存
	* @Author: hongbing.wang
	* @Date: 2024-08-20 10:27
	*/
	(0, react.useEffect)(() => {
		onChangeParams && onChangeParams(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, data), data.eftpos === "tyro" ? { custom: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, data.custom), {}, {
			btnList: [],
			list: []
		}) } : {}), {}, { form: require_const.RestoreEumn.Restore }));
	}, [data, onChangeParams]);
	/**
	* @title: 支付状态切换页
	* @Author: hongbing.wang
	* @Date: 2024-08-20 13:10
	*/
	const onChange = (0, react.useCallback)((status, params, other) => {
		if (status === "page" && typeof params === "string") {
			if ([
				"amount",
				"deviceList",
				"pay"
			].includes(params)) {
				setCurrent(params);
				dispatch(require_index$1.updateState({ action: params }));
			}
		}
		props.onChangeStatus(status, params, other);
	}, []);
	/**
	* @title: 支付失败
	* @Author: hongbing.wang
	* @Date: 2024-08-20 13:09
	*/
	const onClose = () => {
		require_payLog.default.log("关闭支付");
		onChange("fail", {
			number: data.number,
			uniquePaymentNumber: data.number,
			device: data.device,
			order_id: data.order_id
		});
	};
	if (current === require_const.RouteEnum.DeviceList) {
		var _props$styles, _props$params;
		return /* @__PURE__ */ react.default.createElement(require_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 === require_const.RouteEnum.Amount) return /* @__PURE__ */ react.default.createElement(require_amount.default, {
		onChange,
		onClose,
		formatAmount,
		isMobile,
		className: isMobile ? "pisell-lowcode__eft-pay-terminal" : "",
		net,
		client
	});
	return /* @__PURE__ */ react.default.createElement(require_pay.default, {
		api,
		tyroUrl,
		tyroVersion,
		onChange,
		className: isMobile ? "pisell-lowcode__eft-pay-terminal" : "",
		terminal,
		isMobile,
		net,
		formatAmount,
		client
	});
};
//#endregion
exports.default = App;
