import {
  Component,
  EventEmitter,
  Input,
  OnChanges,
  Output,
  SimpleChanges,
} from '@angular/core';

@Component({
  selector: 'app-toast',
  templateUrl: './toast.component.html',
  styleUrls: ['./toast.component.scss'],
})
export class ToastComponent implements OnChanges {
  @Input() isVisible = false;
  @Input() shouldHideAutomatically = true;
  @Input() showCloseButton = true;
  @Input() delay = 5000;

  @Output() isVisibleChange = new EventEmitter<boolean>();

  ngOnChanges(changes: SimpleChanges): void {
    if (changes['isVisible']?.currentValue) {
      if (!!this.shouldHideAutomatically && this.isVisible) {
        setTimeout(() => this.hideToast(), this.delay);
      }
    }
  }

  hideToast() {
    this.isVisible = false;
    this.isVisibleChange.emit(this.isVisible);
  }

  handleClose() {
    this.isVisibleChange.emit(false);
  }
}
