"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 });
const lodash = __importStar(require("lodash"));
class MongoDbInserts {
    constructor(mongooseModels) {
        this.mongooseModels = mongooseModels;
    }
    async insertReport(report) {
        const mongoReport = lodash.cloneDeep(report);
        const features = lodash.cloneDeep(report?.features);
        mongoReport.features = [];
        // mongoReport._id = '';
        const element = await this.mongooseModels.reportModel.create(mongoReport);
        // element.features = element.id as mongoose.Types.ObjectId[];
        await element.save();
        // const reportId = mongoReport._id as mongoose.Types.ObjectId;
        // if ( features?.length > 0 ) {
        // mongoReport.featuresId = new ObjectId();
        // features = features.sort( ( a, b ) => ( a.id > b.id ) ? 1 : -1 );
        await Promise.all(features.map(async (feature) => {
            // feature.Index = index;
            const scenarios = lodash.cloneDeep(feature.elements);
            feature.reportId = element._id;
            delete feature.elements;
            const featureInDb = await this.mongooseModels.featureModel.create(feature);
            // if( scenarios != null ) {
            await this._insertScenarios(scenarios, featureInDb._id);
            // }
            return feature;
        }));
        // }
        return element._id;
    }
    /**
     * Insert the scenarios of a feature into the mongodb database
     *
     * @param {*} feature
     * @param {*} reportFeaturesId
     * @param {*} index
     */
    async _insertScenarios(scenarios, reportFeaturesId) {
        // feature.reportId = reportFeaturesId;
        // feature.scenariosId = new ObjectId();
        // feature.index = index;
        if (typeof scenarios !== 'undefined' && scenarios?.length > 0) {
            await Promise.all(scenarios.map(async (scenario) => {
                scenario.featureId = reportFeaturesId;
                // scenario.Index = featuresIndex;
                const scenarioSteps = lodash.cloneDeep(scenario.steps);
                delete scenario.steps;
                const scenarioDb = await this.mongooseModels.scenarioModel.create(scenario);
                await this._insertScenarioSteps(scenarioSteps, scenarioDb._id);
                return scenario;
            }));
        }
    }
    /**
     * Insert the steps of a scenario into a mongodb database
     *
     * @param {*} scenarioSteps
     * @param {*} scenarioStepsId
     * @param {*} reportFeaturesId
     */
    async _insertScenarioSteps(scenarioSteps, scenarioStepsId) {
        if (typeof scenarioSteps !== 'undefined' && scenarioSteps !== null && scenarioSteps.length > 0) {
            await Promise.all(scenarioSteps.map(async (step) => {
                step.scenarioId = scenarioStepsId;
                // step.Index = index;
                // delete step.attachments;
                await this.mongooseModels.stepModel.create(step);
                // delete step.scenarioId;
                // delete step.index;
                return step;
            }));
        }
    }
}
exports.default = MongoDbInserts;
//# sourceMappingURL=mongoose-inserts.js.map