import { mapGetters, mapActions } from 'vuex'

function routeHandler() {
  let route = null
  if (this.getVerifyContactStatus.requires) {
    route = { name: 'PreferredContact' }
  } else if (this.getChangePasswordStatus) {
    route = { name: 'ChangePassword' }
  } else if (this.getResetStatus) {
    if (Object.keys(this.getResetResponseData || {}).length > 0) {
      route = { name: 'OTPResetPassword' }
    } else {
      this.activateReset(false)
      route = { name: 'Login' }
    }
  }
  if (route && this.$route.name !== route.name) {
    this.$router.push(route)
  }
}

export default {
  methods: {
    ...mapActions(['activateReset']),
  },
  computed: {
    ...mapGetters(['getResetStatus', 'getResetResponseData', 'getVerifyContactStatus', 'getChangePasswordStatus']),
  },
  watch: {
    getVerifyContactStatus: {
      handler: routeHandler,
      deep: true,
    },
    getChangePasswordStatus: {
      handler: routeHandler,
      deep: true,
    },
    getResetStatus: {
      handler: routeHandler,
      deep: true,
    },
  },
}
