"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CURL_CMD = exports.RENDER_INFO_URL = void 0;
const https_1 = require("https");
const core_base_1 = require("@applitools/core-base");
const utils_1 = require("./utils");
const utils = __importStar(require("@applitools/utils"));
const node_fetch_1 = __importDefault(require("node-fetch"));
exports.RENDER_INFO_URL = `${utils_1.config.serverUrl}/api/sessions/renderinfo?apiKey=${utils_1.config.apiKey}`;
exports.CURL_CMD = `curl ${exports.RENDER_INFO_URL} ${(0, utils_1.getProxyCurlArg)()}`;
const validateRawAccountInfo = (res) => {
    if (!res || !res.accessToken || !res.resultsUrl) {
        throw new Error(`bad render info result ${JSON.stringify(res)}`);
    }
};
const validateAccountInfo = (res) => {
    if (!res || !res.ufg || !res.ufg.accessToken || !res.uploadUrl) {
        throw new Error(`bad render info result ${JSON.stringify(res)}`);
    }
};
exports.default = {
    async testFetch() {
        const response = await (0, node_fetch_1.default)(exports.RENDER_INFO_URL);
        const data = await response.json();
        validateRawAccountInfo(data);
    },
    async testCurl() {
        const { stdout } = await utils.process.execute(exports.CURL_CMD, {
            maxBuffer: 10000000,
        });
        const data = JSON.parse(stdout);
        validateRawAccountInfo(data);
    },
    async testServer() {
        const server = (0, core_base_1.makeCoreRequestsWithCache)();
        const result = await server.getAccountInfo({ settings: utils_1.config });
        validateAccountInfo(result);
    },
    testHttps: async () => {
        return new Promise((resolve, reject) => {
            const request = (0, https_1.get)(exports.RENDER_INFO_URL, response => {
                let data = '';
                response.on('data', chunk => (data += chunk));
                response.on('end', () => {
                    try {
                        validateRawAccountInfo(JSON.parse(data));
                        resolve();
                    }
                    catch (err) {
                        reject(err);
                    }
                });
            });
            request.on('error', reject);
        });
    },
    url: new URL(exports.RENDER_INFO_URL),
    cmd: exports.CURL_CMD,
};
