import {
  ESHOP_PRODUCT_SET_CATEGORIES,
  ESHOP_PRODUCT_SET_CURRENT_CATEGORY,
  ESHOP_PRODUCT_SET_SELECTED_SUB_CATEGORY,
  ESHOP_PRODUCT_SET,
  ESHOP_PRODUCT_FREE_SET,
  ESHOP_PRODUCT_SET_SALES_TYPES_FILTER_IDS,
  ESHOP_PRODUCT_SET_IS_EK_SMART,
  ESHOP_PRODUCT_SET_PRICE_RANGE_FILTER,
  ESHOP_PRODUCT_SET_CV_RANGE_FILTER,
  ESHOP_PRODUCT_SET_SORT_ORDER,
  ESHOP_PRODUCT_SET_TOTAL,
  ESHOP_SET_PRODUCT_OR_KITTING_DETAILS,
  CLEAR_LOADED_DATA,
  ESHOP_PRODUCT_SET_SEARCH_NAME,
  ESHOP_PRODUCT_MEMBER_KIT,
  ESHOP_PRODUCT_SET_AVAILABLE_FILTERS,
  SET_IS_RETAIL_PRICE,
} from '../../../utils/types'
import {
  ESHOP_PRODUCT_CATEGORIES,
  ESHOP_PRODUCT_FILTERED_IS_EK_SMART,
  ESHOP_PRODUCT_CURRENT_CATEGORY,
  ESHOP_PRODUCT_SELECTED_SUB_CATEGORIES,
  ESHOP_PRODUCT_FILTERED_SALES_TYPES,
  ESHOP_PRODUCT_FILTERED_PRICE_RANGE,
  ESHOP_PRODUCT_FILTERED_CV_RANGE,
  ESHOP_PRODUCT_SORT_ORDER,
  ESHOP_PRODUCT_SEARCH_NAME,
  ESHOP_PRODUCT_FREE,
  ESHOP_PRODUCT_MEMBER_KIT as ESHOP_PRODUCT_MEMBER_KIT_STORE,
  IS_RETAIL,
} from '../../../utils/localStorageKeys'
import _ from 'lodash'

export default {
  [ESHOP_PRODUCT_SET_CATEGORIES](state, data) {
    state.categories = data
    localStorage.setItem(ESHOP_PRODUCT_CATEGORIES, JSON.stringify(state.categories))
  },
  [ESHOP_PRODUCT_SET_CURRENT_CATEGORY](state, data) {
    state.currentCategory = data
    state.selectedSubCategories = []
    localStorage.setItem(ESHOP_PRODUCT_CURRENT_CATEGORY, JSON.stringify(state.currentCategory))
  },
  [ESHOP_PRODUCT_SET_SELECTED_SUB_CATEGORY](state, data) {
    state.selectedSubCategories = data
    localStorage.setItem(ESHOP_PRODUCT_SELECTED_SUB_CATEGORIES, JSON.stringify(state.selectedSubCategories))
  },
  [ESHOP_PRODUCT_SET](state, data) {
    state.products = data.map(product => {
      product.free = false
      return product
    })
  },
  [ESHOP_PRODUCT_FREE_SET](state, data) {
    state.freeProducts = data.map(product => {
      if (product.image === null || product.image === '') {
        product.image = '/static/products/img-placeholder.svg'
      }
      product.free = true
      return product
    })
    localStorage.setItem(
      ESHOP_PRODUCT_FREE,
      JSON.stringify(_.map(state.freeProducts, item => _.pick(item, ['product_id', 'kitting_id', 'free'])))
    )
  },
  [ESHOP_PRODUCT_MEMBER_KIT](state, data) {
    state.memberKit = data.map(product => {
      if (product.image === null || product.image === '') {
        product.image = '/static/products/img-placeholder.svg'
      }
      product.member_kit = true
      return product
    })
    localStorage.setItem(
      ESHOP_PRODUCT_MEMBER_KIT_STORE,
      JSON.stringify(_.map(state.memberKit, item => _.pick(item, ['product_id', 'kitting_id', 'member_kit'])))
    )
  },
  [ESHOP_PRODUCT_SET_IS_EK_SMART](state, isEKsmart) {
    state.filterIsEkSmart = isEKsmart
    localStorage.setItem(ESHOP_PRODUCT_FILTERED_IS_EK_SMART, JSON.stringify(state.filterIsEkSmart))
  },
  [ESHOP_PRODUCT_SET_SALES_TYPES_FILTER_IDS](state, salesTypeIds) {
    state.filterSalesTypeIds = salesTypeIds || []
    localStorage.setItem(ESHOP_PRODUCT_FILTERED_SALES_TYPES, JSON.stringify(state.filterSalesTypeIds))
  },
  [ESHOP_PRODUCT_SET_PRICE_RANGE_FILTER](state, payload) {
    state.filterPriceRange = payload.value
    localStorage.setItem(ESHOP_PRODUCT_FILTERED_PRICE_RANGE, JSON.stringify(state.filterPriceRange))
  },
  [ESHOP_PRODUCT_SET_CV_RANGE_FILTER](state, payload) {
    state.filterCvRange = payload.value
    localStorage.setItem(ESHOP_PRODUCT_FILTERED_CV_RANGE, JSON.stringify(state.filterCvRange))
  },
  [ESHOP_PRODUCT_SET_SORT_ORDER](state, sortOrder) {
    state.sortOrder = sortOrder
    localStorage.setItem(ESHOP_PRODUCT_SORT_ORDER, JSON.stringify(state.sortOrder))
  },
  [ESHOP_PRODUCT_SET_TOTAL](state, total) {
    state.totalProducts = total
  },
  [ESHOP_SET_PRODUCT_OR_KITTING_DETAILS](state, payload) {
    state.productDetails.push(payload)
  },
  [ESHOP_PRODUCT_SET_SEARCH_NAME](state, payload) {
    state.searchName = payload.trim()
    localStorage.setItem(ESHOP_PRODUCT_SEARCH_NAME, JSON.stringify(state.searchName))
  },
  [CLEAR_LOADED_DATA](state, type) {
    if (type === 'products' || !type) {
      state.productDetails = []
      state.searchName = ''
      localStorage.removeItem(ESHOP_PRODUCT_SEARCH_NAME)
      localStorage.removeItem(ESHOP_PRODUCT_FILTERED_PRICE_RANGE)
      localStorage.removeItem(ESHOP_PRODUCT_FILTERED_CV_RANGE)
      state.filterPriceRange = [0, 0]
      state.filterCvRange = [0, 0]
    }
    if (type === 'products-load' || !type) {
      state.products = []
    }
  },
  [ESHOP_PRODUCT_SET_AVAILABLE_FILTERS](state, payload) {
    state.availableFilters = { ...payload }
  },
  [SET_IS_RETAIL_PRICE](state, status) {
    state.isRetailPrice = status
    sessionStorage.setItem(IS_RETAIL, JSON.stringify(status))
  },
}
