import { Injectable, NestMiddleware } from '@nestjs/common';
import { Request, Response, NextFunction } from 'express';
import { i18nStore } from './i18n.store';
import { DEFAULT_LANGUAGE } from '../common/constants';

@Injectable()
export class I18nMiddleware implements NestMiddleware {
    use(req: Request, res: Response, next: NextFunction) {
        const language = req.headers['x-user-language'] || DEFAULT_LANGUAGE;
        i18nStore.run({ language: String(language) }, () => {
            next();
        });
    }
}
