"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DebugTargetManager = void 0;
const config_1 = require("@/config");
const iwdp_1 = require("@/utils/iwdp");
const debug_target_1 = require("@/utils/debug-target");
const db_1 = require("@/db");
const enum_1 = require("@/@types/enum");
// import { SIMULATOR_DEVICE_NAME } from '@/@types/constants';
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 auth control
 * @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 } = 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, iwdp_1.getIWDPPages(iWDPPort)]);
    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]);
        }
    });
    // 追加 IWDP 获取到的 h5 页面
    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;
};
