import { Injectable, inject } from '@angular/core';
import { PickupShelfIOService } from '@domain/pickup-shelf';
import { FilterAutocomplete, FilterAutocompleteProvider, FilterInput } from '@shared/components/filter';
import { Observable, of } from 'rxjs';
import { catchError, map } from 'rxjs/operators';

@Injectable()
export class PickUpShelfOutAutocompleteProvider extends FilterAutocompleteProvider {
  private _pickupShelfIOService = inject(PickupShelfIOService);

  for = 'pickup-shelf-out';

  constructor() {
    super();
  }

  complete(input: FilterInput): Observable<FilterAutocomplete[]> {
    const token = input?.parent?.parent?.getQueryToken();
    const filter = token?.filter;
    const type = Object.keys(token?.input).join(';');

    if (input.value.length >= 3) {
      return this._pickupShelfIOService
        .complete({
          filter,
          input: input.value,
          take: 5,
          type,
        })
        .pipe(
          catchError(() => of({ result: [] })),
          map((res) => res.result),
        );
    } else {
      return of([]);
    }
  }
}
