import { Injectable } from '@angular/core';
import { ConfirmationService } from 'primeng/api';
import { TranslateService } from '@ngx-translate/core';
import { ConfirmationDialog } from 'src/app/model/utils/confirmation-dialog';

export interface ConfirmationDialogOptions {
  header: string;
  message: string;
  accept?: () => void;
  reject?: () => void;
  headerParams?: object;
  messageParams?: object;
}

@Injectable({
  providedIn: 'root',
})
export class ConfirmationDialogService {
  constructor(
    private translate: TranslateService,
    private confirmationService: ConfirmationService
  ) {}

  confirmDialog({
    header,
    message,
    accept,
    reject,
    headerParams = undefined,
    messageParams = undefined,
  }: ConfirmationDialogOptions) {
    const options: ConfirmationDialog = {
      key: 'confirmConfirmationDialog',
      header: this.translate.instant(header, headerParams),
      message: this.translate.instant(message, messageParams),
    };

    if (accept) {
      options.accept = accept;
    }

    if (reject) {
      options.reject = reject;
    }

    this.confirmationService.confirm(options);
  }
}
