"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 driver_1 = require("@applitools/driver");
const take_screenshot_1 = require("./utils/take-screenshot");
const take_dom_capture_1 = require("../classic/utils/take-dom-capture");
const utils = __importStar(require("@applitools/utils"));
const { getText } = require('@applitools/snippets');
function makeExtractText({ core, spec, logger: mainLogger }) {
    return async function extractText({ target, settings, logger = mainLogger, }) {
        logger = logger.extend(mainLogger);
        logger.log('Command "extractText" is called with settings', settings);
        if (!(0, driver_1.isDriver)(target, spec)) {
            return core.base.extractText({ target, settings: settings, logger });
        }
        settings = utils.types.isArray(settings) ? settings : [settings];
        const driver = await (0, driver_1.makeDriver)({ spec, driver: target, logger });
        const environment = await driver.getEnvironment();
        const results = await settings.reduce(async (prev, settings) => {
            var _a;
            const steps = await prev;
            const screenshot = await (0, take_screenshot_1.takeScreenshot)({ driver, settings, logger });
            if (!settings.hint && (0, driver_1.isElementReference)(settings.region, spec)) {
                const element = await driver.currentContext.element(settings.region);
                if (!element)
                    throw new Error(`Unable to find element using provided selector`);
                settings.hint = await driver.currentContext.execute(getText, [element]);
                if (settings.hint)
                    settings.hint = settings.hint.replace(/[.\\+]/g, '\\$&');
            }
            const baseTarget = {
                image: await screenshot.image.toPng(),
                size: utils.geometry.size(screenshot.region),
                locationInViewport: utils.geometry.location(screenshot.region),
            };
            if (environment.isWeb) {
                if (settings.fully)
                    await screenshot.scrollingElement.setAttribute('data-applitools-scroll', 'true');
                else
                    await ((_a = screenshot.element) === null || _a === void 0 ? void 0 : _a.setAttribute('data-applitools-scroll', 'true'));
                baseTarget.dom = await (0, take_dom_capture_1.takeDomCapture)({ driver, settings: { proxy: settings.proxy }, logger }).catch(() => undefined);
            }
            delete settings.region;
            delete settings.normalization;
            const results = await core.base.extractText({
                target: baseTarget,
                settings: settings,
                logger,
            });
            steps.push(results);
            return steps;
        }, Promise.resolve([]));
        return results.flat();
    };
}
exports.makeExtractText = makeExtractText;
