"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.DebugTargetManager = void 0;
const config_1 = require("@debug-server-next/config");
const iwdp_1 = require("@debug-server-next/utils/iwdp");
const debug_target_1 = require("@debug-server-next/utils/debug-target");
const db_1 = require("@debug-server-next/db");
const enum_1 = require("@debug-server-next/@types/enum");
const pub_sub_manager_1 = require("./pub-sub-manager");
class DebugTargetManager {
    /**
     * find debugTarget by clientId
     *
     * @static
     * @param {string} clientId
     * @param {string} hash auth control
     * @param {boolean} [ignoreHash=false] only use in server side, for vue-devtools find current debug ContextName
     * @return {*}
     * @memberof DebugTargetManager
     */
    static async findDebugTarget(clientId, hash, ignoreHash = false) {
        const debugTargets = await DebugTargetManager.getDebugTargets(hash, ignoreHash);
        return debugTargets.find((target) => target.clientId === clientId);
    }
}
exports.DebugTargetManager = DebugTargetManager;
DebugTargetManager.debugTargets = [];
/**
 * find all debugTargets
 *
 * @static
 * @param {string} hash to filter list
 * @param {boolean} [ignoreHash=false] only use in server side, for vue-devtools find current debug ContextName
 * @memberof DebugTargetManager
 */
DebugTargetManager.getDebugTargets = async (hash, ignoreHash = false) => {
    const { iWDPPort, enableIOS } = global.debugAppArgv;
    const { DB } = db_1.getDBOperator();
    const db = new DB(config_1.config.redis.debugTargetTable);
    let getDebugTargetPromise;
    if (config_1.config.isCluster && !ignoreHash) {
        getDebugTargetPromise = hash ? db.find('hash', hash) : Promise.resolve([]);
    }
    else {
        getDebugTargetPromise = db.getAll();
    }
    const [targets, iOSPages] = await Promise.all([
        getDebugTargetPromise,
        enableIOS ? iwdp_1.getIWDPPages(iWDPPort) : Promise.resolve([]),
    ]);
    targets.forEach((target, i) => {
        if (target.platform === enum_1.DevicePlatform.IOS) {
            targets[i] = iwdp_1.patchIOSTarget(target, iOSPages);
            pub_sub_manager_1.updateIWDPAppClient(targets[i]);
        }
    });
    // append H5 pages from IWDP
    const iOSPagesWithFlag = iOSPages;
    const h5Pages = iOSPagesWithFlag.filter(
    // (iOSPage) => !iOSPage.shouldRemove && iOSPage.device.deviceName !== SIMULATOR_DEVICE_NAME,
    (iOSPage) => !iOSPage.shouldRemove && !iOSPage.title.startsWith('HippyContext: '));
    const h5DebugTargets = h5Pages.map(debug_target_1.createTargetByIWDPPage);
    pub_sub_manager_1.subscribeByIWDP(h5DebugTargets);
    DebugTargetManager.debugTargets = targets.concat(h5DebugTargets);
    return DebugTargetManager.debugTargets;
};
