import * as T from '../utils/types'
import * as U from '@/config/url-prefix'
import router from '@/router'

export default store => {
  store.subscribe(async mutation => {
    if (
      [
        U.ESAC_VOUCHER_LIST,
        T.SET_ESAC_VOUCHER_LIST_SEARCH_TEXT,
        mutation.type === T.SET_PAGINATION && mutation.payload.name === 'esacvoucherlist' ? T.SET_PAGINATION : null,
      ].indexOf(mutation.type) !== -1
    ) {
      let pagination = store.getters.getPagination('esacvoucherlist') || { offset: 0 }
      if (mutation.type !== T.SET_PAGINATION && pagination.offset !== 0) {
        router.push(U.ESAC_VOUCHER_LIST)
      }
      store.dispatch('loadEsacVoucherList')
    }
  })
}
