"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 core_1 = require("./classic/core");
const core_2 = require("./ufg/core");
const get_typed_eyes_1 = require("./get-typed-eyes");
const check_1 = require("./check");
const check_and_close_1 = require("./check-and-close");
const close_1 = require("./close");
const abort_1 = require("./abort");
const get_eyes_results_1 = require("./get-eyes-results");
const populate_eyes_server_settings_1 = require("./utils/populate-eyes-server-settings");
const utils = __importStar(require("@applitools/utils"));
function makeOpenEyes({ type: defaultType = 'classic', clients, batch, core, cores, spec, environment, logger: mainLogger, asyncCache, }) {
    return async function openEyes({ type = defaultType, settings, config, target, logger = mainLogger, }) {
        var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s;
        var _t, _u, _v, _w, _x;
        logger = logger.extend(mainLogger, { tags: [`eyes-${type}-${utils.general.shortid()}`] });
        settings = { ...config === null || config === void 0 ? void 0 : config.open, ...settings };
        (0, populate_eyes_server_settings_1.populateEyesServerSettings)(settings);
        (_a = settings.userTestId) !== null && _a !== void 0 ? _a : (settings.userTestId = `${settings.testName}--${utils.general.guid()}`);
        settings.batch = { ...batch, ...settings.batch };
        (_b = (_t = settings.batch).id) !== null && _b !== void 0 ? _b : (_t.id = (_c = utils.general.getEnvValue('BATCH_ID')) !== null && _c !== void 0 ? _c : `generated-${utils.general.guid()}`);
        (_d = (_u = settings.batch).buildId) !== null && _d !== void 0 ? _d : (_u.buildId = utils.general.getEnvValue('BATCH_BUILD_ID'));
        (_e = (_v = settings.batch).name) !== null && _e !== void 0 ? _e : (_v.name = utils.general.getEnvValue('BATCH_NAME'));
        (_f = (_w = settings.batch).sequenceName) !== null && _f !== void 0 ? _f : (_w.sequenceName = utils.general.getEnvValue('BATCH_SEQUENCE'));
        (_g = (_x = settings.batch).notifyOnCompletion) !== null && _g !== void 0 ? _g : (_x.notifyOnCompletion = utils.general.getEnvValue('BATCH_NOTIFY', 'boolean'));
        (_h = settings.keepBatchOpen) !== null && _h !== void 0 ? _h : (settings.keepBatchOpen = utils.general.getEnvValue('DONT_CLOSE_BATCHES', 'boolean'));
        (_j = settings.branchName) !== null && _j !== void 0 ? _j : (settings.branchName = utils.general.getEnvValue('BRANCH'));
        (_k = settings.parentBranchName) !== null && _k !== void 0 ? _k : (settings.parentBranchName = utils.general.getEnvValue('PARENT_BRANCH'));
        (_l = settings.baselineBranchName) !== null && _l !== void 0 ? _l : (settings.baselineBranchName = utils.general.getEnvValue('BASELINE_BRANCH'));
        (_m = settings.gitBranchingTimestamp) !== null && _m !== void 0 ? _m : (settings.gitBranchingTimestamp = utils.general.getEnvValue('GIT_MERGE_BASE_TIMESTAMP'));
        (_o = settings.ufgServerUrl) !== null && _o !== void 0 ? _o : (settings.ufgServerUrl = utils.general.getEnvValue('UFG_SERVER_URL'));
        (_p = settings.ignoreBaseline) !== null && _p !== void 0 ? _p : (settings.ignoreBaseline = false);
        (_q = settings.compareWithParentBranch) !== null && _q !== void 0 ? _q : (settings.compareWithParentBranch = false);
        const driver = target && (await (0, driver_1.makeDriver)({ spec, driver: target, logger, customConfig: settings }));
        const driverEnvironment = await (driver === null || driver === void 0 ? void 0 : driver.getEnvironment());
        const driverUrl = await (driver === null || driver === void 0 ? void 0 : driver.getDriverUrl());
        core.logEvent({
            settings: {
                ...settings,
                level: 'Notice',
                event: {
                    type: 'openEyes',
                    userTestId: settings.userTestId,
                    concurrency: (_r = cores === null || cores === void 0 ? void 0 : cores[type].concurrency) !== null && _r !== void 0 ? _r : core.concurrency,
                    environment,
                    driver: {
                        deviceName: driverEnvironment === null || driverEnvironment === void 0 ? void 0 : driverEnvironment.deviceName,
                        browserName: driverEnvironment === null || driverEnvironment === void 0 ? void 0 : driverEnvironment.browserName,
                        browserVersion: driverEnvironment === null || driverEnvironment === void 0 ? void 0 : driverEnvironment.browserVersion,
                        platformName: driverEnvironment === null || driverEnvironment === void 0 ? void 0 : driverEnvironment.platformName,
                        platformVersion: driverEnvironment === null || driverEnvironment === void 0 ? void 0 : driverEnvironment.platformVersion,
                        isApplitoolsLib: (_s = driverEnvironment === null || driverEnvironment === void 0 ? void 0 : driverEnvironment.applitoolsLib) === null || _s === void 0 ? void 0 : _s.instrumented,
                        isEC: driverEnvironment === null || driverEnvironment === void 0 ? void 0 : driverEnvironment.isEC,
                    },
                    driverUrl,
                },
            },
            logger,
        });
        const getTypedEyes = (0, get_typed_eyes_1.makeGetTypedEyes)({
            type,
            settings: settings,
            target: driver,
            cores: cores !== null && cores !== void 0 ? cores : {
                ufg: (0, core_2.makeCore)({ spec, clients, base: core.base, asyncCache, logger }),
                classic: (0, core_1.makeCore)({ spec, clients, base: core.base, logger }),
            },
            logger,
        });
        const eyes = await getTypedEyes({ logger });
        return utils.general.extend(eyes, eyes => {
            var _a, _b;
            return ({
                getTypedEyes,
                check: (0, check_1.makeCheck)({ type, eyes, target: driver, spec, logger }),
                checkAndClose: (0, check_and_close_1.makeCheckAndClose)({ type, eyes, target: driver, spec, logger }),
                close: (0, close_1.makeClose)({ eyes, environments: (_a = config === null || config === void 0 ? void 0 : config.check) === null || _a === void 0 ? void 0 : _a.environments, logger }),
                abort: (0, abort_1.makeAbort)({ eyes, environments: (_b = config === null || config === void 0 ? void 0 : config.check) === null || _b === void 0 ? void 0 : _b.environments, logger }),
                getResults: (0, get_eyes_results_1.makeGetEyesResults)({ eyes, logger }),
            });
        }); // TODO solve the types issue
    };
}
exports.makeOpenEyes = makeOpenEyes;
