import * as T from '../../utils/types'
import * as L from '../../utils/localStorageKeys'
import { getValue } from '@/helpers/variable-handling'

export default {
  [T.MEMBER_SET_GENDER](state, data) {
    state.memberGender = data
  },
  [T.MEMBER_SET_DETAILS](state, data) {
    state.member = Object.assign(state.member, data)
    localStorage.setItem(L.MEMBER_INFO, JSON.stringify(state.member))
  },
  [T.MEMBER_SET_SUBSCRIPTION](state, data) {
    if (data) {
      state.subscription.expiryDate = new Date(data.expiry_date)
      state.subscription.status = state.subscription.expiryDate > new Date()
    } else {
      state.subscription.status = false
      state.subscription.hasSubscripbedBefore = false
    }
  },
  [T.MEMBER_SET_INFORMATION](state, data) {
    state.memberInfo = Object.assign(state.memberInfo, data)
    localStorage.setItem(L.MEMBER_INFORMATION, JSON.stringify(state.memberInfo))
  },
  [T.MEMBER_SET_RANK_LIST](state, data) {
    data.forEach(function(rank) {
      rank.sales_types = JSON.parse(rank.sales_types)
    })
    data.sort((a, b) => {
      return a.entitlement_lvl - b.entitlement_lvl
    })
    state.enrollmentRankList = data
    if (Array.isArray(state.enrollmentRankList) && typeof state.enrollmentRankList[0].level !== 'undefined') {
      state.enrollmentRankList = state.enrollmentRankList.sort(function(a, b) {
        return parseFloat(a.level) - parseFloat(b.level)
      })
    }
    localStorage.setItem(L.MEMBER_RANKS_LIST, JSON.stringify(state.enrollmentRankList))
  },
  [T.MEMBER_SET_IBO_DETAILS](state, data) {
    state.iboDetails = data
    localStorage.setItem(L.MEMBER_IBO_DETAILS, JSON.stringify(state.iboDetails))
  },
  [T.MEMBER_SET_MEMBER_LIST](state, data) {
    state.memberList = data
    localStorage.setItem(L.MEMBER_LIST, JSON.stringify(state.memberList))
  },
  [T.MEMBER_SET_SELECTED_DETAILS](state, data) {
    state.selectedMemberDetails = data
  },
  [T.MEMBER_SET_AVATAR](state, data) {
    state.member.avatar_image_link = data
    localStorage.setItem(L.MEMBER_AVATAR, data)
  },
  [T.MEMBER_SET_DASHBOARD_DETAIL](state, data) {
    state.memberDashboardDetail = data
    state.iwbStatus = data.iwb_status
    state.builder_status = data.builder
    state.updatedAtTime = data.updated_at
    state.calculating = data.calculating
    state.totalBvGraphDetail = data.total_tvc_graph
    state.goalRankDetail = {
      rank: data.rank,
      pv: data.tvc,
      highest_rank: getValue(data, 'translated_highest_rank.content', data.highest_rank),
    }
  },
  [T.MEMBER_SET_TVC_RANK_INFO](state, data) {
    state.tvcRankInfo = data
  },
  [T.MEMBER_SET_MEMBER_FOUNDER](state, data) {
    state.founderBonus = data || null
  },
}
