import {
  Injectable,
  ExecutionContext,
  HttpException,
  HttpStatus,
} from '@nestjs/common';
import { ThrottlerGuard, ThrottlerLimitDetail } from '@nestjs/throttler';
import AppResponse from '../response/ktq-response';
import { SystemLang } from '../lang/system.lang';

@Injectable()
export class RateLimitGuard extends ThrottlerGuard {
  protected async throwThrottlingException(
    context: ExecutionContext,
    throttlerLimitDetail: ThrottlerLimitDetail,
  ): Promise<void> {
    throw new HttpException(
      AppResponse.toResponse(null, {
        message: SystemLang.getText('messages', 'too_many_request'),
        status_code: HttpStatus.TOO_MANY_REQUESTS,
      }),
      HttpStatus.TOO_MANY_REQUESTS,
    );
  }
}
