"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.makeOpenFunctionalSession = void 0;
const abort_controller_1 = require("abort-controller");
const close_1 = require("./close");
const abort_1 = require("./abort");
const get_results_1 = require("./get-results");
const throat_1 = __importDefault(require("throat"));
const utils = __importStar(require("@applitools/utils"));
function makeOpenFunctionalSession({ requests, agentId: defaultAgentId, concurrency, logger: mainLogger, }) {
    const throttle = concurrency ? (0, throat_1.default)(concurrency) : (fn) => fn();
    return async function openFunctionalSession({ settings, logger = mainLogger, }) {
        logger = logger.extend(mainLogger, { tags: [`functional-session-base-${utils.general.shortid()}`] });
        settings.agentId = `${defaultAgentId} ${settings.agentId ? `[${settings.agentId}]` : ''}`.trim();
        logger.log('Command "openFunctionalSession" is called with settings', settings);
        return new Promise((resolve, reject) => {
            throttle(() => {
                return new Promise(async (done) => {
                    try {
                        const controller = new abort_controller_1.AbortController();
                        const functionalSessionRequests = await requests.openFunctionalSession({ settings, logger });
                        resolve(utils.general.extend(functionalSessionRequests, {
                            close: (0, close_1.makeClose)({ requests: functionalSessionRequests, done, logger }),
                            abort: (0, abort_1.makeAbort)({ requests: functionalSessionRequests, done, controller, logger }),
                            getResults: (0, get_results_1.makeGetResults)({ requests: functionalSessionRequests, logger }),
                        }));
                    }
                    catch (error) {
                        reject(error);
                        done();
                    }
                });
            });
        });
    };
}
exports.makeOpenFunctionalSession = makeOpenFunctionalSession;
