import { _asyncToGenerator } from "../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import useEngineContext from "../../hooks/useEngineContext.js";
import exec from "../../utils/load.js";
import { useEffect, useMemo, useRef, useState } from "react";
//#region src/components/eftpos/hooks.tsx
/**
* @title: tyro 设备连接
* @Author: hongbing.wang
* @Date: 2024-08-20 13:23
*/
const usePairTerminalo = (type, params) => {
	const ref = useRef();
	const iclientRef = useRef();
	const url = useMemo(() => {
		return params && params.url && params.version ? `${params.url}/${params.version}` : "https://iclientsimulator.test.tyro.com/iclient-v1.js";
	}, [params]);
	const loadSDK = () => {
		var _window;
		if ((_window = window) === null || _window === void 0 ? void 0 : _window.TYRO) {
			var _window2;
			return Promise.resolve((_window2 = window) === null || _window2 === void 0 ? void 0 : _window2.TYRO);
		}
		return exec(url, document.head).then(() => {
			var _window3, _window4;
			console.log((_window3 = window) === null || _window3 === void 0 ? void 0 : _window3.TYRO);
			return (_window4 = window) === null || _window4 === void 0 ? void 0 : _window4.TYRO;
		});
	};
	return function() {
		var _ref = _asyncToGenerator(function* ({ mid, tid, apiKey, posProductData }, cb) {
			if (type !== "tyro") {
				cb({});
				return;
			}
			if (!ref.current) ref.current = yield loadSDK();
			if (!ref.current) {
				cb({});
				return;
			}
			if (!iclientRef.current) iclientRef.current = new ref.current.IClient(apiKey, posProductData);
			iclientRef.current.pairTerminal(mid, tid, (response) => {
				console.log(response);
				cb(response);
			});
		});
		return function pair(_x, _x2) {
			return _ref.apply(this, arguments);
		};
	}();
};
/**
* @title: 获取 Tyro 资源
* @description:
* @param {Eftpos} type 类型
* @param {string} url 链接
* @return {*}
* @Author: hongbing.wang
* @Date: 2024-08-19 18:36
*/
const useTyro = (params) => {
	const ref = useRef();
	const [load, setLoad] = useState(false);
	const url = useMemo(() => {
		return params && params.url && params.version ? `${params.url}/${params.version}` : "https://iclientsimulator.test.tyro.com/iclient-v1.js";
	}, [params]);
	useEffect(() => {
		exec(url, document.head).then(() => {
			var _window5;
			return (_window5 = window) === null || _window5 === void 0 ? void 0 : _window5.TYRO;
		}).then((res) => {
			setLoad(true);
			ref.current = res;
		}, () => {
			setLoad(true);
		});
	}, [url]);
	return [ref, load];
};
const useRequest = () => {
	var _context$appHelper;
	const context = useEngineContext();
	return (context === null || context === void 0 || (_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 || (_context$appHelper = _context$appHelper.utils) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.request) || {};
};
/**
* @title 配对请求
*/
const usePairRequest = () => {
	const request = useRequest();
	const run = (params) => {
		return request.post("/shop/pay/card-reader/pair", params, {});
	};
	return { run };
};
/**
* @title 检查配对状态
*/
const useCheckPairRequest = () => {
	const request = useRequest();
	const run = (params) => {
		return request.post("/shop/pay/card-reader/pairing-info", params, { customResponse: true });
	};
	return { run };
};
//#endregion
export { useCheckPairRequest, usePairRequest, usePairTerminalo, useRequest, useTyro };
