import {
  ESHOP_PRODUCT_SET_CURRENT_CATEGORY,
  ESHOP_PRODUCT_SET_SALES_TYPES_FILTER_IDS,
  ESHOP_PRODUCT_SET_PRICE_RANGE_FILTER,
  ESHOP_PRODUCT_SET_CV_RANGE_FILTER,
  ESHOP_PRODUCT_SET_SORT_ORDER,
  ESHOP_PRODUCT_SET_SELECTED_SUB_CATEGORY,
  SET_PAGINATION,
  ESHOP_PRODUCT_SET_SEARCH_NAME,
  ESHOP_CART_ORDER_FOR_DOWNLINE,
  ESHOP_CART_SET_ADD_PRODUCT,
} from '../utils/types'
import { RouteHasPath } from '@/helpers/routing'
import { ESHOP, ESHOP_LISTING, ESHOP_ORDER, ESHOP_ORDER_REVIEW } from '@/config/url-prefix'
import router from '@/router'
import _ from 'lodash'

export default store => {
  // Debounce product search to avoid multiple request
  let loadProducts = _.debounce(() => store.dispatch('eshop/initProducts'), 300)
  store.subscribe(async mutation => {
    // load eshop Listing defaults
    if (mutation.type === 'route/ROUTE_CHANGED' && RouteHasPath(mutation.payload.to, ESHOP_LISTING)) {
      if (!store.getters.hasLoaded('categoriesLoading')) {
        await store.dispatch('eshop/loadProductCategories')
      }
      if (!store.getters.hasLoaded('memberDetails')) {
        store.dispatch('loadMemberDetails', store.state.memberModule.member)
      }
      if (!store.getters.hasLoaded('cwInfo')) {
        store.dispatch('eshop/loadCwInfo')
      }
      store.dispatch('eshop/loadCart')
    }
    // set the application status to eshop if inside eshop
    if (mutation.type === 'route/ROUTE_CHANGED') {
      if (RouteHasPath(mutation.payload.to, ESHOP)) {
        store.dispatch('setAppStatus', { eshop: true })
      } else {
        store.dispatch('setAppStatus', { eshop: false })
      }
    }
    // set the application status to eshop if inside eshop
    if (mutation.type === 'route/ROUTE_CHANGED') {
      if (RouteHasPath(mutation.payload.to, ESHOP_ORDER)) {
        store.dispatch('setAppStatus', { eshopOrder: true })
      } else {
        store.dispatch('setAppStatus', { eshopOrder: false })
      }
    }
    // set the application status if inside eshop product detail
    if (mutation.type === 'route/ROUTE_CHANGED') {
      if (mutation.payload.to.name === 'Product Detail') {
        store.dispatch('setAppStatus', { eshopProductDetail: true })
      } else {
        store.dispatch('setAppStatus', { eshopProductDetail: false })
      }
    }
    if (
      [
        // Load products when mutation changes
        'eshop/' + ESHOP_PRODUCT_SET_CURRENT_CATEGORY,
        'eshop/' + ESHOP_PRODUCT_SET_SALES_TYPES_FILTER_IDS,
        'eshop/' + ESHOP_PRODUCT_SET_PRICE_RANGE_FILTER,
        'eshop/' + ESHOP_PRODUCT_SET_CV_RANGE_FILTER,
        'eshop/' + ESHOP_PRODUCT_SET_SORT_ORDER,
        'eshop/' + ESHOP_PRODUCT_SET_SELECTED_SUB_CATEGORY,
        'eshop/' + ESHOP_PRODUCT_SET_SEARCH_NAME,
        mutation.type === SET_PAGINATION && mutation.payload.name === 'products' ? SET_PAGINATION : null,
      ].indexOf(mutation.type) !== -1
    ) {
      let pagination = (await store.getters.getPagination('products')) || { offset: 0, isInitial: true }
      store.getters.afterLoading('categoriesLoading').then(async () => {
        if (store.getters.getAppStatus('eshop')) {
          // Avoid duplicate requests
          if (
            (mutation.type === SET_PAGINATION && pagination.offset === 0 && pagination.isInitial) ||
            (mutation.type === 'eshop/' + ESHOP_PRODUCT_SET_PRICE_RANGE_FILTER && mutation.payload.isInitial) ||
            (mutation.type === 'eshop/' + ESHOP_PRODUCT_SET_CV_RANGE_FILTER && mutation.payload.isInitial)
          ) {
            loadProducts.cancel
          } else {
            loadProducts()
          }

          if (
            mutation.type !== SET_PAGINATION &&
            mutation.type !== 'eshop/' + ESHOP_PRODUCT_SET_SORT_ORDER &&
            !pagination.isInitial &&
            pagination.offset !== 0
          ) {
            router.push({ name: 'ListingHomePaginated', params: { page: 1 } })
          }
        }
      })
    }
    // load cart if cart is not loaded when in eshop
    if (
      mutation.type === 'route/ROUTE_CHANGED' &&
      RouteHasPath(mutation.payload.to, ESHOP) &&
      !store.state.eshop.cart.loaded
    ) {
      store.dispatch('eshop/loadCart')
    }
    // load cart when enters order review page
    if (mutation.type === 'route/ROUTE_CHANGED') {
      if (RouteHasPath(mutation.payload.to, ESHOP_ORDER_REVIEW)) {
        store.dispatch('eshop/loadCart')
      }
    }
    if (mutation.type === 'eshop/' + ESHOP_CART_SET_ADD_PRODUCT) {
      if (store.getters.getAppStatus('eshop') === true) {
        if (store.getters.getAppStatus('eshopProductDetail') === true) {
          store.dispatch('clearShoppingData', ['products'])
        } else {
          store.dispatch('clearShoppingData')
        }
      }
    }
    // load cart if downline changed
    if (mutation.type === 'eshop/' + ESHOP_CART_ORDER_FOR_DOWNLINE) {
      store.dispatch('clearShoppingData')
      store.dispatch('eshop/loadCart')
      if (mutation.payload !== null && RouteHasPath(store.state.route.path, ESHOP_LISTING)) {
        store.getters.afterLoading('categoriesLoading').then(() => {
          loadProducts()
        })
      }
    }
  })
}
