import {
  SET_COUNTRIES_LOCATIONS,
  SET_COUNTRIES_SHIPPING,
  SET_CURRENT_COUNTRY,
  SET_CURRENT_LANGUAGE,
  SET_ALL_LANGUAGES,
  SET_LANGUAGES,
  SET_STATES_LOCATIONS,
  SET_USER_COUNTRY,
  UNSET_COUNTRY,
  SET_DYNAMIC_CONTENT,
  SET_STATES_LOCATIONS_BY_COUNTRY,
  SET_STATES_LOCATIONS_BY_STATE,
  SET_SHIPPING_DATA,
  SET_STOCKIST_STATE_AND_CITIES,
  SET_LOCATION_TYPE_LIST,
  SET_LOCATION_NATIONALITY,
  SET_ALL_COUNTRIES_OPTION,
  SET_COUNTRY_RELATION_DATA,
  SET_AUTOSHIP_COUNTRY,
  UNSET_AUTOSHIP_COUNTRY,
  SET_IP_COUNTRY,
  SET_CURRENT_IP,
} from '../../utils/types'
import {
  COUNTRY_LIST,
  SELECTED_COUNTRY,
  USER_COUNTRY,
  ENROL_ADDRESS,
  LANGUAGE_LIST,
  SELECTED_LANGUAGE,
  SELECTED_AUTOSHIP_COUNTRY,
  IP_COUNTRY,
  CURRENT_IP,
} from '../../utils/localStorageKeys'
import Vue from 'vue'
import helpers from '@/helpers/index'

export default {
  [SET_COUNTRIES_LOCATIONS](state, data) {
    state.countries = data
    localStorage.setItem(COUNTRY_LIST, JSON.stringify(state.countries))
  },
  [SET_STATES_LOCATIONS](state, data) {
    state.states = data
  },
  [SET_USER_COUNTRY](state, data) {
    state.countries.forEach(country => {
      if (country.id === data) {
        state.userCountry = country
      }
    })
    localStorage.setItem(USER_COUNTRY, JSON.stringify(state.userCountry))
  },
  [SET_CURRENT_COUNTRY](state, data) {
    if (!data) {
      state.selectedCountry = helpers.getValue(state.countries, '[0]', {})
    } else {
      state.countries.forEach(country => {
        if (country.id === data) {
          state.selectedCountry = country
        }
      })
    }
    if (Object.keys(state.selectedCountry).length > 0) {
      localStorage.setItem(SELECTED_COUNTRY, JSON.stringify(state.selectedCountry))
    }
  },
  [UNSET_COUNTRY](state) {
    state.selectedCountry = null
    state.selectedLanguage = null
    state.userCountry = null
    localStorage.removeItem(USER_COUNTRY)
    localStorage.removeItem(SELECTED_COUNTRY)
    localStorage.removeItem(SELECTED_LANGUAGE)
  },
  [SET_CURRENT_LANGUAGE](state, data) {
    data = !isNaN(data) ? data : 1

    state.selectedLanguage = state.languagesAll.find(language => language.id === data)
    if (helpers.getValue(state, 'selectedLanguage.key')) {
      localStorage.setItem(SELECTED_LANGUAGE, JSON.stringify(state.selectedLanguage))
      Vue.i18n.set(state.selectedLanguage.key.toLowerCase())
    }
  },
  [SET_LANGUAGES](state, data) {
    state.languages = data.reverse()
  },
  [SET_ALL_LANGUAGES](state, data) {
    state.languagesAll = data
    localStorage.setItem(LANGUAGE_LIST, JSON.stringify(state.languagesAll))
  },
  [SET_DYNAMIC_CONTENT](state, payload) {
    Vue.set(state.dynamicContentData, 'data', payload.data)
    localStorage.setItem(ENROL_ADDRESS, JSON.stringify(payload.data.address))
  },
  [SET_STATES_LOCATIONS_BY_COUNTRY](state, payload) {
    Vue.set(state.statesByCountriesData, 'data', payload)
  },
  [SET_STATES_LOCATIONS_BY_STATE](state, payload) {
    Vue.set(state.citiesByStateData, 'data', payload)
  },
  [SET_COUNTRIES_SHIPPING](state, data) {
    state.shippingCountries = data
  },
  [SET_SHIPPING_DATA](state, data) {
    state.shippingData = data
  },
  [SET_STOCKIST_STATE_AND_CITIES](state, payload) {
    state.stockistStateAndCities[payload.country_id] = payload.data
  },
  [SET_LOCATION_TYPE_LIST](state, data) {
    state.locationTypeList = data
  },
  [SET_LOCATION_NATIONALITY](state, data) {
    state.nationality = data
  },
  [SET_ALL_COUNTRIES_OPTION](state, data) {
    // state.allCountries = data.map((country) => {
    //   return {
    //     text: country.name,
    //     value: country.id
    //   }
    // })
    state.allCountries = data
  },
  [SET_COUNTRY_RELATION_DATA](state, payload) {
    state.countryRelation.data = { ...state.countryRelation.data, ...payload.data }
  },
  [SET_AUTOSHIP_COUNTRY](state, payload) {
    state.autoshipCountry = payload
    sessionStorage.setItem(SELECTED_AUTOSHIP_COUNTRY, payload)
  },
  [UNSET_AUTOSHIP_COUNTRY](state) {
    state.autoshipCountry = false
    sessionStorage.removeItem(SELECTED_AUTOSHIP_COUNTRY)
  },
  [SET_IP_COUNTRY](state, data) {
    state.ipCountry = {
      ...state.ipCountry,
      [data.ip]: {
        countryCode: data.countryCode,
        neighbours: data.countryNeighbours,
      },
    }
    localStorage.setItem(IP_COUNTRY, JSON.stringify(state.ipCountry))
  },
  [SET_CURRENT_IP](state, data) {
    state.currentIp = data.toString()
    localStorage.setItem(CURRENT_IP, data.toString())
  },
}
