"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.makeExtractText = void 0;
const transform_target_1 = require("./utils/transform-target");
const utils = __importStar(require("@applitools/utils"));
function makeExtractText({ requests, agentId: defaultAgentId, logger: mainLogger }) {
    return async function extractText({ target, settings, logger = mainLogger, }) {
        logger = logger.extend(mainLogger, { tags: [`extract-text-base-${utils.general.shortid()}`] });
        settings = await Promise.all((utils.types.isArray(settings) ? settings : [settings]).map(async (settings) => {
            var _a, _b, _c, _d, _e;
            const account = await requests.getAccountInfo({ settings, logger });
            (_a = settings.normalization) !== null && _a !== void 0 ? _a : (settings.normalization = {});
            settings.normalization.limit = {
                maxImageHeight: Math.min((_c = (_b = settings.normalization.limit) === null || _b === void 0 ? void 0 : _b.maxImageHeight) !== null && _c !== void 0 ? _c : Infinity, account.maxImageHeight),
                maxImageArea: Math.min((_e = (_d = settings.normalization.limit) === null || _d === void 0 ? void 0 : _d.maxImageArea) !== null && _e !== void 0 ? _e : Infinity, account.maxImageArea),
            };
            settings.agentId = `${defaultAgentId} ${settings.agentId ? `[${settings.agentId}]` : ''}`.trim();
            return settings;
        }));
        logger.log('Command "extractText" is called with settings', settings);
        const results = await Promise.all(settings.map(async (settings) => {
            target = await (0, transform_target_1.transformTarget)({ target, settings });
            return requests.extractText({ target, settings, logger });
        }));
        return results.flat();
    };
}
exports.makeExtractText = makeExtractText;
