"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.getHistoryProtocol = exports.clearHistoryProtocol = exports.saveHistoryProtocol = exports.isHistoryProtocol = void 0;
/**
 * cache all history event to redis to support for history protocol for iOS, android V8 support history log natively.
 *
 * if devtools had not attached, cache all history logs to redis,
 * clean cached data when app disconnect,
 * consume by re-Pub all history logs when devtools connect, in this case will broadcast to
 * all connected devtools, so some devtools client maybe receive log twice.
 */
const types_1 = require("@hippy/devtools-protocol/dist/types");
const db_1 = require("@debug-server-next/db");
const config_1 = require("@debug-server-next/config");
const enum_1 = require("@debug-server-next/@types/enum");
const getHistoryKey = (clientId) => `${config_1.config.redis.logTablePrefix}${clientId}`;
/**
 * cache log, network event when devtools have not opened,
 * those protocol is dispatched in app create lifecycle
 */
const isHistoryProtocol = (method, platform) => {
    if (platform === enum_1.DevicePlatform.IOS &&
        [types_1.ChromeEvent.RuntimeConsoleAPICalled, types_1.ChromeEvent.LogEntryAdded, types_1.IOS100Event.ConsoleMessageAdded].includes(method))
        return true;
    if (method === null || method === void 0 ? void 0 : method.startsWith('Network.'))
        return true;
    return false;
};
exports.isHistoryProtocol = isHistoryProtocol;
const saveHistoryProtocol = async (clientId, msg) => {
    const { DB } = (0, db_1.getDBOperator)();
    const db = new DB(getHistoryKey(clientId));
    await db.rPush(msg);
};
exports.saveHistoryProtocol = saveHistoryProtocol;
const clearHistoryProtocol = async (clientId) => {
    const { DB } = (0, db_1.getDBOperator)();
    const db = new DB(getHistoryKey(clientId));
    await db.clearList();
};
exports.clearHistoryProtocol = clearHistoryProtocol;
const getHistoryProtocol = async (clientId) => {
    const { DB } = (0, db_1.getDBOperator)();
    const db = new DB(getHistoryKey(clientId));
    return await db.getList();
};
exports.getHistoryProtocol = getHistoryProtocol;
