import { Injectable } from '@angular/core';
import { MatDialog, MatDialogConfig, MatDialogRef } from '@angular/material/dialog';
import { map, Observable, take } from 'rxjs';
import { ConfirmDialogComponent } from '../../components/confirm-dialog/confirm-dialog.component';

@Injectable({
  providedIn: 'root'
})
export class ConfirmService {

  dialogRef!: MatDialogRef<ConfirmDialogComponent>;

  constructor(
    private dialog: MatDialog
  ) { }

  public open(options: MatDialogConfig) {
    this.dialogRef = this.dialog.open(ConfirmDialogComponent, options);
  }

  public confirmed(): Observable<any> {
    return this.dialogRef.afterClosed().pipe(take(1), map(res => {
      return res;
    }
    ));
  }
}
