"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "setupServer", {
    enumerable: true,
    get: function() {
        return setupServer;
    }
});
const _express = /*#__PURE__*/ _interop_require_wildcard(require("express"));
const _cookieparser = /*#__PURE__*/ _interop_require_default(require("cookie-parser"));
const _passport = /*#__PURE__*/ _interop_require_default(require("passport"));
const _cors = /*#__PURE__*/ _interop_require_default(require("cors"));
const _helmet = /*#__PURE__*/ _interop_require_default(require("helmet"));
const _awilixexpress = require("awilix-express");
const _container = require("./container");
const _database = require("./middleware/database");
const _globalmiddleware = require("./middleware/global.middleware");
const _passport1 = require("./middleware/passport");
const _serverconstants = require("./server.constants");
const _path = require("path");
const _fsutils = require("./utils/fs.utils");
const _envutils = require("./utils/env.utils");
function _interop_require_default(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}
function _getRequireWildcardCache(nodeInterop) {
    if (typeof WeakMap !== "function") return null;
    var cacheBabelInterop = new WeakMap();
    var cacheNodeInterop = new WeakMap();
    return (_getRequireWildcardCache = function(nodeInterop) {
        return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
    })(nodeInterop);
}
function _interop_require_wildcard(obj, nodeInterop) {
    if (!nodeInterop && obj && obj.__esModule) {
        return obj;
    }
    if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
        return {
            default: obj
        };
    }
    var cache = _getRequireWildcardCache(nodeInterop);
    if (cache && cache.has(obj)) {
        return cache.get(obj);
    }
    var newObj = {
        __proto__: null
    };
    var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
    for(var key in obj){
        if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
            var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
            if (desc && (desc.get || desc.set)) {
                Object.defineProperty(newObj, key, desc);
            } else {
                newObj[key] = obj[key];
            }
        }
    }
    newObj.default = obj;
    if (cache) {
        cache.set(obj, newObj);
    }
    return newObj;
}
async function setupServer() {
    const httpServer = (0, _express.default)();
    const experimentalTypeormEnabled = (0, _envutils.getEnvOrDefault)(_serverconstants.AppConstants.ENABLE_EXPERIMENTAL_TYPEORM, _serverconstants.AppConstants.enableExperimentalTypeormDefault) === "true";
    if (experimentalTypeormEnabled) {
        const dbFolder = process.env[_serverconstants.AppConstants.DATABASE_PATH] || "./database";
        (0, _fsutils.ensureDirExists)((0, _path.join)((0, _fsutils.superRootPath)(), dbFolder));
    }
    const container = (0, _container.configureContainer)(experimentalTypeormEnabled);
    (0, _passport1.initializePassportStrategies)(_passport.default, container);
    httpServer.use((0, _cors.default)({
        origin: "*",
        methods: "GET,HEAD,PUT,PATCH,POST,DELETE"
    })).use((0, _helmet.default)({
        contentSecurityPolicy: false
    })).use((0, _express.json)({
        limit: "10mb"
    })).use((0, _cookieparser.default)()).use((0, _express.urlencoded)({
        extended: false
    })).use(_passport.default.initialize()).use(_passport.default.authenticate([
        "jwt",
        "anonymous"
    ], {
        session: false
    })).use((0, _awilixexpress.scopePerRequest)(container)).use(_database.interceptDatabaseError).use(_globalmiddleware.validateWizardCompleted).use(_globalmiddleware.interceptRoles);
    return {
        httpServer,
        container
    };
}

//# sourceMappingURL=server.core.js.map