"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const reporters_1 = require("@jest/reporters");
const jest_message_util_1 = require("jest-message-util");
const chalk_1 = __importDefault(require("chalk"));
const utils_1 = require("../../utils");
const consts_1 = require("../../consts");
const runConclusionUtils_1 = require("./runConclusionUtils");
const fs_extra_1 = __importDefault(require("fs-extra"));
class EnhancedDefault {
    constructor(globalConfig, reporterOptions = {}) {
        this.globalConfig = globalConfig;
        this.reporterOptions = reporterOptions;
        this.rootDir = process.cwd();
        this.runId = this.reporterOptions.runId || consts_1.FALLBACK_RUN_ID;
        this.defaultReporter = new reporters_1.DefaultReporter(globalConfig);
        this.summaryReporter = new reporters_1.SummaryReporter(globalConfig);
    }
    async onTestResult(test, testResult, aggregatedResult) {
        const screenplayRunResultsPath = utils_1.constructTestInvocationResultDir(this.rootDir, this.runId);
        const screenplayResults = await runConclusionUtils_1.readRunResultsDirToMap(screenplayRunResultsPath);
        testResult.testResults = testResult.testResults.map((r) => {
            const screenplayTestId = utils_1.testUniqueIdentifierFromStartParams({ projectRoot: this.rootDir, fullSuitePath: testResult.testFilePath, fullName: r.fullName });
            if (screenplayResults.has(screenplayTestId) && r.failureMessages[0]) {
                r.failureMessages[0] = `${chalk_1.default.blue(`To open in Screenplay viewer, run: ${chalk_1.default.underline(`npx screenplay show ${screenplayTestId}`)}`)}\n ${r.failureMessages[0]}'`;
            }
            return r;
        });
        const formattedErrorMessage = jest_message_util_1.formatResultsErrors(testResult.testResults, {
            rootDir: this.globalConfig.rootDir,
            testMatch: [],
        }, {
            noStackTrace: false,
        }, test.path);
        testResult.failureMessage = formattedErrorMessage;
        this.defaultReporter.onTestResult(test, testResult, aggregatedResult);
        this.summaryReporter.onTestResult(test, testResult, aggregatedResult);
    }
    onRunStart(results, options) {
        this.defaultReporter.onRunStart(results, options);
        this.summaryReporter.onRunStart(results, options);
    }
    onTestStart(test) {
        this.defaultReporter.onTestStart(test);
        this.summaryReporter.onTestStart(test);
    }
    async onRunComplete(contexts, results) {
        this.defaultReporter.onRunComplete();
        this.summaryReporter.onRunComplete(contexts, results);
        const screenplayPath = utils_1.constructScreenplayResultDir(this.rootDir);
        const screenplayRunResultsPath = utils_1.constructTestInvocationResultDir(this.rootDir, this.runId);
        if (!await fs_extra_1.default.pathExists(screenplayRunResultsPath)) {
            return;
        }
        const screenplayResults = await runConclusionUtils_1.readRunResultsDirToMap(screenplayRunResultsPath);
        const jestSide = runConclusionUtils_1.jestResultsToIdMap(results.testResults, this.rootDir);
        const finalResults = runConclusionUtils_1.intersectJestAndScreenplay(screenplayResults, jestSide);
        const conclusion = runConclusionUtils_1.prepareRunConclusion(this.runId, results.startTime, finalResults);
        runConclusionUtils_1.writeRunConclusion(screenplayPath, this.runId, conclusion);
        const history = await runConclusionUtils_1.readRunsHistory(screenplayPath);
        runConclusionUtils_1.updateRunsHistory(history, conclusion, consts_1.HISTORY_RUNS_TO_RETAIN);
        runConclusionUtils_1.writeRunsHistory(screenplayPath, history);
    }
    getLastError() {
        const errorFromDefaultReporter = this.defaultReporter.getLastError();
        if (errorFromDefaultReporter) {
            return errorFromDefaultReporter;
        }
        return this.summaryReporter.getLastError();
    }
}
module.exports = EnhancedDefault;
