import { Injectable, inject } from '@angular/core';
import {
  AbholfachService,
  AutocompleteTokenDTO,
  ListResponseArgsOfDBHOrderItemListItemDTO,
  QueryTokenDTO,
} from '@generated/swagger/oms-api';
import { PickupShelfIOService } from './pickup-shelf-io.service';
import { Observable, throwError } from 'rxjs';
import { Filter } from '@shared/components/filter';

@Injectable({ providedIn: 'root' })
export class PickupShelfOutService extends PickupShelfIOService {
  private _abholfachService = inject(AbholfachService);

  name() {
    return 'PickupShelfOutService';
  }

  getQuerySettings() {
    return this._abholfachService.AbholfachWarenausgabeQuerySettings();
  }

  search(queryToken: QueryTokenDTO) {
    return this._abholfachService.AbholfachWarenausgabe(queryToken);
  }

  complete(autocompleteToken: AutocompleteTokenDTO) {
    return this._abholfachService.AbholfachWarenausgabeAutocomplete(autocompleteToken);
  }

  getOrderItemsByOrderNumberOrCompartmentCode(args: {
    orderNumber?: string;
    compartmentCode?: string;
    filter?: Filter;
  }): Observable<ListResponseArgsOfDBHOrderItemListItemDTO> {
    if (!args.orderNumber && !args.compartmentCode) {
      return throwError(
        'PickupShelfOutService.getOrderItemsByOrderNumberOrCompartmentCode(): Either orderNumber or compartmentCode must be provided.',
      );
    }

    const { orderdate, supplier_id } = args.filter?.getQueryToken()?.filter ?? {};

    return this._abholfachService.AbholfachWarenausgabe({
      input: {
        qs: args.compartmentCode ?? args.orderNumber,
      },
      filter: {
        archive: String(true),
        all_branches: String(true),
        orderdate,
        supplier_id,
      },
    });
  }

  getOrderItemsByCustomerNumber(args: {
    customerNumber: string;
  }): Observable<ListResponseArgsOfDBHOrderItemListItemDTO> {
    throw new Error('Method not implemented.');
  }
}
