import { buildReducer, mapReducer } from 'redux-keto'


import { accountReducer, } from './account/account-reducer'



import { currency, } from './currency/currency-reducer'
import { login, } from './login/login-reducer'
import { plugins, } from './plugins/plugins-reducer'
import { storageWallets, } from './storage/storage-reducer'

























export const defaultLogSettings = {
  sources: {},
  defaultLogLevel: 'warn'
}

const dummyClientInfo = { clientId: new Uint8Array(0) }

export const reducer = buildReducer({
  accountCount(state = 0, action) {
    return action.type === 'LOGIN' ? state + 1 : state
  },

  accountIds(state = [], action, next) {
    switch (action.type) {
      case 'LOGIN':
        return [...state, next.lastAccountId]

      case 'LOGOUT': {
        const { accountId } = action.payload
        const out = state.filter(id => id !== accountId)
        if (out.length === state.length) {
          throw new Error(`Login ${accountId} does not exist`)
        }
        return out
      }

      case 'CLOSE':
        return []
    }
    return state
  },

  accounts: mapReducer(accountReducer, next => next.accountIds),

  changeServers(state = [], action) {
    return action.type === 'INIT' ? action.payload.changeServers : state
  },

  clientInfo(state = dummyClientInfo, action) {
    switch (action.type) {
      case 'INIT':
        return action.payload.clientInfo
      case 'LOGIN_DURESS_MODE_DISABLED': {
        return {
          ...state,
          duressLoginId: undefined
        }
      }
      case 'LOGIN_DURESS_MODE_ENABLED': {
        return {
          ...state,
          duressLoginId: action.payload.duressLoginId
        }
      }
    }
    return state
  },

  contextAppId: (state = '', action) => {
    return action.type === 'LOGIN' ? action.payload.appId : state
  },

  hideKeys(state = true, action) {
    return action.type === 'INIT' ? action.payload.hideKeys : state
  },

  infoCache(state = {}, action) {
    switch (action.type) {
      case 'INIT':
        return action.payload.infoCache
      case 'INFO_CACHE_FETCHED':
        return action.payload
    }
    return state
  },

  infoServers(state = [], action) {
    return action.type === 'INIT' ? action.payload.infoServers : state
  },

  lastAccountId(state, action, next) {
    return `login${next.accountCount}`
  },

  logSettings(state = defaultLogSettings, action) {
    switch (action.type) {
      case 'INIT':
        return action.payload.logSettings
      case 'CHANGE_LOG_SETTINGS':
        return action.payload
    }
    return state
  },

  paused(state = false, action) {
    return action.type === 'PAUSE' ? action.payload : state
  },

  ready(state = false, action) {
    return action.type === 'INIT' ? true : state
  },

  skipBlockHeight(state = false, action) {
    return action.type === 'INIT' ? action.payload.skipBlockHeight : state
  },

  syncServers(state = [], action) {
    return action.type === 'INIT' ? action.payload.syncServers : state
  },

  currency,
  login,
  plugins,
  storageWallets
})
