import { RouteHasPath } from '@/helpers/routing'
import { CREATE_AUTOSHIP } from '@/config/url-prefix'
import * as T from '@/store/utils/types'
import router from '@/router'

export default store => {
  store.subscribe(async mutation => {
    if (mutation.type === 'route/ROUTE_CHANGED' && RouteHasPath(mutation.payload.to, CREATE_AUTOSHIP)) {
      store.dispatch('eshop/loadCart', { functionType: 'autoship_sale' })
    }

    if ([T.SET_AUTOSHIP_SEARCH_FILTER].indexOf(mutation.type) !== -1) {
      let pagination = store.getters.getPagination('autoship-products') || { offset: 0 }
      if (pagination.offset !== 0) {
        router.push({ name: 'AutoshipSelectProduct', params: { page: 1 } })
      }
    }
  })
}
