
<template>
  <!-- eslint-disable vue/require-component-is -->
  <component :is="getLinkType(to)" v-bind="linkProps(to)">
    <slot />
  </component>
</template>

<script>
import { isExternal } from '@/utils/validate'

export default {
  props: {
    to: {
      type: String,
      required: true
    },
    reactMode: {
      type: String,
      default: ''
    }
  },
  methods: {
    getLinkType(url) {
      if (this.reactMode === '3') {
        return 'a'
      }
      if (isExternal(url)) {
        return 'a'
      }
      if (this.$router.hasRoute(url.name)) {
        return 'router-link'
      } else {
        return 'span'
      }
    },
    linkProps(url) {
      if (this.reactMode === '3') {
        return {
          href: url,
          target: '_blank',
          rel: 'noopener'
        }
      }
      if (isExternal(url)) {
        return {
          href: url,
          target: '_blank',
          rel: 'noopener'
        }
      }
      return {
        to: url
      }
    }
  }
}
</script>
