"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.runConclusionToTestimExecution = exports.getRunConclusionFiles = void 0;
const util_1 = require("util");
const glob_1 = __importDefault(require("glob"));
const guid_1 = require("./guid");
const crypto_1 = require("crypto");
function convertExecution(conclusion, testimUserMetadata, gitMetadata, guidImpl = guid_1.guid) {
    const execution = {};
    const testIdToResultIds = new Map();
    for (const test of conclusion.tests) {
        const resultId = guidImpl();
        testIdToResultIds.set(test.id, resultId);
        execution[resultId] = {
            resultId,
            testId: test.id,
            status: 'FINISHED',
            testRunOverrideStatus: 'completed',
            success: test.success,
            reason: test.reason,
            startTime: test.timestamp || conclusion.timestamp,
            endTime: test.endedTimestamp || conclusion.timestamp,
            isTestsContainer: false,
            name: test.name,
            show: true,
            config: getDetaultTestConfig(conclusion, testimUserMetadata, gitMetadata),
        };
    }
    const containers = new Set(conclusion.tests.map((x) => x.suiteFilePath).filter(Boolean));
    for (const container of containers) {
        const resultId = guidImpl();
        const tests = conclusion.tests.filter((x) => x.suiteFilePath === container);
        tests.forEach((test) => {
            execution[testIdToResultIds.get(test.id)].parentResultId = resultId;
        });
        const lastTestEnd = Math.max(...conclusion.tests.map((x) => x.endedTimestamp), conclusion.timestamp);
        execution[resultId] = {
            resultId,
            name: container,
            testId: crypto_1.createHash('md5').update(container).digest('hex'),
            status: 'FINISHED',
            testRunOverrideStatus: 'completed',
            success: tests.every((x) => x.success),
            startTime: conclusion.timestamp,
            endTime: lastTestEnd,
            show: false,
            config: getDetaultTestConfig(conclusion, testimUserMetadata),
            childTestResultIds: tests.map((x) => testIdToResultIds.get(x.id)),
            isTestsContainer: true,
        };
    }
    return execution;
}
function getStatus(conclusion) {
    return 'FINISHED';
}
function getDetaultTestConfig(conclusion, testimUserMetadata, gitMetadata) {
    var _a, _b, _c;
    return {
        parallel: 1,
        browser: 'chrome',
        runnerVersion: 'none(rootCause)',
        testimBranch: 'master',
        canaryMode: false,
        gitBranch: (_a = gitMetadata === null || gitMetadata === void 0 ? void 0 : gitMetadata.gitBranch) !== null && _a !== void 0 ? _a : undefined,
        gitCommit: (_b = gitMetadata === null || gitMetadata === void 0 ? void 0 : gitMetadata.gitCommit) !== null && _b !== void 0 ? _b : undefined,
        gitRepoUrl: (_c = gitMetadata === null || gitMetadata === void 0 ? void 0 : gitMetadata.gitRepoUrl) !== null && _c !== void 0 ? _c : undefined,
        source: 'n/a',
        testPlans: [],
        testLabels: [],
        testNames: conclusion.tests.map((x) => x.name),
        testIds: [],
        testConfigs: [],
        testConfigIds: [],
        browserTimeout: 3e5,
        timeout: 3e5,
        newBrowserWaitTimeout: 3e5,
        tunnelPort: '80',
        runnerMode: 'n/a',
        sessionType: 'rootCause',
        companyId: testimUserMetadata.companyId,
        testConfig: {},
    };
}
async function getRunConclusionFiles(rootCauseRunDir) {
    const files = (await util_1.promisify((cb) => glob_1.default(`${rootCauseRunDir}/**/*`, { nodir: true }, cb))());
    return files;
}
exports.getRunConclusionFiles = getRunConclusionFiles;
function runConclusionToTestimExecution(conclusion, testimUserMetadata, gitMetadata, guidImpl = guid_1.guid) {
    return {
        runId: conclusion.runId + guidImpl(),
        projectId: testimUserMetadata.projectId,
        labels: `Testim Root Cause Run ${new Date().toLocaleString()}`,
        startTime: conclusion.timestamp,
        endTime: Date.now(),
        execution: convertExecution(conclusion, testimUserMetadata, gitMetadata, guidImpl),
        status: getStatus(conclusion),
        config: getDetaultTestConfig(conclusion, testimUserMetadata, gitMetadata),
        resultLabels: testimUserMetadata.resultLabels,
        show: true,
        remoteRunId: '',
        metadata: {},
    };
}
exports.runConclusionToTestimExecution = runConclusionToTestimExecution;
