"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.getHistoryLogProtocol = exports.clearLogProtocol = exports.saveLogProtocol = exports.isLogProtocol = void 0;
/**
 * cache all logs to redis to support for history log 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 getLogKey = (clientId) => `${config_1.config.redis.logTablePrefix}${clientId}`;
const isLogProtocol = (method) => [types_1.ChromeEvent.RuntimeConsoleAPICalled, types_1.ChromeEvent.LogEntryAdded, types_1.IOS100Event.ConsoleMessageAdded].includes(method);
exports.isLogProtocol = isLogProtocol;
const saveLogProtocol = async (clientId, msg) => {
    const { DB } = db_1.getDBOperator();
    const db = new DB(getLogKey(clientId));
    await db.rPush(msg);
};
exports.saveLogProtocol = saveLogProtocol;
const clearLogProtocol = async (clientId) => {
    const { DB } = db_1.getDBOperator();
    const db = new DB(getLogKey(clientId));
    await db.clearList();
};
exports.clearLogProtocol = clearLogProtocol;
const getHistoryLogProtocol = async (clientId) => {
    const { DB } = db_1.getDBOperator();
    const db = new DB(getLogKey(clientId));
    return await db.getList();
};
exports.getHistoryLogProtocol = getHistoryLogProtocol;
