"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.publishRes = exports.publishEvaluateCommand = exports.runtimeEvaluateCommandId = exports.publishReloadCommand = exports.resumeCommands = void 0;
const types_1 = require("@hippy/devtools-protocol/dist/types");
const db_1 = require("@debug-server-next/db");
const pub_sub_channel_1 = require("@debug-server-next/utils/pub-sub-channel");
const report_1 = require("@debug-server-next/utils/report");
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 resumeCommandId = new global_id_1.GlobalId(-100000, -1);
exports.resumeCommands = [
    {
        id: resumeCommandId.create(),
        method: types_1.TdfCommand.TDFRuntimeResume,
        params: {},
        performance: (0, report_1.createCDPPerformance)(),
    },
    {
        id: resumeCommandId.create(),
        method: types_1.ChromeCommand.DebuggerDisable,
        params: {},
        performance: (0, report_1.createCDPPerformance)(),
    },
    {
        id: resumeCommandId.create(),
        method: types_1.ChromeCommand.RuntimeDisable,
        params: {},
        performance: (0, report_1.createCDPPerformance)(),
    },
];
const publishReloadCommand = (debugTarget) => {
    setTimeout(async () => {
        const { clientId } = debugTarget;
        const upwardChannelId = (0, pub_sub_channel_1.createUpwardChannel)(clientId);
        const downwardChannelId = (0, pub_sub_channel_1.createDownwardChannel)(clientId);
        const { Publisher } = (0, db_1.getDBOperator)();
        const publisher = new Publisher(upwardChannelId);
        const downPublisher = new Publisher(downwardChannelId);
        await Promise.all(reloadCommand.map(publisher.publish.bind(publisher)));
        await Promise.all(reloadEvent.map(downPublisher.publish.bind(downPublisher)));
        publisher.disconnect();
        downPublisher.disconnect();
    }, 2000);
};
exports.publishReloadCommand = publishReloadCommand;
exports.runtimeEvaluateCommandId = 20200101; // 随机定义的ID
const publishEvaluateCommand = (clientId, expression) => {
    const upwardChannelId = (0, pub_sub_channel_1.createUpwardChannel)(clientId);
    const { Publisher } = (0, db_1.getDBOperator)();
    const publisher = new Publisher(upwardChannelId);
    publisher.publish(JSON.stringify({
        id: exports.runtimeEvaluateCommandId,
        method: 'Runtime.evaluate',
        params: {
            expression,
        },
    }));
};
exports.publishEvaluateCommand = publishEvaluateCommand;
const publishRes = (clientId, res) => {
    const downwardChannelId = (0, pub_sub_channel_1.createDownwardChannel)(clientId);
    const { Publisher } = (0, db_1.getDBOperator)();
    const downPublisher = new Publisher(downwardChannelId);
    downPublisher.publish(JSON.stringify(res));
};
exports.publishRes = publishRes;
