"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const cors_1 = __importDefault(require("cors"));
const express_1 = __importDefault(require("express"));
const config_1 = __importDefault(require("../config"));
const config_2 = require("../services/config");
const apiRoutes = __importStar(require("./api"));
const middleware_1 = require("./middleware");
const status_1 = require("./status");
const web3_1 = require("./web3");
const app = (0, express_1.default)();
const bodyLimit = 100 * 1024 * 1024; // 100 MB
app.use(express_1.default.json({
    limit: bodyLimit
}));
app.use(express_1.default.raw({
    limit: bodyLimit
}));
const chains = config_2.Config.chains();
const networks = {};
for (const chain of chains) {
    for (const network of Object.keys(config_1.default.chains[chain])) {
        networks[chain] = networks[chain] || {};
        Object.assign(networks[chain], {
            [network]: true
        });
    }
}
function bootstrapApiRoutes() {
    const router = express_1.default.Router({
        mergeParams: true
    });
    for (const route of Object.values(apiRoutes)) {
        router.use(route.path, route.router);
    }
    return router;
}
app.use((0, cors_1.default)());
app.use((0, middleware_1.LogMiddleware)());
app.use((0, middleware_1.CacheMiddleware)(middleware_1.CacheTimes.Second, middleware_1.CacheTimes.Second));
app.use((0, middleware_1.RateLimiter)('GLOBAL', 10, 200, 4000));
app.use('/api' + status_1.statusRoute.path, status_1.statusRoute.router);
// Change aliased chain and network params
app.param(['chain', 'network'], (req, _, next) => {
    const { chain: beforeChain, network: beforeNetwork } = req.params;
    const { chain, network } = config_2.Config.aliasFor({ chain: beforeChain, network: beforeNetwork });
    req.params.chain = chain;
    req.params.network = network;
    next();
});
app.use('/api/:chain/:network', (req, resp, next) => {
    const { chain, network } = req.params;
    const hasChain = chains.includes(chain);
    const chainNetworks = networks[chain] || null;
    const hasChainNetworks = chainNetworks != null;
    const hasNetworkForChain = hasChainNetworks ? chainNetworks[network] : false;
    if (chain && !hasChain) {
        return resp.status(500).send(`This node is not configured for the chain ${chain}`);
    }
    if (network && (!hasChainNetworks || !hasNetworkForChain)) {
        return resp.status(500).send(`This node is not configured for the network ${network} on chain ${chain}`);
    }
    return next();
});
app.use('/api/:chain/:network', bootstrapApiRoutes());
app.use('/web3/:chain/:network', web3_1.Web3Proxy);
exports.default = app;
//# sourceMappingURL=index.js.map