Object.defineProperty(exports, "__esModule", { value: true });
exports.WebServer = void 0;
var Logger_1 = require("./Logger");
var exphbs = require("express-handlebars");
var Express = require("express");
var path = require("path");
var fs_extra_1 = require("fs-extra");
var WebServer = /** @class */ (function () {
    function WebServer() {
        this.app = Express();
        var hbs = exphbs.create({ defaultLayout: 'main/index' });
        this.app.engine('handlebars', hbs.engine);
        this.app.set('view engine', 'handlebars');
        this.app.set('views', path.join(__filename, '..', '..', '..', 'Frontend', 'pages'));
        this.app.set('view options', { layout: 'main/index' });
        var staticDir = path.join(__filename, '..', '..', '..', 'Static');
        this.app.use('/stc', Express.static(staticDir));
        this.app.use('/favicon.ico', Express.static(path.join(staticDir, 'logo', '256.ico')));
        this.app.get([
            '/pd/:pagename/:type',
            '/ld/:pagename/:type'
        ], function (req, res, next) {
            var pagename = req.params.pagename;
            var type = req.params.type;
            var pagePath = req.url.startsWith('/pd')
                ? path.join(__filename, '..', '..', '..', 'Frontend', 'pages', pagename)
                : path.join(__filename, '..', '..', '..', 'Frontend', 'pages', 'layouts', pagename);
            switch (type) {
                case 'style':
                    var stylePath = path.join(pagePath, 'style.css');
                    (0, fs_extra_1.pathExists)(stylePath, function (err, exists) {
                        if (exists == true)
                            res.sendFile(stylePath);
                        else
                            next();
                    });
                    break;
                case 'script':
                    var stylePath = path.join(pagePath, 'script.js');
                    (0, fs_extra_1.pathExists)(stylePath, function (err, exists) {
                        if (exists == true)
                            res.sendFile(stylePath);
                        else
                            next();
                    });
                    break;
                default:
                    next();
                    break;
            }
        });
        this.app.get('/*', function (req, res) {
            res.render('home', {
                icons: Undecked.Icons.getList(),
                actions: Undecked.Integrations.getActions(),
                connections: Undecked.Integrations.getConnections(),
                connected: Undecked.Connections.getList()
            });
        });
    }
    WebServer.prototype.start = function (callback) {
        (0, Logger_1.Log)('info', 'Preparing WebServer');
        var server = require('http').Server(this.app);
        callback(server);
    };
    return WebServer;
}());
exports.WebServer = WebServer;
// export interface WebServer {
// 	start: (callback: (server: Server) => void) => void;
// }
//# sourceMappingURL=WebServer.js.map