"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.heapMiddleWareManager = void 0;
const tslib_1 = require("tslib");
const types_1 = require("tdf-devtools-protocol/dist/types");
const log_1 = require("@/utils/log");
const global_id_1 = require("@/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]: ({ msg, sendToApp, sendToDevtools }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
            const res = yield 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]: ({ msg, sendToApp, sendToDevtools }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
            const req = msg;
            const { reportProgress } = req.params;
            const commandRes = (yield 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: {},
        }),
    },
};
