"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const util_1 = require("util");
const events_1 = require("events");
const lodash_1 = __importDefault(require("lodash"));
const p_queue_1 = __importDefault(require("p-queue"));
const worker_farm_1 = __importDefault(require("worker-farm"));
const static_1 = require("./lib/report-builder/static");
const plugin_api_1 = require("./lib/plugin-api");
const config_1 = require("./lib/config");
const constants_1 = require("./lib/constants");
const playwright_1 = require("./lib/test-adapter/playwright");
const sqlite_client_1 = require("./lib/sqlite-client");
const image_store_1 = require("./lib/image-store");
const images_info_saver_1 = require("./lib/images-info-saver");
const cache_1 = require("./lib/cache");
const server_utils_1 = require("./lib/server-utils");
class MyReporter {
    constructor(opts) {
        this._promiseQueue = new p_queue_1.default();
        this._config = (0, config_1.parseConfig)(lodash_1.default.omit(opts, ['configDir']));
        this._htmlReporter = plugin_api_1.HtmlReporter.create(this._config, { toolName: constants_1.ToolName.Playwright });
        this._staticReportBuilder = null;
        this._workerFarm = (0, worker_farm_1.default)(require.resolve('./lib/workers/worker'), ['saveDiffTo']);
        const workers = new events_1.EventEmitter();
        workers.saveDiffTo = (imageDiffError, diffPath) => (0, util_1.promisify)(this._workerFarm.saveDiffTo)(imageDiffError, diffPath);
        this._workers = workers;
        this._initPromise = (async (htmlReporter, config) => {
            const dbClient = await sqlite_client_1.SqliteClient.create({ htmlReporter, reportPath: config.path });
            const imageStore = new image_store_1.SqliteImageStore(dbClient);
            const expectedPathsCache = new cache_1.Cache(server_utils_1.getExpectedCacheKey);
            const imagesInfoSaver = new images_info_saver_1.ImagesInfoSaver({
                imageFileSaver: htmlReporter.imagesSaver,
                expectedPathsCache,
                imageStore,
                reportPath: htmlReporter.config.path
            });
            this._staticReportBuilder = static_1.StaticReportBuilder.create(htmlReporter, config, { dbClient, imagesInfoSaver });
            this._staticReportBuilder.registerWorkers(workers);
            await this._staticReportBuilder.saveStaticFiles();
        })(this._htmlReporter, this._config);
        this._promiseQueue.add(async () => this._initPromise);
    }
    onTestEnd(test, result) {
        this._promiseQueue.add(async () => {
            await this._initPromise;
            const staticReportBuilder = this._staticReportBuilder;
            const formattedResult = new playwright_1.PlaywrightTestAdapter(test, result, constants_1.UNKNOWN_ATTEMPT);
            await staticReportBuilder.addTestResult(formattedResult);
        });
    }
    async onEnd() {
        await this._promiseQueue.onIdle();
        await this._staticReportBuilder?.finalize();
        await this._htmlReporter.emitAsync(constants_1.PluginEvents.REPORT_SAVED);
    }
}
exports.default = MyReporter;
//# sourceMappingURL=playwright.js.map