import Vue from 'vue'
import stores from '@/stores/api/stores'
import { optionsFor } from '@/stores/storeStatus'
import {
  createMetaModule,
  withMeta,
  metaStatuses,
  metaStatusesWithId,
  indexById,
  createRouteError,
  toggles,
} from '@/utils/datastore/helpers'
import router from '@/base/router'

function initialState () {
  return {
    entries: {},
    statistics: {},
    activeStoreId: null,
  }
}

export default {
  namespaced: true,
  modules: {
    meta: createMetaModule(),
    toggle: toggles({
      showAll: false,
    }),
  },
  state: initialState(),
  getters: {
    all: (state, getters) => Object.values(state.entries).map(getters.enrich).sort(sortByName).sort(sortByStatus),
    notArchived: (state, getters) => getters.all.filter(s => s.status !== 'archived'),
    archived: (state, getters) => getters.all.filter(s => s.status === 'archived'),
    filtered: (state, getters) => getters.notArchived.filter(store => getters['toggle/showAll'] || store.status === 'active'),
    byCurrentGroup: (state, getters, rootState, rootGetters) => getters.filtered.filter(({ group }) => group && group.isCurrentGroup),
    byCurrentGroupArchived: (state, getters, rootState, rootGetters) => getters.archived.filter(({ group }) => group && group.isCurrentGroup),
    get: (state, getters) => id => getters.enrich(state.entries[id]),
    enrich: (state, getters, rootState, rootGetters) => store => {
      return store && {
        ...store,
        ...metaStatusesWithId(getters, ['save'], store.id),
        ui: optionsFor(store),
        group: rootGetters['groups/get'](store.group),
        statistics: state.statistics[store.id],
        isActiveStore: store.id === state.activeStoreId,
      }
    },
    activeStore: (state, getters) => getters.get(state.activeStoreId),
    activeStoreId: state => state.activeStoreId,
    ...metaStatuses(['create']),
  },
  actions: {
    ...withMeta({
      async save ({ dispatch }, store) {
        dispatch('update', [await stores.save(store)])
        router.push({ name: 'store', params: { storeId: store.id } })
      },
      async create ({ dispatch, rootGetters }, store) {
        const createdStore = await stores.create({
          ...store,
          group: rootGetters['currentGroup/id'],
        })
        dispatch('update', [createdStore])
        router.push({ name: 'store', params: { storeId: createdStore.id } })
      },
      async fetch ({ commit }) {
        commit('set', await stores.list())
      },

    }),
    ...withMeta({
      async selectStore ({ commit, dispatch, getters }, { storeId }) {
        if (!getters.get(storeId)) {
          try {
            const store = await stores.get(storeId)
            commit('update', [store])
          }
          catch (error) {
            throw createRouteError()
          }
        }
        const getStatistics = stores.statistics(storeId)
        dispatch('sidenavBoxes/toggle/group', false, { root: true })
        commit('select', storeId)
        commit('setStatistics', { data: await getStatistics, id: storeId })
      },
    }, {
      findId: ({ storeId }) => storeId,
    }),

    clearSelectedStore ({ commit, dispatch }) {
      dispatch('sidenavBoxes/toggle/group', true, { root: true })
      commit('clearSelected')
    },

    update ({ commit, dispatch, getters }, update) {
      const old = getters.get(update.id)
      // make sure we refresh pickups if store status changes
      // TODO move to vuex plugin in pickups module
      if (old && old.status !== update.status) {
        if (old.status === 'active' || update.status === 'active') {
          commit('pickups/clearUpcomingForStore', old.id, { root: true })
          dispatch('pickups/fetchListByGroupId', old.group.id, { root: true })
        }
      }
      commit('update', [update])
    },
  },
  mutations: {
    select (state, storeId) {
      state.activeStoreId = storeId
    },
    clearSelected (state) {
      state.activeStoreId = null
    },
    set (state, stores) {
      state.entries = indexById(stores)
    },
    clear (state) {
      Object.assign(state, initialState())
    },
    update (state, stores) {
      for (const store of stores) {
        Vue.set(state.entries, store.id, store)
      }
    },
    setStatistics (state, { id, data }) {
      Vue.set(state.statistics, id, data)
    },
  },
}

export function sortByStatus (a, b) {
  return a.ui.sort - b.ui.sort
}

export function sortByName (a, b) {
  return a.name.localeCompare(b.name)
}

@@ -3323,18 +3323,56 @@
  %7D, 
-update
+stores) %7B%0A      for (const store of stores
 ) %7B%0A
+  
     
@@ -3401,19 +3401,20 @@
 get(
-updat
+stor
 e.id)%0A
+  
     
@@ -3477,16 +3477,18 @@
 s%0A      
+  
 // TODO 
@@ -3527,24 +3527,26 @@
 odule%0A      
+ 
+ 
 if (old && o
@@ -3559,21 +3559,20 @@
 tus !== 
-updat
+stor
 e.status
@@ -3571,24 +3571,26 @@
 e.status) %7B%0A
+  
         if (
@@ -3616,21 +3616,20 @@
 ive' %7C%7C 
-updat
+stor
 e.status
@@ -3647,32 +3647,34 @@
 e') %7B%0A          
+ 
+ 
 commit('pickups/
@@ -3713,32 +3713,34 @@
 %7B root: true %7D)%0A
+  
           dispat
@@ -3814,18 +3814,22 @@
     
-%7D%0A
+  %7D%0A  
       %7D%0A
     
@@ -3820,24 +3820,32 @@
 %7D%0A        %7D%0A
+      %7D%0A
       commit
@@ -3859,16 +3859,14 @@
 e', 
-%5Bupdate%5D
+stores
 )%0A  

fix store update action