"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.publishRes = exports.publishReloadCommand = void 0;
const types_1 = require("tdf-devtools-protocol/dist/types");
const db_1 = require("@/db");
const pub_sub_channel_1 = require("@/utils/pub-sub-channel");
const global_id_1 = require("./global-id");
const reloadCommandId = new global_id_1.GlobalId(-10000, -1);
const reloadCommand = [
    {
        id: reloadCommandId.create(),
        method: types_1.ChromeCommand.NetworkEnable,
        params: {
            maxPostDataSize: 65536,
        },
    },
    {
        id: reloadCommandId.create(),
        method: types_1.ChromeCommand.NetworkSetAttachDebugStack,
        params: {
            enabled: true,
        },
    },
    {
        id: reloadCommandId.create(),
        method: types_1.ChromeCommand.PageEnable,
        params: {},
    },
    {
        id: reloadCommandId.create(),
        method: types_1.ChromeCommand.PageGetResourceTree,
        params: {},
    },
    {
        id: reloadCommandId.create(),
        method: types_1.ChromeCommand.RuntimeEnable,
        params: {},
    },
    {
        id: reloadCommandId.create(),
        method: types_1.ChromeCommand.DOMEnable,
        params: {},
    },
    {
        id: reloadCommandId.create(),
        method: types_1.ChromeCommand.CSSEnable,
        params: {},
    },
    {
        id: reloadCommandId.create(),
        method: types_1.ChromeCommand.DebuggerEnable,
        params: {
            maxScriptsCacheSize: 10000000,
        },
    },
    {
        id: reloadCommandId.create(),
        method: types_1.ChromeCommand.DebuggerSetPauseOnExceptions,
        params: {
            state: 'none',
        },
    },
    {
        id: reloadCommandId.create(),
        method: types_1.ChromeCommand.DebuggerSetAsyncCallStackDepth,
        params: {
            state: 'none',
        },
    },
    {
        id: reloadCommandId.create(),
        method: types_1.ChromeCommand.ProfilerEnable,
        params: {},
    },
    {
        id: reloadCommandId.create(),
        method: types_1.ChromeCommand.LogEnable,
        params: {},
    },
    {
        id: reloadCommandId.create(),
        method: types_1.ChromeCommand.LogStartViolationsReport,
        params: {
            config: [
                {
                    name: 'longTask',
                    threshold: 200,
                },
                {
                    name: 'longLayout',
                    threshold: 30,
                },
                {
                    name: 'blockedEvent',
                    threshold: 100,
                },
                {
                    name: 'blockedParser',
                    threshold: -1,
                },
                {
                    name: 'handler',
                    threshold: 150,
                },
                {
                    name: 'recurringHandler',
                    threshold: 50,
                },
                {
                    name: 'discouragedAPIUse',
                    threshold: -1,
                },
            ],
        },
    },
    {
        id: reloadCommandId.create(),
        method: types_1.ChromeCommand.InspectorEnable,
        params: {},
    },
    {
        id: reloadCommandId.create(),
        method: types_1.ChromeCommand.TargetSetAutoAttach,
        params: {
            autoAttach: true,
            waitForDebuggerOnStart: true,
            flatten: true,
        },
    },
    {
        id: reloadCommandId.create(),
        method: types_1.ChromeCommand.TargetSetDiscoverTargets,
        params: {
            discover: true,
        },
    },
    {
        id: reloadCommandId.create(),
        method: types_1.ChromeCommand.RuntimeGetIsolateId,
        params: {},
    },
    {
        id: reloadCommandId.create(),
        method: types_1.ChromeCommand.DebuggerSetBlackboxPatterns,
        params: {
            patterns: [],
        },
    },
    {
        id: reloadCommandId.create(),
        method: types_1.ChromeCommand.PageStartScreencast,
        params: {
            format: 'jpeg',
            quality: 60,
            maxWidth: 1522,
            maxHeight: 1682,
        },
    },
    {
        id: reloadCommandId.create(),
        method: types_1.ChromeCommand.RuntimeRunIfWaitingForDebugger,
        params: {},
    },
    {
        id: reloadCommandId.create(),
        method: types_1.ChromeCommand.DOMGetDocument,
        params: {},
    },
];
const reloadEvent = [
    {
        method: types_1.ChromeEvent.DOMDocumentUpdated,
        params: {},
    },
];
const publishReloadCommand = (debugTarget) => {
    setTimeout(() => {
        const { clientId } = debugTarget;
        const upwardChannelId = pub_sub_channel_1.createUpwardChannel(clientId);
        const downwardChannelId = pub_sub_channel_1.createDownwardChannel(clientId);
        const { Publisher } = db_1.getDBOperator();
        const publisher = new Publisher(upwardChannelId);
        const downPublisher = new Publisher(downwardChannelId);
        reloadCommand.forEach((command) => {
            publisher.publish(JSON.stringify(command));
        });
        reloadEvent.forEach((event) => {
            downPublisher.publish(JSON.stringify(event));
        });
    }, 2000);
};
exports.publishReloadCommand = publishReloadCommand;
const publishRes = (clientId, res) => {
    const downwardChannelId = pub_sub_channel_1.createDownwardChannel(clientId);
    const { Publisher } = db_1.getDBOperator();
    const downPublisher = new Publisher(downwardChannelId);
    downPublisher.publish(JSON.stringify(res));
};
exports.publishRes = publishRes;
