"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@nestjs/core");
const common_1 = require("@nestjs/common");
const config_1 = require("@nestjs/config");
const swagger_1 = require("@nestjs/swagger");
const helmet_1 = require("helmet");
const compression = require("compression");
const cookieParser = require("cookie-parser");
const path_1 = require("path");
const fs_1 = require("fs");
const app_module_1 = require("./app.module");
const http_exception_filter_1 = require("./common/filters/http-exception.filter");
const response_interceptor_1 = require("./common/interceptors/response.interceptor");
const logging_interceptor_1 = require("./common/interceptors/logging.interceptor");
async function bootstrap() {
    const app = await core_1.NestFactory.create(app_module_1.AppModule);
    const config = app.get(config_1.ConfigService);
    app.use((0, helmet_1.default)({
        crossOriginResourcePolicy: { policy: 'cross-origin' },
    }));
    app.use(compression());
    app.use(cookieParser(config.get('app.cookieSecret')));
    app.useStaticAssets((0, path_1.join)(process.cwd(), 'uploads'), {
        prefix: '/uploads/',
    });
    app.enableCors({
        origin: config.get('app.corsOrigins'),
        credentials: true,
        methods: ['GET', 'POST', 'PATCH', 'DELETE', 'OPTIONS'],
        allowedHeaders: ['Content-Type', 'Authorization'],
    });
    app.setGlobalPrefix(config.get('app.apiPrefix', 'api'));
    app.enableVersioning({ type: common_1.VersioningType.URI, defaultVersion: '1' });
    app.useGlobalPipes(new common_1.ValidationPipe({
        whitelist: true,
        forbidNonWhitelisted: true,
        transform: true,
        transformOptions: { enableImplicitConversion: true },
    }));
    app.useGlobalFilters(new http_exception_filter_1.HttpExceptionFilter());
    app.useGlobalInterceptors(new logging_interceptor_1.LoggingInterceptor(), new response_interceptor_1.ResponseInterceptor());
    if (config.get('app.nodeEnv') !== 'production') {
        const swaggerConfig = new swagger_1.DocumentBuilder()
            .setTitle('Splitwise Clone API')
            .setDescription('Expense sharing application REST API')
            .setVersion('1.0')
            .addBearerAuth()
            .addCookieAuth('refreshToken')
            .build();
        const document = swagger_1.SwaggerModule.createDocument(app, swaggerConfig);
        swagger_1.SwaggerModule.setup('api/docs', app, document, {
            swaggerOptions: { persistAuthorization: true },
        });
    }
    const publicDir = (0, path_1.join)(process.cwd(), 'public');
    const spaIndex = (0, path_1.join)(publicDir, 'index.html');
    if ((0, fs_1.existsSync)(spaIndex)) {
        app.useStaticAssets(publicDir);
        const express = app.getHttpAdapter().getInstance();
        express.use((req, res, next) => {
            if (req.method !== 'GET' && req.method !== 'HEAD')
                return next();
            const path = req.path;
            if (path.startsWith('/api') || path.startsWith('/uploads'))
                return next();
            res.sendFile(spaIndex);
        });
        console.log(`Web app served from ${publicDir}`);
    }
    const port = Number(process.env.PORT) || config.get('app.port', 3001);
    await app.listen(port, '0.0.0.0');
    console.log(`API running on port ${port} (/api/v1)`);
}
bootstrap();
//# sourceMappingURL=main.js.map