import helpers from '@/helpers'
import constants from '@/config/const'
import _ from 'lodash'
export default {
  getMemberDetails: state => {
    return state.member
  },
  getMemberFounderBonus: state => state.founderBonus,
  getMemberInformation: state => {
    return state.memberInfo
  },
  getMemberCountryName: state => {
    return helpers.getValue(state, 'member.country.name')
  },
  getMemberCountryId: state => {
    return helpers.getValue(state, 'member.country_id')
  },
  getMemberName: state => {
    return state.member.name
  },
  getMemberEnrollmentRank: (state, rootGetters) => {
    let enrolmentRank = {}
    state.enrollmentRankList.forEach(function(enrolment) {
      let memberRankId = state.member.enrollment_rank_id
      if (rootGetters.getAppStatus('eshop') === true && rootGetters['eshop/getIsOrderingForDownLine'] === true) {
        memberRankId = rootGetters['eshop/getOrderingFor'].details.enrollment_rank_id
      }
      if (enrolment.id === memberRankId) {
        enrolmentRank = enrolment
      }
    })
    if (rootGetters['getMemberEnrolmentTerminated'] === true) {
      enrolmentRank = _.cloneDeep(enrolmentRank)
      enrolmentRank.sales_types = {
        key: constants.masterKey.SALE_TYPES,
        values: [constants.masterData.REGISTRATION, constants.masterData.FORMATION],
      }
    }
    return enrolmentRank
  },
  getEligibleMemberEnrollmentRank: (state, rootGetters) => typeId => {
    let enrolmentRank = {}
    state.enrollmentRankList.forEach(function(enrolment) {
      if (rootGetters['enrolment/getEnrolmentTypes'].length > 0) {
        if (enrolment.id === rootGetters['enrolment/getEnrolmentTypes'].find(x => x.id === typeId).id) {
          enrolmentRank = enrolment
        }
      }
    })
    return enrolmentRank
  },
  getMemberEnrollmentRankCW: () => {
    return 30
  },
  getMemberEnrollmentRankList: state => {
    return state.enrollmentRankList
  },
  getMemberGender: state => {
    return state.memberGender
  },
  getMemberIboDetails: state => {
    return state.iboDetails
  },
  getMemberList: state => {
    return state.memberList
  },
  getIboDetails: state => weekName => {
    let weekList = state.cwScheduleList.find(function(week) {
      if (week.cw_name === weekName) {
        return week
      }
    })
    return weekList
  },
  getIboId: state => {
    let iboId = ''
    if (state.member !== undefined && state.member.user !== undefined) {
      let countryCode =
        state.member.country !== undefined && state.member.country.code !== undefined ? state.member.country.code : ''
      iboId = countryCode + (state.member.member_id || state.member.user.old_ibs_user_id) // .toString().padStart(11, '0')
    }
    return iboId
  },
  getIboIdPlan: (state, getters) => {
    if (getters.getAppStatus('eshop') === true) {
      return helpers.getValue(state, 'member.member_id') || helpers.getValue(state, 'member.user.old_ibs_user_id')
    } else if (getters.getAppStatus('enrolment') === true) {
      return helpers.getValue(getters['enrolment/getSponsorDetails'], 'member_id')
    }
  },
  getUserName: state => {
    if (state.member !== undefined && state.member.user !== undefined) {
      return state.member.user.name
    }
  },
  getSelectedMemberDetails: state => {
    return state.selectedMemberDetails
  },
  getMemberEnrolmentTerminated: state => {
    return state.member.is_enrollment_terminate !== undefined && state.member.is_enrollment_terminate === 1
  },
  getSubscriptionLinkByCountry: state => countryId => {
    return state.subscription.learnMoreLinks.find(link => link.countryId === countryId)
  },

  // Dashboard getters
  getMemberDashboardDetail: state => state.memberDashboardDetail,
  getIwbStatus: state => state.iwbStatus,
  getGcvGraphDetail: state => state.totalBvGraphDetail,
  getGoalRankDetail: state => state.goalRankDetail,
  getTvcRankInfo: state => state.tvcRankInfo,
  getDashboardUpdatedAt: state => state.updatedAtTime,
  getBuilderStatus: state => state.builder_status,
  getCalculating: state => state.calculating,
}
