"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;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const path = __importStar(require("path"));
const express_1 = __importDefault(require("express"));
const GenerateReport_1 = require("../generate-report/GenerateReport");
const mongoose_helper_1 = require("../../lib/mongoose-report-manager/mongoose-helper");
class Server {
    constructor() {
        this.userProperties = {
            disableLog: true,
            jsonDir: `${process.cwd()}/test/unit/data/jsons`,
            metadata: [
                { name: 'browser', value: 'chrome 81' }
            ],
            mongoDb: {
                collections: {
                    features: 'Features',
                    outputs: 'Outputs',
                    reports: 'Reports',
                    scenarios: 'Scenarios',
                    steps: 'Steps'
                },
                dbHost: 'localhost',
                dbName: 'multiple-cucumber-html-reports',
                dbPort: 27017,
            },
            navigateToFeatureIfThereIsOnlyOne: false,
            openReportInBrowser: true,
            server: {
                serverPort: 3000,
                serverView: 'index'
            }
        };
        this.app = express_1.default();
        this.mongooseHelper = new mongoose_helper_1.MongooseHelper(this.userProperties);
        this.app.set('port', this.userProperties.server?.serverPort);
        this.app.set('views', path.join(__dirname, 'views'));
        this.app.set('view engine', 'ejs');
        this.app.use(express_1.default.urlencoded({ extended: true }));
        this.app.use(express_1.default.json());
        this.app.get('/', async (req, res) => {
            const allCollectionData = await this.mongooseHelper.getAllTheElementsOrderedAndFiltered('', -1, '');
            res.render('index', { collectionData: allCollectionData });
        });
        this.app.get('/mongo/get/datatable', async (req, res) => {
            const orderColumnNumber = req.query.order;
            (orderColumnNumber[0]).column;
            const columnArray = req.query.columns;
            const orderColumnName = columnArray[(orderColumnNumber[0]).column].data;
            const orderColumnDirection = orderColumnNumber[0].dir === 'asc' ? 1 : -1;
            const searchValue = req.query.search.value;
            const allCollectionData = await this.mongooseHelper.getAllTheElementsOrderedAndFiltered(orderColumnName, orderColumnDirection, searchValue);
            const returnData = { data: allCollectionData, recordsFiltered: allCollectionData.length, recordsTotal: allCollectionData.length };
            res.send(returnData);
        });
        this.app.get('/generateReport', async (req, res) => {
            const reportId = req.query.id;
            const jsonReport = await this.mongooseHelper.getReportById(reportId);
            await new GenerateReport_1.GenerateReport().generateHtmlReport(this.userProperties, jsonReport);
            res.setHeader('Content-Type', 'application/json');
            res.json({ htmlreport: jsonReport });
        });
    }
    startServer() {
        this.server = this.app.listen(this.app.get('port'));
    }
    closeServer() {
        this.server.close();
    }
}
exports.default = Server;
//# sourceMappingURL=server.js.map