"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.ClassicRunner = exports.VisualGridRunner = exports.EyesRunner = void 0;
const NewTestError_1 = require("./errors/NewTestError");
const DiffsFoundError_1 = require("./errors/DiffsFoundError");
const TestFailedError_1 = require("./errors/TestFailedError");
const RunnerOptions_1 = require("./input/RunnerOptions");
const TestResults_1 = require("./output/TestResults");
const TestResultsSummary_1 = require("./output/TestResultsSummary");
const utils = __importStar(require("@applitools/utils"));
class EyesRunner {
    constructor(options) {
        /** @internal */
        this._managerSettings = {};
        /** @internal */
        this._getResultsSettings = {};
        if (options) {
            this._managerSettings = {
                concurrency: options.testConcurrency,
                legacyConcurrency: options.legacyConcurrency,
                fetchConcurrency: options.fetchConcurrency,
            };
            this._getResultsSettings = { removeDuplicateTests: options.removeDuplicateTests };
        }
    }
    /** @internal */
    attach(core) {
        var _a;
        (_a = this._core) !== null && _a !== void 0 ? _a : (this._core = core);
    }
    /** @internal */
    async openEyes(options) {
        var _a;
        (_a = this._manager) !== null && _a !== void 0 ? _a : (this._manager = await this._core.makeManager({
            type: this.type,
            settings: this._managerSettings,
            logger: options.logger,
        }));
        return await this._manager.openEyes(options);
    }
    async getAllTestResults(throwErr = true) {
        var _a;
        if (!this._manager)
            return new TestResultsSummary_1.TestResultsSummaryData();
        try {
            const summary = await this._manager.getResults({ settings: { throwErr, ...this._getResultsSettings } });
            return new TestResultsSummary_1.TestResultsSummaryData({ summary, core: this._core });
        }
        catch (err) {
            if ((_a = err.info) === null || _a === void 0 ? void 0 : _a.result) {
                const result = new TestResults_1.TestResultsData({ result: err.info.result, core: this._core });
                if (err.reason === 'test failed') {
                    throw new TestFailedError_1.TestFailedError(err.message, result);
                }
                else if (err.reason === 'test different') {
                    throw new DiffsFoundError_1.DiffsFoundError(err.message, result);
                }
                else if (err.reason === 'test new') {
                    throw new NewTestError_1.NewTestError(err.message, result);
                }
            }
            throw err;
        }
    }
}
exports.EyesRunner = EyesRunner;
class VisualGridRunner extends EyesRunner {
    constructor(optionsOrLegacyConcurrency) {
        let options;
        if (utils.types.instanceOf(optionsOrLegacyConcurrency, RunnerOptions_1.RunnerOptionsFluent)) {
            options = optionsOrLegacyConcurrency.toJSON();
        }
        else if (utils.types.isNumber(optionsOrLegacyConcurrency)) {
            options = { legacyConcurrency: optionsOrLegacyConcurrency };
        }
        else {
            options = optionsOrLegacyConcurrency;
        }
        super(options);
        this.type = 'ufg';
    }
    get testConcurrency() {
        var _a;
        return (_a = this._managerSettings) === null || _a === void 0 ? void 0 : _a.concurrency;
    }
    /** @deprecated */
    get legacyConcurrency() {
        var _a;
        return (_a = this._managerSettings) === null || _a === void 0 ? void 0 : _a.legacyConcurrency;
    }
    /** @deprecated */
    getConcurrentSessions() {
        var _a;
        return (_a = this._managerSettings) === null || _a === void 0 ? void 0 : _a.legacyConcurrency;
    }
}
exports.VisualGridRunner = VisualGridRunner;
class ClassicRunner extends EyesRunner {
    constructor() {
        super(...arguments);
        this.type = 'classic';
    }
}
exports.ClassicRunner = ClassicRunner;
