"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.heapMiddleWareManager = void 0;
const tslib_1 = require("tslib");
const types_1 = require("@hippy/devtools-protocol/dist/types");
const log_1 = require("@debug-server-next/utils/log");
const global_id_1 = require("@debug-server-next/utils/global-id");
const heap_adapter_1 = tslib_1.__importDefault(require("./adapter/heap-adapter"));
const log = new log_1.Logger('heap-middleware');
exports.heapMiddleWareManager = {
    downwardMiddleWareListMap: {},
    upwardMiddleWareListMap: {
        [types_1.ChromeCommand.HeapProfilerEnable]: async ({ msg, sendToApp, sendToDevtools }) => {
            const res = await sendToApp({
                id: global_id_1.requestId.create(),
                method: types_1.IOS100Command.HeapEnable,
                params: {},
            });
            log.verbose(`${types_1.IOS100Command.HeapEnable} res: `, msg);
            const convertedRes = {
                id: msg.id,
                method: msg.method,
                result: res,
            };
            sendToDevtools(convertedRes);
            return convertedRes;
        },
        [types_1.ChromeCommand.HeapProfilerDisable]: ({ sendToApp }) => sendToApp({
            id: global_id_1.requestId.create(),
            method: types_1.IOS100Command.HeapDisable,
            params: {},
        }),
        [types_1.ChromeCommand.HeapProfilerTakeHeapSnapshot]: async ({ msg, sendToApp, sendToDevtools }) => {
            const req = msg;
            const { reportProgress } = req.params;
            const commandRes = (await sendToApp({
                id: global_id_1.requestId.create(),
                method: types_1.IOS100Command.HeapSnapshot,
                params: {},
            }));
            const snapshot = JSON.parse(commandRes.result.snapshotData);
            const wholeChunk = JSON.stringify(heap_adapter_1.default.jsc2v8(snapshot));
            if (reportProgress)
                sendToDevtools({
                    method: types_1.ChromeEvent.HeapProfilerReportHeapSnapshotProgress,
                    params: {
                        finished: true,
                        done: wholeChunk.length,
                        total: wholeChunk.length,
                    },
                });
            sendToDevtools({
                method: types_1.ChromeEvent.HeapProfilerAddHeapSnapshotChunk,
                params: {
                    chunk: wholeChunk,
                },
            });
            const convertedRes = {
                id: msg.id,
                method: msg.method,
                result: {},
            };
            sendToDevtools(convertedRes);
            return convertedRes;
        },
        [types_1.ChromeCommand.HeapProfilerCollectGarbage]: ({ sendToApp }) => sendToApp({
            id: global_id_1.requestId.create(),
            method: types_1.IOS100Command.HeapGc,
            params: {},
        }),
    },
};
