"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.WebServer = void 0;
const tslib_1 = require("tslib");
const express_1 = (0, tslib_1.__importDefault)(require("express"));
const fs_extra_1 = require("fs-extra");
const open_1 = (0, tslib_1.__importDefault)(require("open"));
const node_path_1 = (0, tslib_1.__importDefault)(require("node:path"));
const shelljs_1 = (0, tslib_1.__importDefault)(require("shelljs"));
class WebServer {
    constructor(port, host = 'localhost', path = '/') {
        this.app = (0, express_1.default)();
        this.port = port;
        this.host = host;
        this.path = path;
    }
    async start() {
        this.app.listen(this.port, this.host, () => {
            const path = `http://${this.host}:${this.port}${this.path}`;
            console.log(`Web server listening on ${path}`);
            (0, open_1.default)(path);
        });
        const modulesLocation = await shelljs_1.default.exec('npm root -g');
        const interfacePath = process.env.LOCAL ? node_path_1.default.resolve('dist/interface') : modulesLocation.trim() + '/@steerprotocol/steer-toolset/dist/interface/';
        this.app.use(this.path, express_1.default.static(interfacePath));
    }
    setupSwapsRoute(data) {
        // define a route handler for the default home page
        this.addRoute(this.path + 'data', async (req, res) => {
            res.send(JSON.stringify(JSON.parse(await (0, fs_extra_1.readFile)(data, 'utf8')), null, 4));
        });
    }
    addRoute(route, handler) {
        this.app.get(route, handler);
    }
}
exports.WebServer = WebServer;
