"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.start = void 0;
const express = require("express");
const events_1 = require("./events");
async function start(config) {
    Promise.all(config.plugins.map(async (plugin, idx) => {
        var _a;
        if (typeof plugin === 'string') {
            console.info(`Init plugin ${plugin}`);
            const construct = await (_a = plugin, Promise.resolve().then(() => require(_a)));
            await construct(config, config.pluginsOpts[plugin]);
        }
        else {
            console.info(`Init plugin #${idx}`);
            const construct = plugin;
            await construct(config, config.pluginsOpts[plugin]);
        }
    }));
    const app = express();
    await config.emit(events_1.EVENT_STARTUP_START, { app });
    return new Promise((resolve, reject) => {
        app.listen(config.port, () => {
            config.emit(events_1.EVENT_STARTUP_END, { app });
            resolve(app);
        });
    });
}
exports.start = start;
