/**
 * UserPreferenceResolver
 *
 * nestjs-i18n resolver that reads the authenticated user's preferredLanguage
 * from the request (populated by JwtAuthGuard). Highest priority in the chain.
 */

import { Injectable, ExecutionContext } from '@nestjs/common';
import { I18nResolver } from 'nestjs-i18n';
import { isLocale } from '@domain/value-objects/locale.vo';

@Injectable()
export class UserPreferenceResolver implements I18nResolver {
  resolve(context: ExecutionContext): string | undefined {
    const request = context.switchToHttp().getRequest();
    const preferred = request?.user?.preferredLanguage;
    return isLocale(preferred) ? preferred : undefined;
  }
}
