import { Injectable, Logger } from '@nestjs/common';
import { RedmineTypes } from '../models/redmine-types';
import { IssueEnhancerInterface } from './issue-enhancer-interface';

@Injectable()
export class EnhancerService {
  private logger = new Logger(EnhancerService.name);
  private enhancers: IssueEnhancerInterface[] = [];

  addEnhancer(enh: IssueEnhancerInterface[]): void {
    this.enhancers.push(...enh);
  }

  async enhanceIssue(
    issue: RedmineTypes.Issue & Record<string, any>,
  ): Promise<RedmineTypes.Issue & Record<string, any>> {
    for (let i = 0; i < this.enhancers.length; i++) {
      const enhancer = this.enhancers[i];
      // eslint-disable-next-line prettier/prettier
      this.logger.debug(`Enhancer "${enhancer.name}" for issue.id = ${issue.id} start`);
      issue = await enhancer.enhance(issue);
      this.logger.debug(
        // eslint-disable-next-line prettier/prettier
        `Enhancer "${enhancer.name}" for issue.id = ${issue.id} finished`,
      );
    }
    return issue;
  }
}
