import { combineReducers } from 'redux';
import Immutable from 'immutable';
import {
  REQUEST_CHANNELS,
  REQUEST_SOURCES,
  RECEIVE_CHANNELS,
  RECEIVE_SOURCES
} from 'actions/filters';

export const FeedFilter = new Immutable.Record({
  date: null,
  keywords: new Immutable.List(),
  channels: new Immutable.List(),
  symbols: new Immutable.List(),
  watchlists: new Immutable.List(),
  contentType: new Immutable.List(['story', ])  // BZ source only initially
});

const Channels = new Immutable.Record({
  isFetching: false,
  items: new Immutable.List()
});

const Sources = new Immutable.Record({
  isFetching: false,
  contentTypes: new Immutable.List(),
  pressReleaseTypes: new Immutable.List()
});

export function channels(state = new Channels(), action) {
  switch (action.type) {
  // Getting new channels
  case REQUEST_CHANNELS:
    return state.set('isFetching', true);
  // Receiving a list of channels
  case RECEIVE_CHANNELS:
    return state.set('isFetching', false).set('items', new Immutable.List(action.channels));
  default:
    return state;
  }
}

export function sources(state = new Sources(), action) {
  switch (action.type) {
  // Getting new sources
  case REQUEST_SOURCES:
    return state.set('isFetching', true);
  // Receiving a list of sources
  case RECEIVE_SOURCES:
    return state.set('isFetching', false)
      .set('contentTypes', new Immutable.List(action.sources))
      .set('pressReleaseTypes', new Immutable.List(action.pressReleaseTypes));
  default:
    return state;
  }
}
