import { inject, Injectable } from '@angular/core';
import { OMSPrintService } from '@generated/swagger/print-api';
import { PrinterType, PrintService } from '@isa/common/print';
import { firstValueFrom } from 'rxjs';

@Injectable({ providedIn: 'root' })
export class PrintTolinoReturnReceiptService {
  #omsPrintService = inject(OMSPrintService);
  #printService = inject(PrintService);

  /**
   * Prints a Tolino return receipt using the office printer.
   *
   * @param params - The parameters for printing.
   * @param params.returnReceiptId - The unique identifier of the return receipt to print.
   * @returns A promise that resolves when the print job is completed.
   * @throws {Error} If no return receipt ID is provided.
   *
   * @example
   * await printTolinoReturnReceiptService.printTolinoReturnReceipt({ returnReceiptId: 123 });
   */
  async printTolinoReturnReceipt({
    returnReceiptId,
  }: {
    returnReceiptId: number;
  }) {
    if (!returnReceiptId) {
      throw new Error('No return receipt ID provided');
    }

    return this.#printService.print({
      printerType: PrinterType.OFFICE,
      printFn: (printer) => {
        return firstValueFrom(
          this.#omsPrintService.OMSPrintTolinoRetourenschein({
            printer: printer.key,
            data: returnReceiptId,
          }),
        );
      },
      directPrint: false,
    });
  }
}
