import moment from 'moment'
import sortBy from 'lodash/sortBy'

const initialState = {
  messages: [],
  savedMessages: [],
  filter: 'all'
}

const toggleSavedMessage = (message) => {
  return {
    type: 'toggle-saved-message',
    message
  }
}

const setMessageFilter = (value) => {
  return {
    type: 'set-message-filter',
    value
  }
}

const getVisibleMessages = (state, filter) => {
  switch(filter) {
    case 'successful':
      return sortBy(state.messages.filter(m => m.hasError === false), 'timestamp')
    case 'failed':
      return sortBy(state.messages.filter(m => m.hasError === true), 'timestamp')
    case 'saved':
      return sortBy(state.savedMessages, 'timestamp')
    default:
      return sortBy(state.messages, 'timestamp')
  }
}

export {
  getVisibleMessages,
  setMessageFilter,
  toggleSavedMessage
}

const insert = (array, index, ...items) => {
  return [
    ...array.slice(0, index),
    ...items,
    ...array.slice(index)
  ]
}

export default (state = initialState, action = {}) => {
  switch(action.type) {
    case 'bus-message-succeeded':
      action.envelope.saved = false
      action.envelope.timestamp = moment().format()
      return {
        ...state,
        messages: insert(state.messages, 0, action.envelope)
      }
    case 'bus-message-failed':
      action.envelope.saved = false
      action.envelope.timestamp = moment().format()
      return {
        ...state,
        messages: insert(state.messages, 0, action.envelope)
      }
    case 'set-message-filter':
      return {
        ...state,
        filter: action.value
      }
    case 'toggle-saved-message': {
      const save = !action.message.saved

      if (save === true) {
        return {
          ...state,
          messages: state.messages.map(m => m.correlationId == action.message.correlationId ?
            { ...m, saved: save} :
            m
          ),
          savedMessages: [
            ...state.savedMessages.filter(m => m.correlationId !== action.message.correlationId),
            { ...action.message, saved: save }
          ]
        }
      } else {
        return {
          ...state,
          messages: state.messages.map(m => m.correlationId == action.message.correlationId ?
            { ...m, saved: save} :
            m
          ),
          savedMessages: [
            ...state.savedMessages.filter(m => m.correlationId !== action.message.correlationId)
          ]
        }
      }
    }
    default: return state
  }
}

@@ -28,20 +28,21 @@
 %0Aimport 
-s
 or
-t
+der
 By from 
@@ -49,20 +49,21 @@
 'lodash/
-s
 or
-t
+der
 By'%0A%0Acon
@@ -437,36 +437,37 @@
 ':%0A      return 
-s
 or
-t
+der
 By(state.message
@@ -506,32 +506,40 @@
 se), 'timestamp'
+, 'desc'
 )%0A    case 'fail
@@ -548,36 +548,37 @@
 ':%0A      return 
-s
 or
-t
+der
 By(state.message
@@ -620,24 +620,32 @@
  'timestamp'
+, 'desc'
 )%0A    case '
@@ -657,36 +657,37 @@
 ':%0A      return 
-s
 or
-t
+der
 By(state.savedMe
@@ -701,24 +701,32 @@
  'timestamp'
+, 'desc'
 )%0A    defaul
@@ -745,12 +745,13 @@
 urn 
-s
 or
-t
+der
 By(s
@@ -776,16 +776,24 @@
 mestamp'
+, 'desc'
 )%0A  %7D%0A%7D%0A
@@ -1193,32 +1193,35 @@
 moment().format(
+'x'
 )%0A      return %7B
@@ -1425,16 +1425,19 @@
 .format(
+'x'
 )%0A      

Fix message sorting