import { ElementRef, Injectable } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
import { take } from 'rxjs';
import { ConfirmDialogComponent } from '../component/confirm-dialog/confirm-dialog.component';

@Injectable({
  providedIn: 'root',
})
export class ConfirmDialogService {
  private isDialogOpen = false;

  constructor(private dialog: MatDialog) {}

  openDialog(): void {
    if (!this.isDialogOpen) {
      this.isDialogOpen = true;
      const dialogRef = this.dialog.open(ConfirmDialogComponent);

      dialogRef
        .afterClosed()
        .pipe(take(1))
        .subscribe(() => {
          this.isDialogOpen = false;
        });
    }
  }
}
