import { AppState, AppStateModel } from '../state/app.state';
import { Selector } from '@ngxs/store';
import { OrderDTO } from '@swagger/oms';
import { CollectingShelfStateModel, CollectingShelfState } from '../state/collecting-shelf.state';
import { CollectingShelfOrder } from '../../models/collecting-shelf-order.model';
import { mapToIterable } from '../../utils/app.utils';
import { ModuleSwitcher } from '../../models/app-switcher.enum';

export class CollectingShelfSelectors {
  @Selector([AppState, CollectingShelfState])
  static getShelfEditOrder(state: AppStateModel, shelf: CollectingShelfStateModel): OrderDTO {
    const currentProcessId = state.activeModule === ModuleSwitcher.Customer ? state.currentProcesssId : -1;
    const order = shelf.editOrder && shelf.editOrder[currentProcessId] ? shelf.editOrder[currentProcessId] : null;
    return order;
  }

  @Selector([AppState, CollectingShelfState])
  static getCachedResults(state: AppStateModel, shelf: CollectingShelfStateModel): { result: CollectingShelfOrder[]; hits: number } {
    const currentProcessId = state.currentProcesssId;
    const cachedSearchResults =
      shelf.cachedSearchResults && shelf.cachedSearchResults[currentProcessId] ? shelf.cachedSearchResults[currentProcessId] : null;
    return cachedSearchResults;
  }

  @Selector([AppState, CollectingShelfState])
  static getShelfOrders(state: AppStateModel, shelf: CollectingShelfStateModel): CollectingShelfOrder[] {
    const currentProcessId = state.currentProcesssId;
    const orders = shelf.orders && shelf.orders[currentProcessId] ? shelf.orders[currentProcessId] : null;
    return mapToIterable(orders);
  }

  @Selector([AppState, CollectingShelfState])
  static getShelfCacheState(state: AppStateModel, shelf: CollectingShelfStateModel): boolean {
    const currentProcessId = state.currentProcesssId;
    return shelf.useCache[currentProcessId] ? shelf.useCache[currentProcessId] : false;
  }
}
