"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.SuiteFactory = void 0;
const CommonFunctions = __importStar(require("../../common-functions/common-functions"));
const Models = __importStar(require("../../models/models"));
const FeatureFormatter_1 = require("./FeatureFormatter");
const uuid_1 = require("uuid");
class SuiteFactory {
    constructor(userProperies) {
        this.userProperties = userProperies;
    }
    /**
     * Parse the json report features
     */
    async parseJsons(parsedJsons, providedFeatures) {
        const suite = this.initializeSuite();
        suite.features = await Promise.all(parsedJsons.map(async (jsonFeature) => {
            const providedFeature = this.getProvidedFeature(providedFeatures, jsonFeature.name)?.feature;
            jsonFeature = await new FeatureFormatter_1.FeatureFormatter(providedFeature, jsonFeature).parseFeature();
            jsonFeature.metadata = this.updateMetadata(jsonFeature.metadata, this.userProperties.metadata);
            suite.results = this.updateSuiteStadistics(suite.results, jsonFeature);
            suite.metadata = this.updateMetadata(suite.metadata, jsonFeature.metadata);
            return jsonFeature;
        }));
        suite.features = suite.features.sort((a, b) => (a.id > b.id ? 1 : -1));
        suite.results.overview.durationHHMMSS = CommonFunctions.dateTimeFunctions.convertTimeFromNanoSecondsToHHMMSS(suite.results.overview.duration);
        suite.results.overview.date = this.userProperties.date;
        suite.results.overview.executionDate = CommonFunctions.dateTimeFunctions.getDateInDayMonthYear(this.userProperties.date);
        suite.results.overview.executionTime = CommonFunctions.dateTimeFunctions.getTimeInHHMMSS(this.userProperties.date, ':');
        suite.metadata = this.updateMetadata(suite.metadata, this.userProperties.metadata);
        suite.features = this.setUniqueIdForEachFeature(suite.features);
        if (this.userProperties.saveEnrichedJSON) {
            await CommonFunctions.fileSystemFunctions.saveJsonFile(this.userProperties.reportPath, 'enriched-output.json', suite);
        }
        return suite;
    }
    getProvidedFeature(providedFeatures, jsonFeatureName) {
        return providedFeatures !== null && providedFeatures?.length > 0
            ? providedFeatures?.filter(document => document.feature?.name === jsonFeatureName)[0]
            : null;
    }
    updateSuiteStadistics(results, feature) {
        results.overview.duration += feature.results.overview.duration;
        results.features.passed += feature.results.overview.status.includes(Models.Status.passed) ? 1 : 0;
        results.features.failed += feature.results.overview.status.includes(Models.Status.failed) ? 1 : 0;
        results.features.ambiguous += feature.results.overview.status.includes(Models.Status.ambiguous) ? 1 : 0;
        results.features.total += +1;
        results.scenarios.passed += feature.results.scenarios.passed;
        results.scenarios.failed += feature.results.scenarios.failed;
        results.scenarios.skipped += feature.results.scenarios.skipped;
        results.scenarios.pending += feature.results.scenarios.pending;
        results.scenarios.undefined += feature.results.scenarios.undefined;
        results.scenarios.ambiguous += feature.results.scenarios.ambiguous;
        results.scenarios.total += feature.results.scenarios.total;
        results.steps.passed += feature.results.steps.passed;
        results.steps.failed += feature.results.steps.failed;
        results.steps.skipped += feature.results.steps.skipped;
        results.steps.pending += feature.results.steps.pending;
        results.steps.undefined += feature.results.steps.undefined;
        results.steps.ambiguous += feature.results.steps.ambiguous;
        results.steps.total += feature.results.steps.total;
        return results;
    }
    updateMetadata(metadataToBeUpdated, metadataToBeAdded) {
        if (typeof metadataToBeUpdated !== 'undefined' && typeof metadataToBeAdded !== 'undefined' && metadataToBeAdded?.length > 0) {
            metadataToBeAdded.forEach(elementToBeAdded => {
                if (metadataToBeUpdated.filter(elementToBeUpdated => elementToBeUpdated.name === elementToBeAdded.name).length === 0) {
                    metadataToBeUpdated.push(elementToBeAdded);
                }
            });
            return metadataToBeUpdated;
        }
        else if (typeof metadataToBeUpdated !== 'undefined' && typeof metadataToBeAdded === 'undefined' || metadataToBeAdded?.length === 0) {
            return metadataToBeUpdated;
        }
        else {
            return [];
        }
    }
    initializeSuite() {
        const suite = {};
        suite.results = Models.reportResultsInitializer();
        suite.metadata = [];
        return suite;
    }
    setUniqueIdForEachFeature(features) {
        const seen = new Set();
        features.some(currentObject => {
            if (seen.size === seen.add(currentObject.id).size) {
                currentObject.id = currentObject.id + uuid_1.v4();
            }
        });
        return features;
    }
}
exports.SuiteFactory = SuiteFactory;
//# sourceMappingURL=SuiteFormatter.js.map