import { Injectable, inject } from '@angular/core';
import { UiModalService } from '@ui/modal';
import { SendOrderConfirmationModalComponent } from './send-order-confirmation-modal.component';
import { SendOrderConfirmationModalData, SendOrderConfirmationModalResult } from './types';

@Injectable({ providedIn: 'root' })
export class SendOrderConfirmationModalService {
  private modal = inject(UiModalService);

  async open(data: SendOrderConfirmationModalData): Promise<SendOrderConfirmationModalResult | undefined> {
    // Darf nur für Abholfachbestellungen geöffnet werden
    const orders = data.filter((order) => order.features?.orderType === 'Abholung');

    const modalRef = this.modal.open<SendOrderConfirmationModalResult, SendOrderConfirmationModalData>({
      content: SendOrderConfirmationModalComponent,
      data: orders,
    });

    const result = await modalRef.afterClosed$.toPromise();

    return result.data;
  }
}
