<template>
  <login-form
    disable-language-selection
    v-if="!isLoggedIn"
    namespace="3rdParty-eshop"
    @callback="doBypassLoginSuccessRoute"
  />
</template>

<script>
import LoginForm from '@/app/login/landing/components/LoginForm'
import { mapGetters, mapActions } from 'vuex'
import { customRoutingMixin } from '@@/app/_global/mixins'
import { IS_LOGGED_IN_GETTER_NAME } from '@/config/defaults'

export default {
  name: 'Login',
  components: { LoginForm },
  mixins: [customRoutingMixin],
  watch: {
    isLoggedIn: {
      handler(to) {
        if (to) {
          this.doBypassLoginSuccessRoute()
        }
      },
      immediate: true,
    },
  },
  computed: {
    ...mapGetters({
      isLoggedIn: [IS_LOGGED_IN_GETTER_NAME],
    }),
  },
  methods: {
    ...mapActions(['logoutGuest']),
    doBypassLoginSuccessRoute() {
      if (this.isLoggedIn) {
        let flag = false
        let uri = this.$route.query.redirect

        if (uri) {
          try {
            window.location = decodeURIComponent(uri)
            flag = true
            // eslint-disable-next-line no-empty
          } catch {}
        }

        if (!flag) {
          this.doCustomRoute()
        }
      }
    },
  },
}
</script>
