import { AfterViewInit, ChangeDetectionStrategy, Component, EventEmitter, Input, Output } from '@angular/core';
import { ErrorSeverity } from '@msfa-enums/error-severity.enum';
import { IconType } from '@msfa-enums/icon-type.enum';
import { CustomError } from '@msfa-models/error/custom-error';

@Component({
  selector: 'msfa-toast',
  templateUrl: './toast.component.html',
  styleUrls: ['./toast.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ToastComponent implements AfterViewInit {
  @Input() error: CustomError;
  @Output() closeToast = new EventEmitter<CustomError>();

  iconType = IconType;
  errorSeverity = ErrorSeverity;

  ngAfterViewInit(): void {
    if (this.error.removeAfter) {
      setTimeout(() => {
        this.closeToast.emit(this.error);
      }, this.error.removeAfter);
    }
  }

  get className(): string {
    return `toast toast--${this.error.severity.toLowerCase()}`;
  }

  emitCloseEvent(): void {
    this.closeToast.emit(this.error);
  }
}
