"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;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeECClient = void 0;
const logger_1 = require("@applitools/logger");
const server_1 = require("./server");
const utils = __importStar(require("@applitools/utils"));
async function makeECClient({ settings, logger: defaultLogger, } = {}) {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9;
    var _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29;
    const logger = (0, logger_1.makeLogger)({ logger: defaultLogger, format: { label: 'ec-client' } });
    settings !== null && settings !== void 0 ? settings : (settings = {});
    (_a = settings.ecServerUrl) !== null && _a !== void 0 ? _a : (settings.ecServerUrl = (_c = (_b = utils.general.getEnvValue('EXECUTION_CLOUD_URL')) !== null && _b !== void 0 ? _b : utils.general.getEnvValue('EG_SERVER_URL')) !== null && _c !== void 0 ? _c : 'https://exec-wus.applitools.com');
    (_d = settings.proxy) !== null && _d !== void 0 ? _d : (settings.proxy = utils.general.getEnvValue('PROXY_URL') ? { url: utils.general.getEnvValue('PROXY_URL') } : undefined);
    (_e = settings.useDnsCache) !== null && _e !== void 0 ? _e : (settings.useDnsCache = utils.general.getEnvValue('USE_DNS_CACHE', 'boolean'));
    (_f = settings.tunnel) !== null && _f !== void 0 ? _f : (settings.tunnel = {});
    (_g = (_10 = settings.tunnel).serviceUrl) !== null && _g !== void 0 ? _g : (_10.serviceUrl = utils.general.getEnvValue('EG_TUNNEL_URL'));
    (_h = (_11 = settings.tunnel).groupSize) !== null && _h !== void 0 ? _h : (_11.groupSize = (_j = utils.general.getEnvValue('TUNNEL_GROUP_SIZE', 'number')) !== null && _j !== void 0 ? _j : 2);
    (_k = (_12 = settings.tunnel).pool) !== null && _k !== void 0 ? _k : (_12.pool = {});
    (_l = (_13 = settings.tunnel.pool).maxInuse) !== null && _l !== void 0 ? _l : (_13.maxInuse = (_m = utils.general.getEnvValue('TUNNEL_POOL_MAX_INUSE', 'number')) !== null && _m !== void 0 ? _m : 4);
    (_o = (_14 = settings.tunnel.pool).timeout) !== null && _o !== void 0 ? _o : (_14.timeout = {});
    (_p = (_15 = settings.tunnel.pool.timeout).idle) !== null && _p !== void 0 ? _p : (_15.idle = (_q = utils.general.getEnvValue('TUNNEL_POOL_TIMEOUT_IDLE', 'number')) !== null && _q !== void 0 ? _q : 20 * 60000);
    (_r = (_16 = settings.tunnel.pool.timeout).expiration) !== null && _r !== void 0 ? _r : (_16.expiration = (_s = utils.general.getEnvValue('TUNNEL_POOL_TIMEOUT_EXPIRATION', 'number')) !== null && _s !== void 0 ? _s : 20 * 60000);
    (_t = settings.options) !== null && _t !== void 0 ? _t : (settings.options = {});
    (_u = (_17 = settings.options).eyesServerUrl) !== null && _u !== void 0 ? _u : (_17.eyesServerUrl = (_w = (_v = utils.general.getEnvValue('EYES_SERVER_URL')) !== null && _v !== void 0 ? _v : utils.general.getEnvValue('SERVER_URL')) !== null && _w !== void 0 ? _w : 'https://eyesapi.applitools.com');
    (_x = (_18 = settings.options).apiKey) !== null && _x !== void 0 ? _x : (_18.apiKey = utils.general.getEnvValue('API_KEY'));
    (_y = (_19 = settings.options).region) !== null && _y !== void 0 ? _y : (_19.region = utils.general.getEnvValue('EXECUTION_CLOUD_REGION'));
    (_z = (_20 = settings.options).batch) !== null && _z !== void 0 ? _z : (_20.batch = {});
    (_0 = (_21 = settings.options.batch).id) !== null && _0 !== void 0 ? _0 : (_21.id = (_1 = utils.general.getEnvValue('BATCH_ID')) !== null && _1 !== void 0 ? _1 : `generated-${utils.general.guid()}`);
    (_2 = (_22 = settings.options.batch).name) !== null && _2 !== void 0 ? _2 : (_22.name = utils.general.getEnvValue('BATCH_NAME'));
    (_3 = (_23 = settings.options.batch).sequenceName) !== null && _3 !== void 0 ? _3 : (_23.sequenceName = utils.general.getEnvValue('BATCH_SEQUENCE'));
    (_4 = (_24 = settings.options.batch).notifyOnCompletion) !== null && _4 !== void 0 ? _4 : (_24.notifyOnCompletion = utils.general.getEnvValue('BATCH_NOTIFY', 'boolean'));
    (_5 = (_25 = settings.options).tunnel) !== null && _5 !== void 0 ? _5 : (_25.tunnel = utils.general.getEnvValue('TUNNEL', 'boolean'));
    (_6 = (_26 = settings.options).useSelfHealing) !== null && _6 !== void 0 ? _6 : (_26.useSelfHealing = utils.general.getEnvValue('USE_SELF_HEALING', 'boolean'));
    (_7 = (_27 = settings.options).sessionName) !== null && _7 !== void 0 ? _7 : (_27.sessionName = utils.general.getEnvValue('SESSION_NAME'));
    (_8 = (_28 = settings.options).timeout) !== null && _8 !== void 0 ? _8 : (_28.timeout = utils.general.getEnvValue('EG_TIMEOUT', 'number'));
    (_9 = (_29 = settings.options).inactivityTimeout) !== null && _9 !== void 0 ? _9 : (_29.inactivityTimeout = utils.general.getEnvValue('EG_INACTIVITY_TIMEOUT', 'number'));
    const server = await (0, server_1.makeServer)({ settings: settings, logger });
    return server;
}
exports.makeECClient = makeECClient;
