/**
 * @aurora-catalyst-generated
 * @source cliter/iam/account.aurora.yaml
 */
import { IamGetAccountsService } from '@app/iam/account';
import { HandlerMeta } from '@aurorajs.dev/core-back';
import { Injectable } from '@nestjs/common';

@Injectable()
export class IamCheckUniqueEmailAccountHandler {
  constructor(private readonly getService: IamGetAccountsService) {}

  async main(
    email: string,
    avoidEmails: string[] = [],
    handlerMeta?: HandlerMeta,
  ): Promise<boolean> {
    const normalized = email.toLowerCase();
    const avoid = new Set(avoidEmails.map((value) => value.toLowerCase()));
    const matches = await this.getService.main(
      { where: { email: normalized } },
      undefined,
      handlerMeta,
    );
    return matches.every((account) => avoid.has((account.email ?? '').toLowerCase()));
  }
}
