import _ from 'lodash'

export default {
  getErrors: state => name => {
    return state.errors[name]
  },
  getErrorsBreakup: state => name => {
    let result = []
    if (state.errors[name] !== undefined) {
      if (Array.isArray(state.errors[name])) {
        result = state.errors[name]
      } else {
        Object.entries(state.errors[name]).forEach(([key, value]) => {
          let item = Array.isArray(value) ? value[0] : value.message !== undefined ? value.message : value
          result.push(item)
        })
      }
    }
    return result
  },
  getErrorsState: state => name => {
    let status = state.errors[name]
    if (status === undefined) {
      return true
    }
    return false
  },
  getUnreadNotices: (state, getters) => {
    let location = getters.getAppLocationCountry
    if (location) {
      return _.uniq(
        state.notices.filter(n => {
          return (
            // Check if notice country is equal to current country if defined
            (n.countryId === undefined || n.countryId === location.countryId) &&
            // Check if not is already read
            !state.noticesRead.includes(n.id) &&
            // Check if notice is already expired if defined
            ((typeof n.endTime !== undefined && n.endTime !== null && n.endTime !== ''
              ? Date.parse(n.endTime) > new Date()
              : true) ||
              n.endTime === undefined) &&
            // Check if notice is has started if defined
            ((typeof n.startTime !== undefined && n.startTime !== null && n.startTime !== ''
              ? new Date() > Date.parse(n.startTime)
              : true) ||
              n.startTime === undefined)
          )
        })
      )
    } else {
      return []
    }
  },
  getShowiSPPopup: (state, getters, rootState) => {
    return rootState.memberModule.subscription.status === false ? state.showiSPPopup : false
  },
  getAnnouncementSeenStatus: state => state.isAnnouncementSeen,
}
