"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.readHistoryFallback = exports.updateHistoryFromRootCauseResultsOnly = void 0;
const runConclusionUtils_1 = require("./runConclusion/runConclusionUtils");
const utils_1 = require("./utils");
const consts_1 = require("./consts");
const fs_extra_1 = __importDefault(require("fs-extra"));
async function updateHistoryFromRootCauseResultsOnly(runId = consts_1.FALLBACK_RUN_ID, projectRoot = process.cwd(), dateConstructor = Date) {
    const resultsDirPath = utils_1.constructResultDir(projectRoot);
    const runResultsPath = utils_1.constructTestInvocationResultDir(projectRoot, runId);
    if (!(await fs_extra_1.default.pathExists(resultsDirPath))) {
        throw new Error('None existing Root Cause path, make sure you use the same projectRoot as your attach call');
    }
    if (!(await fs_extra_1.default.pathExists(runResultsPath))) {
        throw new Error('None existing Root Cause runs path, make sure you use the same runId & projectRoot as your attach call');
    }
    const rootCauseResults = await runConclusionUtils_1.readRunResultsDirToMap(runResultsPath);
    const conclusion = prepareRunConclusionFromOnlyRootCauseResults(runId, dateConstructor.now(), rootCauseResults);
    await runConclusionUtils_1.writeRunConclusion(resultsDirPath, runId, conclusion);
    const history = await runConclusionUtils_1.readRunsHistory(resultsDirPath);
    runConclusionUtils_1.updateRunsHistory(history, conclusion, consts_1.HISTORY_RUNS_TO_RETAIN);
    await runConclusionUtils_1.writeRunsHistory(resultsDirPath, history);
}
exports.updateHistoryFromRootCauseResultsOnly = updateHistoryFromRootCauseResultsOnly;
async function readHistoryFallback(runId = consts_1.FALLBACK_RUN_ID, projectRoot = process.cwd(), dateConstructor = Date) {
    const rootCausePath = utils_1.constructResultDir(projectRoot);
    const rootCauseRunResultsPath = utils_1.constructTestInvocationResultDir(projectRoot, runId);
    if (!(await fs_extra_1.default.pathExists(rootCausePath))) {
        throw new Error('None existing Root Cause path, make sure you use the same projectRoot as your attach call');
    }
    if (!(await fs_extra_1.default.pathExists(rootCauseRunResultsPath))) {
        throw new Error('None existing Root Cause runs path, make sure you use the same runId & projectRoot as your attach call');
    }
    const rootCauseResults = await runConclusionUtils_1.readRunResultsDirToMap(rootCauseRunResultsPath);
    const conclusion = prepareRunConclusionFromOnlyRootCauseResults(runId, dateConstructor.now(), rootCauseResults);
    return conclusion;
}
exports.readHistoryFallback = readHistoryFallback;
function prepareRunConclusionFromOnlyRootCauseResults(runId, timestamp, data) {
    const conclusion = {
        runId,
        timestamp,
        tests: [...data]
            .map(([id, entry]) => {
            var _a, _b, _c, _d, _e;
            return ({
                id,
                name: entry.testData.metadata.testName,
                fullName: entry.testData.metadata.testFullName,
                suiteFilePath: entry.testData.metadata.fileName || __filename,
                reason: ((_a = entry.testData.metadata.testEndStatus) === null || _a === void 0 ? void 0 : _a.success) ? undefined
                    : (_c = (_b = entry.testData.metadata.testEndStatus) === null || _b === void 0 ? void 0 : _b.error) === null || _c === void 0 ? void 0 : _c.message,
                success: (_e = (_d = entry.testData.metadata.testEndStatus) === null || _d === void 0 ? void 0 : _d.success) !== null && _e !== void 0 ? _e : true,
                timestamp: entry.testData.metadata.timestamp,
                endedTimestamp: entry.testData.metadata.endedTimestamp,
            });
        })
            .sort((a, b) => b.timestamp - a.timestamp),
    };
    return conclusion;
}
