"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.makeCheck = void 0;
const transform_target_1 = require("./utils/transform-target");
const transform_dom_mapping_1 = require("./utils/transform-dom-mapping");
const utils = __importStar(require("@applitools/utils"));
function makeCheck({ requests, signal, logger: mainLogger }) {
    const queue = [];
    return async function check({ target, settings = {}, logger = mainLogger, }) {
        var _a, _b, _c, _d, _e, _f, _g, _h;
        var _j, _k;
        logger = logger.extend(mainLogger, { tags: [`check-base-${utils.general.shortid()}`] });
        settings !== null && settings !== void 0 ? settings : (settings = {});
        (_a = settings.stepIndex) !== null && _a !== void 0 ? _a : (settings.stepIndex = queue.length);
        (_b = settings.normalization) !== null && _b !== void 0 ? _b : (settings.normalization = {});
        settings.normalization.limit = {
            maxImageHeight: Math.min((_d = (_c = settings.normalization.limit) === null || _c === void 0 ? void 0 : _c.maxImageHeight) !== null && _d !== void 0 ? _d : Infinity, requests.test.account.maxImageHeight),
            maxImageArea: Math.min((_f = (_e = settings.normalization.limit) === null || _e === void 0 ? void 0 : _e.maxImageArea) !== null && _f !== void 0 ? _f : Infinity, requests.test.account.maxImageArea),
        };
        await (0, transform_dom_mapping_1.transformDomMapping)(settings);
        logger.log('Command "check" is called with settings', settings);
        (_g = queue[_j = settings.stepIndex]) !== null && _g !== void 0 ? _g : (queue[_j] = utils.promises.makeControlledPromise());
        const aborted = new Promise((_, reject) => {
            const abort = () => reject(new Error('Command "check" was aborted due to possible error in previous step'));
            signal.addEventListener('abort', abort);
            if (signal.aborted)
                abort();
        });
        target = await (0, transform_target_1.transformTarget)({ target, settings });
        if (settings.stepIndex > 0) {
            await Promise.race([((_h = queue[_k = settings.stepIndex - 1]) !== null && _h !== void 0 ? _h : (queue[_k] = utils.promises.makeControlledPromise())), aborted]);
        }
        return Promise.race([requests.check({ target, settings, logger }), aborted]).finally(queue[settings.stepIndex].resolve);
    };
}
exports.makeCheck = makeCheck;
