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 PickupShelfInService extends PickupShelfIOService {
  private _abholfachService = inject(AbholfachService);

  name() {
    return 'PickupShelfInService';
  }

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

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

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

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

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

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

  getOrderItemsByCustomerNumber(args: {
    customerNumber: string;
  }): Observable<ListResponseArgsOfDBHOrderItemListItemDTO> {
    return this._abholfachService.AbholfachWareneingang({
      filter: { orderitemprocessingstatus: '16;128;8192;1048576' },
      input: {
        customer_no: args.customerNumber,
      },
    });
  }
}
