"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.extractGwInfoFromHandles = extractGwInfoFromHandles;
const IocdWdioServiceError_1 = require("../../../../utils/error/IocdWdioServiceError");
const WdioBrowserHandles_1 = require("./WdioBrowserHandles");
/**
 * Extracts gateway host and token from any open IO.Connect window.
 */
async function extractGwInfoFromHandles(browser) {
    for await (const ctx of WdioBrowserHandles_1.WdioBrowserHandles.iterate(browser)) {
        const res = await ctx.browser.execute(async () => {
            const api = window.glue42gd;
            if (!api)
                return { host: null, token: null };
            try {
                const [token, info] = await Promise.all([api.getGWToken?.(), api.getGWInfo?.()]);
                return { host: info?.url ?? null, token: token ?? null };
            }
            catch {
                return { host: null, token: null };
            }
        });
        if (res.host && res.token) {
            return res;
        }
    }
    throw new IocdWdioServiceError_1.IocdWdioServiceError('Unable to extract GW config. data from platform windows.');
}
