/**
 * @aurora-catalyst-generated
 * @source cliter/o-auth/authorization-code.aurora.yaml
 */
import { OAuthAuthorizationCode } from '@api/graphql';
import {
  OAuthDeleteAuthorizationCodesService,
  OAuthGetAuthorizationCodesService,
} from '@app/o-auth/authorization-code';
import { OAuthAuthorizationCodeFieldSchema } from '@app/o-auth/authorization-code/domain';
import { EmitEvent, Format, HandlerMeta } from '@aurorajs.dev/core-back';
import { QueryStatement } from '@aurorajs.dev/core-common';
import { Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';

@Injectable()
export class OAuthDeleteAuthorizationCodesHandler {
  constructor(
    private readonly eventEmitter: EventEmitter2,
    private readonly deleteService: OAuthDeleteAuthorizationCodesService,
    private readonly getService: OAuthGetAuthorizationCodesService,
  ) {}

  @EmitEvent('oAuth.authorizationCode.deleted')
  @Format(OAuthAuthorizationCodeFieldSchema)
  async main(
    queryStatement?: QueryStatement,
    constraint?: QueryStatement,
    handlerMeta?: HandlerMeta,
  ): Promise<OAuthAuthorizationCode[]> {
    const authorizationCodes = await this.getService.main(queryStatement, constraint, handlerMeta);

    await this.deleteService.main(queryStatement, constraint, handlerMeta);

    return authorizationCodes;
  }
}
