import {
  SET_SALES_VOUCHER_TO_REDEEM,
  SET_SALES_VOUCHER_SELECTED_PRODUCT,
  SET_SALES_VOUCHER_SELECTED_VOUCHER,
  SET_SHOW_EVOUCHER_REDEMPTION_MODAL,
  SET_EVOUCHER_PRODUCT_DUMMY,
  EVOUCHER_CLEAR_PRODUCT_DUMMY,
  CLEAR_LOADED_DATA,
  SET_ORDER_HISTORY_SEARCH_TEXT,
  SET_EK_SMART_ORDER_HISTORY_SEARCH_TEXT,
  SET_SALES_APPLICATION_FORM_LINK,
} from '../../../utils/types'
import {
  SALES_EVOUCHER_SELECTED_DUMMY_IDS,
  ORDER_HISTORY_SEARCH_TEXT,
  EK_SMART_ORDER_HISTORY_SEARCH_TEXT,
} from '../../../utils/localStorageKeys'
import defaultStore from './index'

export default {
  [SET_SALES_APPLICATION_FORM_LINK](state, data) {
    state.applicationFormLink = data.links[0]['download_link']
  },
  [SET_SALES_VOUCHER_TO_REDEEM](state, data) {
    state.salesVoucherToRedeem = data
  },
  [SET_SALES_VOUCHER_SELECTED_PRODUCT](state, data) {
    state.salesVoucherSelectedProduct = data
  },
  [SET_SALES_VOUCHER_SELECTED_VOUCHER](state, data) {
    state.salesVoucherSelectedVoucher = data
  },
  [SET_EVOUCHER_PRODUCT_DUMMY](state, data) {
    state.selectedDummies = data
    localStorage.setItem(SALES_EVOUCHER_SELECTED_DUMMY_IDS, JSON.stringify(data))
  },
  [SET_ORDER_HISTORY_SEARCH_TEXT](state, data) {
    state.searchText = data
    localStorage.setItem(ORDER_HISTORY_SEARCH_TEXT, JSON.stringify(data))
  },
  [SET_EK_SMART_ORDER_HISTORY_SEARCH_TEXT](state, data) {
    state.ekSmartSearchText = data
    localStorage.setItem(EK_SMART_ORDER_HISTORY_SEARCH_TEXT, JSON.stringify(data))
  },
  [EVOUCHER_CLEAR_PRODUCT_DUMMY](state) {
    localStorage.removeItem(SALES_EVOUCHER_SELECTED_DUMMY_IDS)
    state.selectedDummies = defaultStore.state.selectedDummies
  },
  [SET_SHOW_EVOUCHER_REDEMPTION_MODAL](state, data) {
    state.showRedemptionModal = data
  },
  [CLEAR_LOADED_DATA](state, type) {
    if (type === 'order' || !type) {
      state = defaultStore.state
      localStorage.removeItem(SALES_EVOUCHER_SELECTED_DUMMY_IDS)
    }
  },
}
