import { useRef } from "react";
import { sendWarningLog } from "@pisell/utils";
import { useMemoizedFn } from "ahooks";
//#region src/hooks/usePaymentLogger.ts
/**
* @title: 支付日志记录器 Hook
* @description: 用于记录支付过程中的关键信息
* @param {UsePaymentLoggerOptions} options - 配置选项
* @return {object} 返回日志记录相关方法
* @Author: AI Assistant
*/
const usePaymentLogger = (options) => {
	const { orderId, step = "unknown" } = options;
	const logs = useRef([]);
	return {
		logs,
		addLog: useMemoizedFn((logData) => {
			logs.current.push({
				key: logData.key,
				value: logData.value
			});
		}),
		sendLogs: useMemoizedFn((title = "支付日志", err) => {
			const content = [{
				key: "title",
				value: title
			}, ...logs.current];
			if (err) content.push(...[
				{
					key: "错误信息",
					value: (err === null || err === void 0 ? void 0 : err.message) || "未知错误"
				},
				{
					key: "错误堆栈",
					value: (err === null || err === void 0 ? void 0 : err.stack) || "无堆栈信息"
				},
				{
					key: "错误类型",
					value: (err === null || err === void 0 ? void 0 : err.name) || "未知错误类型"
				}
			]);
			const formatLogs = content.map((item) => ({
				key: item.key,
				value: JSON.stringify(item.value)
			}));
			sendWarningLog({
				title: `h5支付step${step}:${orderId}`,
				content: formatLogs
			});
		})
	};
};
//#endregion
export { usePaymentLogger as default };
