import * as T from '../../utils/types'
import * as L from '@/store/utils/localStorageKeys'
import _ from 'lodash'

export default {
  [T.ADD_ERROR](state, error) {
    // Special case we need this to show multiple error at once
    // TODO need enhancement apply proper logic to get multiple messages at once
    // TODO apply same logic to remove and reset error mutation
    if (
      state.errors[error.name] !== undefined &&
      state.errors[error.name] !== false &&
      state.errors[error.name] !== '' &&
      error.message !== false &&
      error.name === 'cartError'
    ) {
      let isExist = state.errors[error.name].includes(error.message)
      if (!isExist) {
        state.errors[error.name] = state.errors[error.name] + ' & ' + error.message
      }
    } else {
      state.errors = { ...state.errors, [error.name]: error.message }
    }
  },
  [T.REMOVE_ERROR](state, name) {
    state.errors[name] = false
  },
  [T.RESET_ERROR](state, name) {
    this._vm.$delete(state.errors, name)
  },
  [T.NOTICE_ADD](state, notice) {
    // if no id is specified only add unique notices
    let omit = object => _.omit(object, ['id'])
    if (!notice.id && !_.find(_.map(state.notices, omit), omit(notice))) {
      notice.id = Date.now()
    }
    if (notice.id) {
      state.notices.push(notice)
    }
    localStorage.setItem(L.NOTICESS_RECIVED, JSON.stringify(state.notices))
  },
  [T.NOTICE_READ](state, id) {
    state.noticesRead.push(id)
    localStorage.setItem(L.NOTICESS_READ, JSON.stringify(state.noticesRead))
  },
  [T.RESET_ERROR_ALL](state) {
    state.errors = []
  },
  [T.NOTICE_SHOW](state, status) {
    state.showNotice = status
  },
  [T.ISP_POPUP_SHOW](state, status) {
    state.showiSPPopup = status
  },
  [T.IS_ANNOUNCEMENT_SEEN](state, payload) {
    state.isAnnouncementSeen = payload
  },
}
