import * as T from '../../utils/types'
import * as L from '../../utils/localStorageKeys'
import _ from 'lodash'
import helpers from '@/helpers'

export default {
  [T.SALES_SET_PAYMENT_MODES](state, payload) {
    state.paymentModes = payload
  },
  [T.SALES_SET_SALE](state, payload) {
    state.sale = payload
    state.currentSaleId = payload.sale_id
    localStorage.setItem(L.SALES_CURRENT_SALE_ID, JSON.stringify(state.currentSaleId))
    if (payload.selected.shipping !== null && typeof payload.selected.shipping.recipient_addresses !== 'undefined') {
      state.sale.selected.shipping.recipient_addresses = JSON.parse(payload.selected.shipping.recipient_addresses)
    }
    if (typeof payload.self_collection_point !== 'undefined' && payload.self_collection_point !== null) {
      state.sale.self_collection_point.address_data = JSON.parse(payload.self_collection_point.address_data)
    }
    state.onHoldPaymentMode = {
      is_paylater: helpers.getValue(payload, 'is_paylater') === 1,
      is_cash_on_delivery: helpers.getValue(payload, 'is_cash_on_delivery') === 1,
    }
    if (typeof payload.evouchers !== 'undefined') {
      state.eVouchers = _.chain(payload.evouchers)
        .map(evoucher => {
          evoucher.product = _.find(
            payload.products,
            product => helpers.getValue(product, 'evoucher.id') === evoucher.id
          )
          evoucher.kitting = _.find(
            payload.kittings,
            kitting => helpers.getValue(kitting, 'evoucher.id') === evoucher.id
          )
          evoucher.name =
            (evoucher.product && evoucher.product.name) || (evoucher.kitting && evoucher.kitting.name) || ''
          return evoucher
        })
        .value()
    }
  },
  [T.CLEAR_LOADED_DATA](state, type) {
    if (type === 'sales' || !type) {
      state.sale = {}
      state.eVouchers = []
      state.currentPaymentIsQr = false
      state.currentSaleId = ''
      localStorage.removeItem(L.SALES_CURRENT_SALE_ID)
    }
  },
  [T.SALES_SET_CURRENT_PAYMENT](state, payments) {
    state.currentPayment = payments
    if (payments.image !== undefined) {
      state.qrCodeData = payments
      state.currentPaymentIsQr = true
      state.currentPaymentId = 0
    } else {
      state.currentPaymentIsQr = false
      state.currentPayment = payments
      state.currentPaymentId = payments.payment.id
      localStorage.setItem(L.SALES_CURRENT_PAYMENT_ID, JSON.stringify(state.currentPaymentId))
    }
  },
  [T.SET_SALES_LIST](state, data) {
    state.salesList = data
  },
  [T.SET_EK_SMART_SALES_LIST](state, data) {
    state.ekSmartSalesList = data
  },
  [T.SET_SALES_LIST_TOTAL_ROWS](state, data) {
    state.salesListTotalRows = data
  },
  [T.SET_EK_SMART_SALES_LIST_TOTAL_ROWS](state, data) {
    state.ekSmartSalesListTotalRows = data
  },
  [T.SET_SALES_LIST_DATA](state, data) {
    state.salesListData = data
  },
  [T.SET_SALES_LIST_DATA_TOTAL_ROWS](state, data) {
    state.salesListDataTotalRows = data
  },
  [T.SET_SALES_LIST_TOTAL_ROWS](state, data) {
    state.salesListTotalRows = data
  },
  [T.SET_SALES_CREDIT_NOTE_PDF](state, data) {
    state.creditNotePdf = data
  },
  [T.SET_SALES_PAYMENT_WILL_PEND](state, saleId) {
    state.saleInPreorder = saleId
  },
  [T.SET_EVOUCHER_BY_SALE](state, data) {
    state.saleEvouchers = data
  },
  [T.SALES_SET_SALE_ID](state, data) {
    state.currentSaleId = data
    localStorage.setItem(L.SALES_CURRENT_SALE_ID, JSON.stringify(state.currentSaleId))
  },
  [T.SET_REDEMPTION_RECEIPT](state, data) {
    state.redemptionReceipt = data
  },
  [T.SALES_SET_SUBSCRIPTION_SALE](state, data) {
    state.subscriptionSale = data.sales_data
    state.currentSaleId = data.sales_data.sale_id
  },
  [T.SALES_SET_ON_HOLD_PAYMENT_MODE](state, data) {
    state.onHoldPaymentMode = data
  },
  [T.SALES_SET_SHOW_LOADING_POPUP](state, data) {
    state.showLoadingPopup = data
  },
  [T.SALES_SET_PAYMENT_QRCODE](state, data) {
    state.qrCodeData = data
  },
  [T.SET_PAYMENT_STATUS](state, payload) {
    state.paymentStatus = payload
  },
}
