import { inject, Injectable } from '@angular/core';
import { firstValueFrom } from 'rxjs';
import { injectDialog } from '@isa/ui/dialog';
import { RemissionStartDialogComponent } from './remission-start-dialog.component';
import { RemissionStore } from '@isa/remission/data-access';

@Injectable({ providedIn: 'root' })
export class RemissionStartService {
  #remissionStartDialog = injectDialog(RemissionStartDialogComponent);
  #remissionStore = inject(RemissionStore);

  async startRemission(returnGroup: string | undefined) {
    const remissionStartDialogRef = this.#remissionStartDialog({
      data: { returnGroup },
      classList: ['gap-0'],
      width: '30rem',
    });

    const result = await firstValueFrom(remissionStartDialogRef.closed);

    if (result) {
      const { returnId, receiptId } = result;
      this.#remissionStore.startRemission({
        returnId,
        receiptId,
      });
    }
  }

  // #5289 - Bei WBS ohne Wannennummer, soll man nur die Wannennummer generieren können
  async assignPackage({
    returnId,
    receiptId,
  }: {
    returnId: number;
    receiptId: number;
  }) {
    const remissionStartDialogRef = this.#remissionStartDialog({
      data: {
        returnGroup: undefined,
        assignPackage: {
          returnId,
          receiptId,
        },
      },
      classList: ['gap-0'],
      width: '30rem',
    });
    return await firstValueFrom(remissionStartDialogRef.closed);
  }
}
