"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.updateHistoryFromScreenplayResultsOnly = void 0;
const runConclusionUtils_1 = require("./jest/reporter/runConclusionUtils");
const utils_1 = require("./utils");
const consts_1 = require("./consts");
const fs_extra_1 = __importDefault(require("fs-extra"));
async function updateHistoryFromScreenplayResultsOnly(runId = consts_1.FALLBACK_RUN_ID, projectRoot = process.cwd(), dateConstructor = Date) {
    const screenplayPath = utils_1.constructScreenplayResultDir(projectRoot);
    const screenplayRunResultsPath = utils_1.constructTestInvocationResultDir(projectRoot, runId);
    if (!await fs_extra_1.default.pathExists(screenplayPath)) {
        throw new Error('None existing screenplay path, make sure you use the same projectRoot as your attach call');
    }
    if (!await fs_extra_1.default.pathExists(screenplayRunResultsPath)) {
        throw new Error('None existing screenplay runs path, make sure you use the same runId & projectRoot as your attach call');
    }
    const screenplayResults = await runConclusionUtils_1.readRunResultsDirToMap(screenplayRunResultsPath);
    const conclusion = prepareRunConclusionFromOnlyScreenplayResults(runId, dateConstructor.now(), screenplayResults);
    runConclusionUtils_1.writeRunConclusion(screenplayPath, 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);
}
exports.updateHistoryFromScreenplayResultsOnly = updateHistoryFromScreenplayResultsOnly;
async function readHistoryFallback(runId = consts_1.FALLBACK_RUN_ID, projectRoot = process.cwd(), dateConstructor = Date) {
    const screenplayPath = utils_1.constructScreenplayResultDir(projectRoot);
    const screenplayRunResultsPath = utils_1.constructTestInvocationResultDir(projectRoot, runId);
    if (!await fs_extra_1.default.pathExists(screenplayPath)) {
        throw new Error('None existing screenplay path, make sure you use the same projectRoot as your attach call');
    }
    if (!await fs_extra_1.default.pathExists(screenplayRunResultsPath)) {
        throw new Error('None existing screenplay runs path, make sure you use the same runId & projectRoot as your attach call');
    }
    const screenplayResults = await runConclusionUtils_1.readRunResultsDirToMap(screenplayRunResultsPath);
    const conclusion = prepareRunConclusionFromOnlyScreenplayResults(runId, dateConstructor.now(), screenplayResults);
    return conclusion;
}
exports.readHistoryFallback = readHistoryFallback;
function prepareRunConclusionFromOnlyScreenplayResults(runId, timestamp, data) {
    const conclusion = {
        runId,
        timestamp,
        tests: [...data].map(([id, entry]) => {
            var _a, _b;
            return ({
                id,
                name: entry.testData.metadata.testName,
                fullName: entry.testData.metadata.testFullName,
                success: (_b = (_a = entry.testData.metadata.testEndStatus) === null || _a === void 0 ? void 0 : _a.success) !== null && _b !== void 0 ? _b : true,
            });
        }),
    };
    return conclusion;
}
