import { Injectable } from '@angular/core';
import { QueryTokenDTO, AbholfachService } from '@swagger/oms';
import { map } from 'rxjs/operators';
import { Store } from '@ngxs/store';
import { GoodsInSelectors } from '../store/selectors/goods-in.selectors';

@Injectable({
  providedIn: 'root',
})
export class GoodsInService {
  constructor(private abholfach: AbholfachService, private store: Store) {}

  search(input: string, skip: number = 0, take: number = 0) {
    const params = <QueryTokenDTO>{
      input: {
        qs: input,
      },
      skip,
      take,
    };
    return this.abholfach.AbholfachWareneingangsuche(params).pipe(
      map((response) => {
        if (response.error) {
          throw new Error(response.message);
        }
        return { results: response.result, hits: response.hits };
      })
    );
  }

  searchCachedOrders() {
    return this.store.select(GoodsInSelectors.getCachedGoodsInOrders);
  }
}
