"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ScenarioFormatter = void 0;
const CommonFunctions = __importStar(require("../../common-functions/common-functions"));
const Models = __importStar(require("../../models/models"));
const lodash = __importStar(require("lodash"));
const StepFormatter_1 = require("./StepFormatter");
class ScenarioFormatter {
    constructor(gherkinFeature) {
        this.gherkinFeature = gherkinFeature;
    }
    /**
     * Parse steps of scenario
     *
     * @param {object} scenario: scenario object
     * @private
     */
    parseScenario(scenario, featureName) {
        scenario = this.initializeScenario(scenario);
        if (typeof scenario.id === 'undefined') {
            console.log(`The scenario ${scenario.name} of the feature: ${featureName} doesn't have id.`);
            return {};
        }
        if (this.isOutlineScenario(scenario)) {
            scenario.keyword = 'Scenario Outline';
        }
        scenario.results = Models.scenarioResultsInitializer();
        scenario.before = Models.beforeOrAfterInitializer();
        scenario.after = Models.beforeOrAfterInitializer();
        scenario?.steps?.map((step) => {
            const parsedStep = new StepFormatter_1.StepFormatter().parseStep(step);
            switch (parsedStep.keyword.trim()) {
                case 'Before':
                    scenario.before.steps.push(parsedStep);
                    scenario.before.results = this.updateBeforeOrAfterStadistics(scenario.before.results, parsedStep.result);
                    break;
                case 'After':
                    scenario.after.steps.push(parsedStep);
                    scenario.after.results = this.updateBeforeOrAfterStadistics(scenario.after.results, parsedStep.result);
                    break;
            }
            scenario.results = this.updateScenarioStadistics(scenario.results, parsedStep.result);
            return parsedStep;
        });
        scenario.steps = scenario?.steps == null ? null : scenario.steps.filter(step => step.keyword.trim() !== 'Before' && step.keyword.trim() !== 'After');
        // scenario.before = before;
        // scenario.after = after;
        scenario.results.overview.durationHHMMSS = CommonFunctions.dateTimeFunctions.convertTimeFromNanoSecondsToHHMMSS(scenario.results.overview.duration);
        if (scenario.before.steps.length === 0) {
            // delete scenario.before;
        }
        else {
            scenario.results.before.durationHHMMSS = CommonFunctions.dateTimeFunctions.convertTimeFromNanoSecondsToHHMMSS(scenario.results.before.duration);
        }
        if (scenario.after.steps.length === 0) {
            // delete scenario.after;
        }
        else {
            scenario.results.after.durationHHMMSS = CommonFunctions.dateTimeFunctions.convertTimeFromNanoSecondsToHHMMSS(scenario.results.after.duration);
        }
        return scenario;
    }
    /**
     * Create first scenario for scenarios outline
     */
    createNewFirstScenarioOutline(jsonScenario, gherkinScenario, jsonFeature, scenarioIndex) {
        // if ( typeof providedScenario === 'undefined' || typeof providedScenario?.examples?.[0].tableHeader?.cells === 'undefined' ){
        //   return null;
        // }
        const firstScenarioOutline = lodash.cloneDeep(jsonScenario);
        firstScenarioOutline.id = firstScenarioOutline.id.substring(0, firstScenarioOutline.id.length - 3) + ';;1';
        firstScenarioOutline.name = CommonFunctions.parsingFunctions.escapeHTML(firstScenarioOutline.name);
        firstScenarioOutline.before = {};
        firstScenarioOutline.after = {};
        firstScenarioOutline.steps.forEach((stepJs) => {
            delete stepJs.text;
            delete stepJs.rows;
            delete stepJs.result.error_message;
            stepJs.embeddings = [];
            stepJs.attachments = [];
            stepJs.result.status = Models.Status.passed;
            gherkinScenario?.steps.map(stepPd => {
                if (stepPd.location.line === stepJs.line) {
                    stepJs.name = CommonFunctions.parsingFunctions.escapeHTML(stepPd.text);
                }
                return stepPd;
            });
        });
        const examples = gherkinScenario?.examples?.[0];
        const examplesHeaderCells = examples?.tableHeader.cells.map(cell => cell.value);
        const examplesRows = examples?.tableBody.map(row => {
            const rowContent = [];
            row.cells.forEach(cell => {
                rowContent.push(cell.value);
            });
            return rowContent;
        });
        let rows = [];
        rows.push(examplesHeaderCells);
        rows = rows.concat(examplesRows);
        firstScenarioOutline.examples = rows;
        firstScenarioOutline.examples.forEach((example, index, arr) => {
            if (index < arr.length - 1) {
                firstScenarioOutline.results.overview.duration += jsonFeature.elements[scenarioIndex + index].results.overview.duration;
                firstScenarioOutline.results.steps.passed += jsonFeature.elements[scenarioIndex + index].results.steps.passed;
                firstScenarioOutline.results.steps.failed += jsonFeature.elements[scenarioIndex + index].results.steps.failed;
                firstScenarioOutline.results.steps.undefined += jsonFeature.elements[scenarioIndex + index].results.steps.undefined;
                firstScenarioOutline.results.steps.pending += jsonFeature.elements[scenarioIndex + index].results.steps.pending;
                firstScenarioOutline.results.steps.ambiguous += jsonFeature.elements[scenarioIndex + index].results.steps.ambiguous;
                firstScenarioOutline.results.steps.skipped += jsonFeature.elements[scenarioIndex + index].results.steps.skipped;
            }
        });
        firstScenarioOutline.results.overview.durationHHMMSS = CommonFunctions.dateTimeFunctions.convertTimeFromNanoSecondsToHHMMSS(firstScenarioOutline.results.overview.duration);
        firstScenarioOutline.isFirstScenarioOutline = true;
        return firstScenarioOutline;
    }
    isOutlineScenario(scenario) {
        if (this.gherkinFeature?.children?.some(scenarioFromFeature => scenarioFromFeature.scenario?.name === scenario.name && scenarioFromFeature?.scenario?.examples && scenarioFromFeature?.scenario?.examples.length > 0)) {
            return true;
        }
        return false;
    }
    initializeScenario(scenario) {
        scenario.isFirstScenarioOutline = false;
        scenario.examples = [];
        scenario.keyword = scenario.keyword == null || typeof scenario.keyword === 'undefined' ? '' : scenario.keyword;
        scenario.name = scenario.name == null || typeof scenario.name === 'undefined' ? '' : scenario.name;
        scenario.type = scenario.type == null || typeof scenario.type === 'undefined' ? '' : scenario.type;
        scenario.results = Models.scenarioResultsInitializer();
        scenario.before = Models.beforeOrAfterInitializer();
        scenario.after = Models.beforeOrAfterInitializer();
        scenario.tags = scenario.tags == null || typeof scenario.tags === 'undefined' ? [] : scenario.tags;
        return scenario;
    }
    updateBeforeOrAfterStadistics(results, stepResults) {
        results.overview.duration += stepResults.duration;
        results.overview.status = stepResults.status !== Models.Status.passed
            ? stepResults.status
            : results.overview.status;
        return results;
    }
    updateScenarioStadistics(results, stepResults) {
        results.overview.duration += stepResults?.duration > 0 ? stepResults.duration : 0;
        results.overview.status = stepResults.status !== Models.Status.failed
            ? Models.Status.failed
            : results.overview.status;
        switch (stepResults.status) {
            case Models.Status.passed:
                results.steps.passed += 1;
                break;
            case Models.Status.failed:
                results.steps.failed += 1;
                break;
            case Models.Status.ambiguous:
                results.steps.ambiguous += 1;
                break;
            case Models.Status.pending:
                results.steps.pending += 1;
                break;
            case Models.Status.skipped:
                results.steps.skipped += 1;
                break;
            case Models.Status.undefined:
                results.steps.undefined += 1;
                break;
        }
        results.steps.total += 1;
        return results;
    }
}
exports.ScenarioFormatter = ScenarioFormatter;
//# sourceMappingURL=ScenarioFormatter.js.map