"use strict";
/*
 * Tencent is pleased to support the open source community by making
 * Hippy available.
 *
 * Copyright (C) 2017-2019 THL A29 Limited, a Tencent company.
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.logMiddleWareManager = void 0;
const types_1 = require("@hippy/devtools-protocol/dist/types");
const enum_1 = require("@debug-server-next/@types/enum");
exports.logMiddleWareManager = {
    downwardMiddleWareListMap: {
        [types_1.IOS100Event.ConsoleMessageAdded]: ({ msg, sendToDevtools, setContext }) => {
            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;
            }
            /**
             * Runtime.consoleAPICalled support style format in console api
             */
            if (message.source === 'console-api') {
                const logEntry = {
                    type: type,
                    stackTrace: transformStacktrace(message.stackTrace),
                    timestamp: Math.floor(new Date().getTime()),
                    executionContextId: 1,
                    args: (message.parameters || []),
                };
                setContext('lastConsoleMessage', logEntry);
                return sendToDevtools({
                    method: types_1.ChromeEvent.RuntimeConsoleAPICalled,
                    params: logEntry,
                });
            }
            const consoleMessage = {
                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;
    };
}
