'use strict'

import * as types from '../constants/action-types'

import { NUMBER_OF_FIRST_RESPONSE_PAGE } from '../constants/authors-list'
import assign from 'lodash/assign'
import get from 'lodash/get'
import uniq from 'lodash/uniq'

const _ = {
  assign,
  get,
  uniq
}

const initSearchedAuthorsListStates = {
  keywords: '',
  isFetching: false,
  currentPage: NUMBER_OF_FIRST_RESPONSE_PAGE - 1,
  hasMore: false,
  items: [],
  lastUpdated: 0
}

export const searchedAuthorsList = (state = initSearchedAuthorsListStates, action = {}) => {
  const keywords = _.get(action, 'keywords', '')
  switch (action.type) {
    case types.SEARCH_AUTHORS_REQUEST:
      return _.assign({}, initSearchedAuthorsListStates, {
        keywords,
        isFetching: true
      })
    case types.SEARCH_AUTHORS_SUCCESS:
      const currentPage = _.get(action, 'currentPage', NUMBER_OF_FIRST_RESPONSE_PAGE - 1)
      const totalPages = _.get(action, 'totalPages', 0)
      const nextAuthorsInList = _.get(action, 'response.result', [])
      return _.assign({}, state, {
        keywords,
        isFetching: false,
        currentPage,
        hasMore: currentPage - NUMBER_OF_FIRST_RESPONSE_PAGE + 1 < totalPages,
        items: nextAuthorsInList,
        lastUpdated: action.receivedAt
      })
    case types.SEARCH_AUTHORS_FAILURE:
      return _.assign({}, state, {
        keywords,
        isFetching: false,
        error: action.error,
        lastUpdated: action.failedAt
      })
    default:
      return state
  }
}

const initAuthorsListStates = {
  isFetching: false,
  currentPage: NUMBER_OF_FIRST_RESPONSE_PAGE - 1,
  hasMore: false,
  items: [],
  lastUpdated: 0
}

export const authorsList = (state = initAuthorsListStates, action = {}) => {
  switch (action.type) {
    case types.LIST_ALL_AUTHORS_REQUEST:
      return _.assign({}, state, {
        isFetching: true
      })
    case types.LIST_ALL_AUTHORS_SUCCESS:
      const currentPage = _.get(action, 'currentPage', NUMBER_OF_FIRST_RESPONSE_PAGE - 1)
      const totalPages = _.get(action, 'totalPages', 0)
      const previousAuthorsInList = _.get(state, 'items', [])
      const nextAuthorsInList = previousAuthorsInList.concat(_.get(action, 'response.result', []))  // Concat array if list all
      return _.assign({}, state, {
        isFetching: false,
        currentPage,
        hasMore: currentPage - NUMBER_OF_FIRST_RESPONSE_PAGE + 1 < totalPages,
        items: nextAuthorsInList,
        lastUpdated: action.receivedAt
      })
    case types.LIST_ALL_AUTHORS_FAILURE:
      return _.assign({}, state, {
        isFetching: false,
        error: action.error,
        lastUpdated: action.failedAt
      })
    default:
      return state
  }
}

@@ -395,34 +395,16 @@
 GE - 1,%0A
-  hasMore: false,%0A
   items:
@@ -420,32 +420,244 @@
 stUpdated: 0%0A%7D%0A%0A
+/*  %0A  Algolia set hitsPerPage limit up to 1000 items per search.%0A  So if number of authors grows over 1000,%0A  it will need to save hasMore in state to add loadmore function as case LIST_ALL_AUTHORS_SUCCESS.%0A*/%0A%0A
 export const sea
@@ -1000,223 +1000,8 @@
 SS:%0A
-      const currentPage = _.get(action, 'currentPage', NUMBER_OF_FIRST_RESPONSE_PAGE - 1)%0A      const totalPages = _.get(action, 'totalPages', 0)%0A      const nextAuthorsInList = _.get(action, 'response.result', %5B%5D)%0A
     
@@ -1095,35 +1095,33 @@
 rentPage
-,%0A        hasMore: 
+: _.get(action, '
 currentP
@@ -1115,34 +1115,34 @@
 on, 'currentPage
- -
+',
  NUMBER_OF_FIRST
@@ -1156,32 +1156,20 @@
 SE_PAGE 
-+ 1 %3C totalPages
+- 1)
 ,%0A      
@@ -1177,33 +1177,52 @@
  items: 
-nextAuthorsInList
+_.get(action, 'response.result', %5B%5D)
 ,%0A      

Remove currentPage and hasMore