"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.runtimeMiddleWareManager = void 0;
const tslib_1 = require("tslib");
const enum_chrome_mapping_1 = require("tdf-devtools-protocol/dist/types/enum-chrome-mapping");
const enum_ios_9_0_mapping_1 = require("tdf-devtools-protocol/dist/types/enum-ios-9.0-mapping");
const global_id_1 = require("@/utils/global-id");
const debugger_middleware_1 = require("./debugger-middleware");
exports.runtimeMiddleWareManager = {
    downwardMiddleWareListMap: {
        [enum_chrome_mapping_1.ChromeEvent.RuntimeExecutionContextCreated]: ({ msg, sendToDevtools }) => {
            var _a;
            const eventRes = msg;
            if ((_a = eventRes.params) === null || _a === void 0 ? void 0 : _a.context) {
                if (!eventRes.params.context.origin) {
                    eventRes.params.context.origin = eventRes.params.context.name;
                }
                if (eventRes.params.context.frameId) {
                    eventRes.params.context.auxData = {
                        frameId: eventRes.params.context.frameId,
                        isDefault: true,
                    };
                    delete eventRes.params.context.frameId;
                }
            }
            return sendToDevtools(eventRes);
        },
        [enum_chrome_mapping_1.ChromeCommand.RuntimeEvaluate]: ({ msg, sendToDevtools }) => {
            var _a, _b, _c;
            const lastScriptEval = debugger_middleware_1.getLastScriptEval();
            const commandRes = msg;
            if ((_a = commandRes.result) === null || _a === void 0 ? void 0 : _a.wasThrown) {
                commandRes.result.result.subtype = 'error';
                commandRes.result.exceptionDetails = {
                    exceptionId: 1,
                    text: commandRes.result.result.description,
                    url: '',
                    scriptId: lastScriptEval,
                    lineNumber: 1,
                    columnNumber: 0,
                    stackTrace: {
                        callFrames: [
                            {
                                functionName: '',
                                scriptId: lastScriptEval,
                                url: '',
                                lineNumber: 1,
                                columnNumber: 1,
                            },
                        ],
                    },
                };
            }
            else if ((_c = (_b = commandRes.result) === null || _b === void 0 ? void 0 : _b.result) === null || _c === void 0 ? void 0 : _c.preview) {
                commandRes.result.result.preview.description = commandRes.result.result.description;
                commandRes.result.result.preview.type = 'object';
            }
            return sendToDevtools(commandRes);
        },
        [enum_chrome_mapping_1.ChromeCommand.RuntimeGetProperties]: ({ msg, sendToDevtools }) => {
            var _a;
            const commandRes = msg;
            const newPropertyDescriptors = [];
            const properties = ((_a = commandRes.result) === null || _a === void 0 ? void 0 : _a.properties) || [];
            for (let i = 0; i < (properties === null || properties === void 0 ? void 0 : properties.length); i += 1) {
                if (properties[i].isOwn || properties[i].nativeGetter) {
                    properties[i].isOwn = true;
                    newPropertyDescriptors.push(properties[i]);
                }
            }
            commandRes.result.result = null;
            commandRes.result.result = newPropertyDescriptors;
            return sendToDevtools(commandRes);
        },
        [enum_chrome_mapping_1.ChromeCommand.RuntimeEnable]: ({ msg, sendToDevtools }) => {
            sendToDevtools({
                method: enum_chrome_mapping_1.ChromeEvent.RuntimeExecutionContextCreated,
                params: {
                    context: {
                        id: global_id_1.contextId.create(),
                        name: 'tdf',
                        origin: '',
                    },
                },
            });
            return sendToDevtools(msg);
        },
    },
    upwardMiddleWareListMap: {
        [enum_chrome_mapping_1.ChromeCommand.RuntimeCompileScript]: ({ msg, sendToApp, sendToDevtools }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
            const eventRes = msg;
            yield sendToApp({
                id: global_id_1.requestId.create(),
                method: enum_ios_9_0_mapping_1.IOS90Command.RuntimeEvaluate,
                params: {
                    expression: eventRes.params.expression,
                    contextId: eventRes.params.executionContextId,
                },
            });
            const convertedRes = {
                id: msg.id,
                method: msg.method,
                result: {
                    scriptId: null,
                    exceptionDetails: null,
                },
            };
            sendToDevtools(convertedRes);
            return convertedRes;
        }),
    },
};
