"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"));
const cors = require('cors');
const bodyParser = require('body-parser');
const isModuleInstalled = async (moduleName, projectDir) => {
    try {
        if (!projectDir) {
            // Look up the current working directory if projectDir is not provided
            projectDir = process.cwd();
        }
        const packageJson = await (0, fs_extra_1.readFile)(projectDir + '/package.json', 'utf8');
        const dependencies = JSON.parse(packageJson).dependencies;
        const devDependencies = JSON.parse(packageJson).devDependencies;
        return Boolean(dependencies[moduleName] || devDependencies[moduleName]);
    }
    catch (error) {
        console.error(error);
        return false;
    }
};
class WebServer {
    constructor(port, host = 'localhost', path = '/') {
        this.app = (0, express_1.default)();
        this.port = port;
        this.host = host;
        this.path = path;
        process.env.NODE_END !== 'production' && this.app.use(cors());
        this.app.use(bodyParser.json());
    }
    async start() {
        this.app.listen(this.port, () => {
            const path = `http://${this.host}:${this.port}${this.path}`;
            console.log(`Web server listening on ${path}`);
            (0, open_1.default)(path);
        });
        let interfacePath = '';
        const localInstall = await isModuleInstalled('@steerprotocol/steer-toolset');
        if (localInstall) {
            interfacePath = node_path_1.default.join(process.cwd(), 'node_modules/@steerprotocol/steer-toolset/dist/interface/');
        }
        else {
            const modulesLocation = await shelljs_1.default.exec('npm root -g');
            interfacePath = process.env.LOCAL ?
                node_path_1.default.resolve('dist/interface') :
                modulesLocation.trim() +
                    '/@steerprotocol/steer-toolset/dist/interface/';
        }
        console.log('interfacePath', interfacePath);
        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, requestType = 'get') {
        switch (requestType) {
            case 'get':
                this.app.get(route, handler);
                break;
            case 'post':
                this.app.post(route, handler);
                break;
            default:
                break;
        }
    }
}
exports.WebServer = WebServer;
