"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExpressServer = void 0;
const tslib_1 = require("tslib");
const express_1 = tslib_1.__importDefault(require("express"));
const p_event_1 = tslib_1.__importDefault(require("p-event"));
const path_1 = tslib_1.__importDefault(require("path"));
class ExpressServer {
    constructor(restApp, rootDirectory) {
        this.rootDirectory = rootDirectory;
        this.app = express_1.default();
        this.lbApp = restApp;
        this.lbApp.addStaticDir = this.static.bind(this);
        this.lbApp.rootDirectory = rootDirectory;
        this.app.use('/api', this.lbApp.requestHandler);
        this.initApplication(this.lbApp);
        /*
        this.app.use(
          '/admin',
          express.static(
            path.join(rootDirectory, 'node_modules/@jacquesparis/objects-angular'),
          ),
        );*/
        this.app.use('/objectsites', express_1.default.static(path_1.default.join(__dirname, 'www/objectsites')));
        this.app.use('/root', express_1.default.static(path_1.default.join(__dirname, 'www')));
        const staticDirs = this.lbApp.getStaticDirs();
        for (const staticPath in staticDirs) {
            this.static(staticPath, staticDirs[staticPath]);
        }
        this.app.use('/', this.lbApp.requestHandler);
    }
    static(basePath, dirName) {
        if (dirName.startsWith('node_modules/')) {
            dirName = path_1.default.join(this.rootDirectory, dirName);
        }
        this.app.use(basePath.startsWith('/') ? basePath : '/' + basePath, express_1.default.static(dirName));
    }
    async boot() {
        await this.lbApp.boot();
        await this.lbApp.bootObjectTrees();
        await this.bootApplication(this.lbApp);
    }
    async start() {
        await this.lbApp.start();
        const port = this.lbApp.restServer.config.port || 3000;
        const host = this.lbApp.restServer.config.host
            ? this.lbApp.restServer.config.host
            : '127.0.0.1';
        const mainHost = host.startsWith('192.168.') ? 'localhost' : host;
        const mainPort = host.startsWith('192.168.') ? 3000 : port;
        try {
            this.server = this.app.listen(mainPort, mainHost);
            console.log('listening', mainHost, mainPort, `http://${mainHost}:${mainPort}`);
        }
        catch (error) {
            console.trace('not possible to listen', mainHost, mainPort);
        }
        await p_event_1.default(this.server, 'listening');
        if (mainHost !== host || mainPort !== port) {
            try {
                this.app.listen(port, host);
                console.log('listening', host, port, `http://${host}:${port}`);
            }
            catch (error) {
                console.trace('not possible to listen', host, port);
            }
        }
    }
    // For testing purposes
    async stop() {
        if (!this.server)
            return;
        await this.lbApp.stop();
        this.server.close();
        await p_event_1.default(this.server, 'close');
        this.server = null;
    }
    initApplication(app) {
        return;
    }
    async bootApplication(app) {
        return;
    }
}
exports.ExpressServer = ExpressServer;
//# sourceMappingURL=server.js.map