import { Map } from 'immutable';
import { createSelector } from 'reselect';

import { getItemsIdsWithNoBids, getMyBidItemIds, getMyWinningItemIds } from './auctions.js';

const getState = (state) => state;

export const getItem = (state, itemId) => state.getIn(['items', itemId], false);

export const getItems = createSelector(
  [getState],
  (state) => state.get('items') || new Map(),
);

export const getItemsAsList = createSelector(
  [getItems],
  (itemsAsMap) => itemsAsMap.toList(),
);

export const getAuctionItems = createSelector(
  [getState],
  (state) => state.get('items').filter(i => i.type === 'auction') || new Map(),
);

export const getAuctionItemsAsList = createSelector(
  [getAuctionItems],
  (auctionItemsAsMap) => auctionItemsAsMap.toList(),
);

export const getAuctionItemsUserIsBidding = createSelector(
  [getAuctionItems, getMyBidItemIds],
  (items, myBids) => items.filter(i => myBids.indexOf(i.id)) || new Map(),
);

export const getAuctionItemsUserIsBiddingAsList = createSelector(
  [getAuctionItemsUserIsBidding],
  (auctionItemsAsMap) => auctionItemsAsMap.toList(),
);

export const getAuctionItemsUserIsWinning = createSelector(
  [getAuctionItemsUserIsBidding, getMyWinningItemIds],
  (items, myWins) => items.filter(i => myWins.indexOf(i.id)) || new Map(),
);

export const getAuctionItemsUserIsWinningAsList = createSelector(
  [getAuctionItemsUserIsWinning],
  (auctionItemsAsMap) => auctionItemsAsMap.toList(),
);

export const getAuctionItemsWithNoBids = createSelector(
  [getAuctionItems, getItemsIdsWithNoBids],
  (items, noBids) => items.filter(i => noBids.indexOf(i.id)) || new Map(),
);

export const getAuctionItemsWithNoBidsAsList = createSelector(
  [getAuctionItemsWithNoBids],
  (auctionItemsAsMap) => auctionItemsAsMap.toList(),
);

export const getTicketItems = createSelector(
  [getState],
  (state) => state.get('items').filter(i => i.type === 'ticket') || new Map(),
);

export const getTicketItemsAsList = createSelector(
  [getTicketItems],
  (ticketItemsAsMap) => ticketItemsAsMap.toList(),
);
