"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.makeMakeManager = void 0;
const core_base_1 = require("@applitools/core-base");
const core_1 = require("./classic/core");
const core_2 = require("./ufg/core");
const open_eyes_1 = require("./open-eyes");
const get_manager_results_1 = require("./get-manager-results");
const utils = __importStar(require("@applitools/utils"));
function makeMakeManager({ spec, clients, core, base: defaultBase, agentId: defaultAgentId, environment, cwd = process.cwd(), logger: mainLogger, asyncCache, }) {
    return async function makeManager({ type = 'classic', settings, logger = mainLogger, } = {}) {
        var _a, _b, _c, _d, _e, _f, _g;
        var _h, _j;
        logger = logger.extend(mainLogger, { tags: [`manager-${type}-${utils.general.shortid()}`] });
        settings !== null && settings !== void 0 ? settings : (settings = {});
        (_a = settings.concurrency) !== null && _a !== void 0 ? _a : (settings.concurrency = (_b = utils.general.getEnvValue('CONCURRENCY', 'number')) !== null && _b !== void 0 ? _b : (utils.types.isInteger(settings.legacyConcurrency) ? settings.legacyConcurrency * 5 : 100));
        (_c = settings.batch) !== null && _c !== void 0 ? _c : (settings.batch = {});
        (_d = (_h = settings.batch).id) !== null && _d !== void 0 ? _d : (_h.id = (_e = utils.general.getEnvValue('BATCH_ID')) !== null && _e !== void 0 ? _e : `generated-${utils.general.guid()}`);
        (_f = (_j = settings.batch).buildId) !== null && _f !== void 0 ? _f : (_j.buildId = utils.general.getEnvValue('BATCH_BUILD_ID'));
        (_g = settings.agentId) !== null && _g !== void 0 ? _g : (settings.agentId = type === 'ufg' ? defaultAgentId === null || defaultAgentId === void 0 ? void 0 : defaultAgentId.replace(/(\/\d)/, '.visualgrid$1') : defaultAgentId);
        logger.log('Command "makeManager" is called with settings', settings);
        const base = defaultBase !== null && defaultBase !== void 0 ? defaultBase : (0, core_base_1.makeCore)({ agentId: settings.agentId, concurrency: settings.concurrency, cwd, logger });
        const cores = {
            ufg: (0, core_2.makeCore)({ spec, base, fetchConcurrency: settings.fetchConcurrency, asyncCache, logger }),
            classic: (0, core_1.makeCore)({ spec, base, logger }),
        };
        const storage = [];
        return {
            get concurrency() {
                return base.concurrency;
            },
            base,
            openEyes: utils.general.wrap((0, open_eyes_1.makeOpenEyes)({ type, clients, batch: settings.batch, spec, core, cores, environment, asyncCache, logger }), async (openEyes, options) => {
                const eyes = await openEyes(options);
                storage.push(eyes);
                return eyes;
            }),
            getResults: (0, get_manager_results_1.makeGetManagerResults)({ core, storage, logger }),
        };
    };
}
exports.makeMakeManager = makeMakeManager;
