import PersistedState from '../PersistedState'
import { getGoodsclassList } from '../../api/homesearch'

// initial state
const state = {
  items: PersistedState.getStorage('goodsclass','items',[]),
  currentItem: PersistedState.getStorage('goodsclass','currentItem')
}

// mutations
const mutations = {
  saveGoodsclassItems (state, items) {
    state.items = items
    PersistedState.setStorage('goodsclass',state)
  },
  clearGoodsclassItems (state) {
    state.items = null
    PersistedState.setStorage('goodsclass',state)
  },
  saveCurrentGoodsclassItem (state, item) {
    state.currentItem = item
    PersistedState.setStorage('goodsclass',state)
  },
  resetCurrentGoodsclassItem (state) {
    if (state.items && state.items.length) {
      state.currentItem = state.items[0]
      PersistedState.setStorage('goodsclass',state)
    }
  }
}

// actions
const actions = {
  fetchGoodsclassList ({ commit, state }) {
    return new Promise((resolve, reject) => {
      getGoodsclassList().then((response) => {
        if (response.result && response.result.class_list && response.result.class_list.length) {
          commit('saveGoodsclassItems', response.result.class_list)
          if (!state.currentItem) {
            commit('saveCurrentGoodsclassItem', response.result.class_list[0])
          }
        }
        resolve(response)
      }, (error) => {
        reject(error)
      })
    })
  }
}

export default {
  state,
  mutations,
  actions
}
