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

export default {
  [T.SET_ESAC_ROUTER_DATA](state, data) {
    state.esacRouterData = data
  },
  [T.SET_ESAC_VOUCHER_DATA](state, data) {
    state.esacRedemptionData.voucherData = data
  },
  [T.SET_ESAC_PRODUCT_CATEGORY_DATA](state, data) {
    state.esacRedemptionData.productCategoryData = data
  },
  [T.SET_ESAC_PRODUCT_DATA](state, data) {
    let productTypeMap = new Map([
      ['P', { id: 'product_id', code: 'sku' }],
      ['K', { id: 'kitting_id', code: 'code' }],
    ])
    state.esacRedemptionData.productData = _.map(data, product => {
      let typeMap = productTypeMap.get(product.item_type)
      if (typeMap) {
        product[typeMap['id']] = product.item_id
        product.code = product[typeMap['code']]
      }
      return product
    })
  },
  [T.SET_ESAC_PRODUCT_SEARCH_VISIBLE](state, data) {
    state.esacRedemptionData.productSearchVisible = data
  },
  // [SET_ESAC_SHIPPING_DATA] (state, data) {
  //   state.esacRedemptionData.shippingData = data
  // },
  [T.SET_ESAC_SALE_CREATION_DATA](state, data) {
    state.esacRedemptionData.saleCreationData = data
  },
  [T.SET_ESAC_SALE_DATA](state, data) {
    state.esacRedemptionData.saleData = data
  },
  [T.SET_ESAC_PAYMENT_DATA](state, data) {
    state.esacRedemptionData.paymentData = data
  },
  [T.SET_ESAC_CW_SCHEDULES_DATA](state, data) {
    state.esacCwScheduleData = data
  },
  [T.SET_ESAC_ACTIVE_COUNTRY_DATA](state, data) {
    state.esacActiveCountryData = data
  },
  [T.SET_ESAC_COUNTRY_RELATION_DATA](state, data) {
    let transactionLocationId = null
    if (typeof data.entity !== 'undefined' && data.entity !== null) {
      if (typeof data.entity.locations !== 'undefined' && data.entity.locations !== null) {
        let transactionLocation = data.entity.locations.find(item => item.location_types_id === 4) // 4 = Online
        if (typeof transactionLocation !== 'undefined' && transactionLocation !== null) {
          transactionLocationId = transactionLocation.id
        }
      }
    }
    state.esacTransactionLocationId = transactionLocationId
    state.esacCountryRelationData = data
  },
  [T.SET_ESAC_STOCK_LOCATION_DATA](state, data) {
    state.esacStockLocationId = data
  },
  [T.SET_ESAC_ADDRESS_FORMAT_DATA](state, data) {
    state.esacAddressFormatData = data
  },
  [T.SET_ESAC_MEMBER_DATA](state, data) {
    state.esacMemberData = data
  },
  [T.SET_ESAC_STATE_CITY_DATA](state, data) {
    state.esacStateCityData = data
  },
  [T.SET_ESAC_LOCATION_ADDRESS_DATA](state, data) {
    state.esacLocationAddressData = data
  },
  [T.SET_ESAC_PAYMENT_MODE_SETTING_DATA](state, data) {
    state.esacPaymentModeSettingData = data
  },
  [T.SET_ESAC_DOWNLOAD_INVOICE_DATA](state, data) {
    state.esacDownloadInvoiceData = data
  },
  [T.SET_ESAC_PAYMENT_SALE_ID](state, data) {
    state.esacRedemptionData.paymentSaleId = data
  },
  [T.SET_ESAC_COMPLETED_SALE_ID](state, data) {
    state.esacRedemptionData.completedSaleId = data
  },
  [T.SET_ESAC_VOUCHER_LIST_TOTAL_ROWS](state, data) {
    state.esacVoucherListTotalRows = data
  },
  [T.SET_ESAC_VOUCHER_LIST_SEARCH_TEXT](state, data) {
    state.esacVoucherListSearchText = data
    localStorage.setItem(L.ESAC_VOUCHER_SEARCH_TEXT, data)
  },
}
