import useEngineContext from "../../hooks/useEngineContext.js";
import { request } from "../../utils/index.js";
import { useRef, useState } from "react";
import { PisellToast } from "@pisell/materials";
import { useRequest } from "ahooks";
//#region src/components/pinVerifyModal/hooks.ts
const usePinVerify = () => {
	var _context$appHelper;
	const context = useEngineContext();
	const apiPrefix = context === null || context === void 0 || (_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 || (_context$appHelper = _context$appHelper.constants) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.apiPrefix;
	const [verifyInfo, setVerifyInfo] = useState(null);
	const verifySuccessRef = useRef(() => {});
	/**
	* @title: 设置验证信息
	* @description 设置验证信息，如果check_max_count为0，则设置为null，剩余次数为check_max_count - failed_count
	* @param data 验证信息
	*/
	const _setVerifyInfo = (data) => {
		const { failed_count, check_max_count } = data || {};
		if (!check_max_count) {
			setVerifyInfo(null);
			return;
		}
		setVerifyInfo({
			check_max_count,
			remaining_count: check_max_count - (failed_count || 0)
		});
	};
	const verifyPinApi = (data) => {
		return request.getRequest().post(`${apiPrefix}/wallet/detail/pin/validate`, data, { fullResult: true });
	};
	const _verifyPin = useRequest(verifyPinApi, {
		manual: true,
		onSuccess: (res) => {
			if ((res === null || res === void 0 ? void 0 : res.code) === 200) {
				const { status, msg, failed_count, check_max_count } = (res === null || res === void 0 ? void 0 : res.data) || {};
				_setVerifyInfo({
					failed_count,
					check_max_count
				});
				PisellToast({
					content: msg,
					duration: 2
				});
				if (status === "success") {
					var _verifySuccessRef$cur;
					(_verifySuccessRef$cur = verifySuccessRef.current) === null || _verifySuccessRef$cur === void 0 || _verifySuccessRef$cur.call(verifySuccessRef, res === null || res === void 0 ? void 0 : res.data);
				}
			}
		}
	});
	const verifyPin = (data, onSuccess) => {
		verifySuccessRef.current = onSuccess;
		_verifyPin.run(data);
	};
	return {
		verifyPin,
		loading: _verifyPin.loading,
		verifyInfo,
		setVerifyInfo: _setVerifyInfo
	};
};
//#endregion
export { usePinVerify };
