"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const express = require("express");
const nodeModules = require("node_modules-path");
const serveStatic = require("serve-static");
const Path = require("path");
const events_1 = require("../events");
const express_1 = require("../express");
const rootPath = Path.join(__dirname, '../../..');
async function default_1(config, opts = {}) {
    const options = {
        routes: [
            {
                route: '/',
                path: 'public',
            }, {
                route: '/',
                path: 'dist/client',
            },
        ]
            .concat(config.debug ? [{
                route: '/',
                path: './',
            }] : []),
        ...opts,
    };
    config.on(events_1.EVENT_STARTUP_START, ({ app }) => {
        const router = express.Router();
        options.routes
            .forEach(folder => {
            const rootFolder = folder.module ? `${nodeModules(folder.module)}/${folder.module}` : rootPath;
            const path = Path.join(rootFolder, folder.path || '');
            const route = folder.module && !folder.route ? `/libs/${folder.module}` : folder.route;
            if (!route)
                throw new Error('The config for static module requires either `route` or `module`');
            router.use(route, serveStatic(path));
        });
        app.use(express_1.withCache, router);
    });
}
exports.default = default_1;
