"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TestContext = void 0;
const StepResultWithName_1 = require("./StepResultWithName");
const fs_extra_1 = __importDefault(require("fs-extra"));
const consts_1 = require("./consts");
const path_1 = __importDefault(require("path"));
class TestContext {
    constructor(testArtifactsFolder, testName, testFullName, dateConstructor = Date) {
        this.testArtifactsFolder = testArtifactsFolder;
        this.testName = testName;
        this.testFullName = testFullName;
        this.dateConstructor = dateConstructor;
        this.stepIndex = 0;
        this.stepResults = [];
        this.testMetadata = {
            testName: this.testName,
            testFullName: this.testFullName,
        };
        this._currentStep = undefined;
        this.consoleEntries = [];
        this.unhandledExceptions = [];
    }
    get currentStep() {
        return this._currentStep;
    }
    stepStarted() {
        this.stepIndex++;
        this._currentStep = new StepResultWithName_1.StepResultWithName(this.stepIndex, this.dateConstructor);
    }
    getStepIndex() {
        return this.stepIndex;
    }
    async stepEnded() {
        if (this._currentStep) {
            this._currentStep.endTimestamp = this.dateConstructor.now();
            this.stepResults.push(this._currentStep);
        }
        await this.persistResults();
    }
    async testEnded() {
        await this.persistResults();
    }
    addTestMetadata(metadata) {
        Object.assign(this.testMetadata, metadata);
    }
    addStepMetadata(metadata) {
        Object.assign(this._currentStep, metadata);
    }
    async persistResults() {
        await fs_extra_1.default.writeFile(path_1.default.resolve(this.testArtifactsFolder, consts_1.SCREENPLAY_RESULTS_FILE_NAME), JSON.stringify(this.getResultsForPersistency(), null, 2));
    }
    getResultsForPersistency() {
        return {
            metadata: this.testMetadata,
            steps: this.stepResults,
        };
    }
}
exports.TestContext = TestContext;
