"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.makeOpenEyes = void 0;
const driver_1 = require("@applitools/driver");
const get_base_eyes_1 = require("./get-base-eyes");
const check_1 = require("./check");
const check_and_close_1 = require("./check-and-close");
const close_1 = require("../automation/close");
const abort_1 = require("../automation/abort");
const get_results_1 = require("../automation/get-results");
const extract_default_environment_1 = require("./utils/extract-default-environment");
const abort_controller_1 = require("abort-controller");
const utils = __importStar(require("@applitools/utils"));
function makeOpenEyes({ core, spec, logger: mainLogger }) {
    return async function openEyes({ target, settings, storage = new Map(), logger = mainLogger, }) {
        var _a, _b, _c;
        logger = logger.extend(mainLogger);
        logger.log(`Command "openEyes" is called with ${target ? 'default driver' : ''}`, ...(settings ? ['and settings', settings] : []), storage.size > 0 ? 'and default eyes storage' : '');
        const driver = target && (await (0, driver_1.makeDriver)({ spec, driver: target, logger }));
        if (driver && storage.size === 0) {
            const environment = await driver.getEnvironment();
            if (((_a = settings.environment) === null || _a === void 0 ? void 0 : _a.viewportSize) && !environment.isMobile) {
                await driver.setViewportSize(settings.environment.viewportSize);
            }
            (_b = settings.environment) !== null && _b !== void 0 ? _b : (settings.environment = {});
            if (environment.isEC) {
                settings.environment.ecSessionId = (_c = (await driver.getSessionId())) !== null && _c !== void 0 ? _c : undefined;
            }
        }
        const environments = await (0, extract_default_environment_1.extractDefaultEnvironments)({ driver });
        const controller = new abort_controller_1.AbortController();
        const account = await core.getAccountInfo({ settings, logger });
        return utils.general.extend({}, eyes => {
            var _a, _b, _c, _d;
            return {
                type: 'ufg',
                core,
                test: {
                    userTestId: settings.userTestId,
                    batchId: (_a = settings.batch) === null || _a === void 0 ? void 0 : _a.id,
                    batchBuildId: (_b = settings.batch) === null || _b === void 0 ? void 0 : _b.buildId,
                    keepBatchOpen: settings.keepBatchOpen,
                    eyesServer: account.eyesServer,
                    ufgServer: { ...account.ufgServer, ufgServerUrl: (_c = settings.ufgServerUrl) !== null && _c !== void 0 ? _c : (_d = account.ufgServer) === null || _d === void 0 ? void 0 : _d.ufgServerUrl },
                    uploadUrl: account.uploadUrl,
                    stitchingServiceUrl: account.stitchingServiceUrl,
                    supportedEnvironmentsUrl: account.supportedEnvironmentsUrl,
                    account,
                },
                running: true,
                storage,
                getBaseEyes: (0, get_base_eyes_1.makeGetBaseEyes)({ settings, eyes, logger }),
                check: (0, check_1.makeCheck)({ eyes, target: driver, environments, spec, signal: controller.signal, logger }),
                checkAndClose: (0, check_and_close_1.makeCheckAndClose)({ eyes, target: driver, environments, spec, signal: controller.signal, logger }),
                close: (0, close_1.makeClose)({ eyes, target: driver, logger }),
                abort: (0, abort_1.makeAbort)({ eyes, target: driver, spec, controller, logger }),
                getResults: (0, get_results_1.makeGetResults)({ eyes, logger }),
            };
        });
    };
}
exports.makeOpenEyes = makeOpenEyes;
