"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createApp = createApp;
exports.startServer = startServer;
const express_1 = __importDefault(require("express"));
const cors_1 = __importDefault(require("cors"));
const integration_foundation_1 = require("@dbis/integration-foundation");
const settlement_1 = require("./api/routes/settlement");
const rate_limit_1 = require("./middleware/rate-limit");
const config_1 = require("./config");
function corsOptions() {
    const policy = (0, integration_foundation_1.loadCustomerSecurityPolicy)();
    const origins = policy.corsOrigins.filter(Boolean);
    if (!origins.length)
        return {};
    return {
        origin(origin, cb) {
            if (!origin || origins.includes(origin))
                cb(null, true);
            else
                cb(new Error('CORS not allowed'));
        },
    };
}
function createApp() {
    const app = (0, express_1.default)();
    app.use((0, cors_1.default)(corsOptions()));
    app.use(express_1.default.json({ limit: '2mb' }));
    app.use(express_1.default.text({ type: 'text/plain', limit: '2mb' }));
    app.use('/api/v1/settlement', rate_limit_1.settlementRateLimiter, (0, settlement_1.createSettlementRouter)());
    return app;
}
function startServer() {
    const app = createApp();
    const p = (0, config_1.port)();
    app.listen(p, () => {
        console.log(`OMNL settlement middleware listening on http://localhost:${p}/api/v1/settlement`);
    });
}
