"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.FeatureFormatter = void 0;
const CommonFunctions = __importStar(require("../../common-functions/common-functions"));
const Models = __importStar(require("../../models/models"));
const lodash = __importStar(require("lodash"));
const ScenarioFormatter_1 = require("./ScenarioFormatter");
const FeatureIcons_1 = require("../helpers/FeatureIcons");
class FeatureFormatter {
    constructor(gherkinFeature, jsonFeature) {
        this.gherkinFeature = gherkinFeature;
        this.jsonFeature = jsonFeature;
        this.scenarioFormater = new ScenarioFormatter_1.ScenarioFormatter(this.gherkinFeature);
    }
    /**
     * Parse the scenarios of the feature
     */
    async parseFeature() {
        this.jsonFeature = this.initalizeFeature(this.jsonFeature);
        // parse features
        if (typeof this.jsonFeature?.elements !== 'undefined' && this.jsonFeature?.elements?.length > 0) {
            {
                this.jsonFeature.elements = await Promise.all(this.jsonFeature.elements.map(jsonScenario => {
                    jsonScenario = this.scenarioFormater.parseScenario(jsonScenario, this.jsonFeature.name);
                    this.jsonFeature.results = this.updateFeatureStadistics(this.jsonFeature?.results, jsonScenario?.results);
                    return jsonScenario;
                }));
            }
            this.jsonFeature.results.overview.durationHHMMSS =
                CommonFunctions.dateTimeFunctions.convertTimeFromNanoSecondsToHHMMSS(this.jsonFeature.results.overview.duration);
            this.joinOutlineScenarios();
            this.jsonFeature.elements = this.jsonFeature.elements.filter(scenario => scenario != null && Object.keys(scenario).length !== 0);
            // add first scenario outline
            this.jsonFeature.elements = this.createFirstScenariosOutline();
        }
        if ((this.jsonFeature?.metadata)?.length > 0) {
            (this.jsonFeature.metadata).map(metadataElement => metadataElement.icon = FeatureIcons_1.getIcon(metadataElement.value));
        }
        return this.jsonFeature;
    }
    createFirstScenariosOutline() {
        const featureScenarios = lodash.cloneDeep(this.jsonFeature.elements);
        let insertedElements = -1;
        featureScenarios.forEach((jsonScenario, index) => {
            if (this.isFirstOutlineScenario(jsonScenario, index)) {
                // if( jsonScenario?.id?.substring( jsonScenario.id.length - 3, jsonScenario.id.length ) === ';;2' && typeof this.gherkinFeature !== 'undefined' ){
                insertedElements += 1;
                const scenarioIndex = featureScenarios.indexOf(jsonScenario);
                const providedScenario = this.gherkinFeature?.children.filter(scenario => scenario?.scenario?.name === jsonScenario.name)[0]?.scenario;
                const firstScenarioOutline = this.scenarioFormater.createNewFirstScenarioOutline(jsonScenario, providedScenario, this.jsonFeature, scenarioIndex);
                // if( firstScenarioOutline !== null ){
                this.jsonFeature.elements.splice(scenarioIndex + insertedElements, 0, firstScenarioOutline);
                // }
            }
        });
        return this.jsonFeature.elements;
    }
    getFeatureStatus(featureResults) {
        const result = [];
        if (featureResults.scenarios.ambiguous > 0) {
            result.push(Models.Status.ambiguous);
        }
        if (featureResults.scenarios.pending > 0) {
            result.push(Models.Status.pending);
        }
        if (featureResults.scenarios.undefined > 0) {
            result.push(Models.Status.undefined);
        }
        if (featureResults.scenarios.skipped > 0) {
            result.push(Models.Status.skipped);
        }
        if (featureResults.scenarios.failed > 0) {
            result.push(Models.Status.failed);
        }
        if (featureResults.scenarios.passed > 0) {
            result.push(Models.Status.passed);
        }
        return result;
    }
    getFeatureIcon(featureResults) {
        const result = [];
        if (featureResults.scenarios.ambiguous > 0) {
            result.push(FeatureIcons_1.getIcon(Models.Status.ambiguous.toString()));
        }
        if (featureResults.scenarios.pending > 0) {
            result.push(FeatureIcons_1.getIcon(Models.Status.pending.toString()));
        }
        if (featureResults.scenarios.undefined > 0) {
            result.push(FeatureIcons_1.getIcon(Models.Status.undefined.toString()));
        }
        if (featureResults.scenarios.skipped > 0) {
            result.push(FeatureIcons_1.getIcon(Models.Status.skipped.toString()));
        }
        if (featureResults.scenarios.failed > 0) {
            result.push(FeatureIcons_1.getIcon(Models.Status.failed.toString()));
        }
        if (featureResults.scenarios.passed > 0) {
            result.push(FeatureIcons_1.getIcon(Models.Status.passed.toString()));
        }
        return result;
    }
    updateFeatureStadistics(featureResults, scenarioResults) {
        if (typeof scenarioResults === 'undefined') {
            return featureResults;
        }
        // update feature scenarios stadistics
        featureResults.scenarios.failed += scenarioResults.steps.failed > 0 ? 1 : 0;
        featureResults.scenarios.ambiguous += scenarioResults.steps.ambiguous > 0 ? 1 : 0;
        featureResults.scenarios.undefined += scenarioResults.steps.undefined > 0 ? 1 : 0;
        featureResults.scenarios.pending += scenarioResults.steps.pending > 0 ? 1 : 0;
        featureResults.scenarios.skipped += scenarioResults.steps.skipped > 0 ? 1 : 0;
        featureResults.scenarios.passed +=
            scenarioResults.steps.passed > 0 &&
                scenarioResults.steps.failed === 0 &&
                scenarioResults.steps.skipped === 0 &&
                scenarioResults.steps.pending === 0 &&
                scenarioResults.steps.undefined === 0 ? 1 : 0;
        featureResults.scenarios.total =
            featureResults.scenarios.passed +
                featureResults.scenarios.skipped +
                featureResults.scenarios.pending +
                featureResults.scenarios.undefined +
                featureResults.scenarios.ambiguous +
                featureResults.scenarios.failed;
        // update feature steps stadistics
        featureResults.steps.passed += scenarioResults.steps.passed;
        featureResults.steps.failed += scenarioResults.steps.failed;
        featureResults.steps.skipped += scenarioResults.steps.skipped;
        featureResults.steps.pending += scenarioResults.steps.pending;
        featureResults.steps.undefined += scenarioResults.steps.undefined;
        featureResults.steps.ambiguous += scenarioResults.steps.ambiguous;
        featureResults.steps.total += scenarioResults.steps.total;
        // update feature result
        featureResults.overview.status = this.getFeatureStatus(featureResults);
        featureResults.overview.duration += scenarioResults.overview.duration;
        featureResults.overview.statusIcons = this.getFeatureIcon(featureResults);
        return featureResults;
    }
    initalizeFeature(jsonFeature) {
        jsonFeature.description = jsonFeature.description === null || typeof jsonFeature.description === 'undefined' ? '' : jsonFeature.description;
        jsonFeature.elements = jsonFeature.elements === null || typeof jsonFeature.elements === 'undefined' ? [] : jsonFeature.elements;
        jsonFeature.results = Models.featureResultsInitializer();
        jsonFeature.tags = jsonFeature.tags == null || typeof jsonFeature.tags == 'undefined' ? [] : jsonFeature.tags;
        jsonFeature.metadata = (jsonFeature?.metadata)?.length > 0 ? jsonFeature.metadata : [];
        return jsonFeature;
    }
    joinOutlineScenarios() {
        const scenarioOutlines = [];
        this.jsonFeature?.elements?.forEach(scenario => {
            if (this.scenarioFormater.isOutlineScenario(scenario)) {
                scenarioOutlines.push({ index: parseInt(`${this.jsonFeature?.elements.indexOf(scenario)}`, 10), name: `${scenario.name}` });
            }
        });
        const scenarioOutlineNames = [...new Set(scenarioOutlines.map(item => item.name))];
        scenarioOutlineNames.forEach(name => {
            const scenarioOutlineElements = scenarioOutlines.filter(scenario => scenario.name === name);
            const firstScenarioIndex = scenarioOutlineElements[0].index;
            scenarioOutlineElements.forEach((scenarioOutlineElement, index) => {
                CommonFunctions.arrayFunctions.arraymove(this.jsonFeature?.elements, scenarioOutlineElement.index, firstScenarioIndex + index);
                // ( jsonFeature.elements as Models.Scenario[] ).splice( scenarioIndex + insertedELements, 0, firstScenarioOutline );
            });
        });
    }
    isFirstOutlineScenario(scenario, index) {
        if (this.getOutlineScenarios().filter(scenarioElement => scenarioElement.name === scenario.name)[0]?.index === index) {
            return true;
        }
        return false;
    }
    getOutlineScenarios() {
        const scenarioOutlines = [];
        this.jsonFeature?.elements?.forEach(scenario => {
            if (this.scenarioFormater.isOutlineScenario(scenario)) {
                scenarioOutlines.push({ index: parseInt(`${this.jsonFeature?.elements.indexOf(scenario)}`, 10), name: `${scenario.name}` });
            }
        });
        return scenarioOutlines;
    }
}
exports.FeatureFormatter = FeatureFormatter;
//# sourceMappingURL=FeatureFormatter.js.map