"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.logMiddleWareManager = void 0;
const types_1 = require("tdf-devtools-protocol/dist/types");
const enum_1 = require("@/@types/enum");
exports.logMiddleWareManager = {
    downwardMiddleWareListMap: {
        [types_1.IOS100Event.ConsoleMessageAdded]: ({ msg, sendToDevtools }) => {
            const eventRes = msg;
            const { message } = eventRes.params;
            let type;
            if (message.type === 'log') {
                type = {
                    log: enum_1.ChromeLogLevel.Info,
                    [enum_1.ChromeLogLevel.Info]: enum_1.ChromeLogLevel.Info,
                    [enum_1.ChromeLogLevel.Error]: enum_1.ChromeLogLevel.Error,
                    [enum_1.ChromeLogLevel.Warning]: enum_1.ChromeLogLevel.Warning,
                }[message.level];
                if (!type)
                    type = enum_1.ChromeLogLevel.Info;
            }
            else {
                type = message.type;
            }
            // if (message.source === 'console-api') {
            //   const logEntry: ProtocolChrome.Runtime.ConsoleAPICalledEvent = {
            //     type: type as any,
            //     stackTrace: transformStacktrace(message.stackTrace),
            //     timestamp: Math.floor(new Date().getTime()),
            //     executionContextId: 1,
            //     args: (message.parameters || []) as any,
            //   };
            //   setContext('lastConsoleMessage', logEntry);
            //   return sendToDevtools({
            //     method: ChromeEvent.RuntimeConsoleAPICalled,
            //     params: logEntry,
            //   });
            // }
            const consoleMessage = {
                // 这里自动生成的类型没有定义 enum，而是用的联合类型，导致类型不同，所以使用 any 转换
                source: message.source,
                level: type,
                text: '',
                lineNumber: message.line,
                timestamp: new Date().getTime(),
                url: message.url,
                args: message.parameters,
                stackTrace: message.stackTrace
                    ? {
                        callFrames: message.stackTrace,
                    }
                    : undefined,
                networkRequestId: message.networkRequestId,
            };
            return sendToDevtools({
                method: types_1.ChromeEvent.LogEntryAdded,
                params: {
                    entry: consoleMessage,
                },
            });
        },
        [types_1.IOS90Command.ConsoleEnable]: ({ msg, sendToDevtools }) => sendToDevtools({
            id: msg.id,
            method: types_1.ChromeCommand.LogEnable,
            result: {},
        }),
    },
    upwardMiddleWareListMap: {
        [types_1.ChromeCommand.LogClear]: ({ msg, sendToApp }) => sendToApp({
            id: msg.id,
            method: types_1.ChromeCommand.ConsoleClearMessages,
            params: {},
        }),
        [types_1.ChromeCommand.LogDisable]: ({ msg, sendToApp }) => sendToApp({
            id: msg.id,
            method: types_1.IOS90Command.ConsoleDisable,
            params: {},
        }),
        [types_1.ChromeCommand.LogEnable]: ({ msg, sendToApp }) => sendToApp({
            id: msg.id,
            method: types_1.IOS90Command.ConsoleEnable,
            params: {},
        }),
    },
};
function transformStacktrace(callFrames) {
    if (!callFrames) {
        return;
    }
    return {
        callFrames,
        // Optional
        // description?: string;
        // parent?: StackTrace;
        // parentId?: StackTraceId;
    };
}
