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

<script>
import { isExternal } from '@/utils/validate'
import CryptoJS from 'crypto-js'

export default {
  props: {
    to: {
      type: String,
      required: true
    }
  },
  methods: {
    linkProps(url) {
      // 双随机的，直接加密username，直接淦
      if (url.indexOf('/market/random') !== -1) {
        const username = this.$store.state.user.username
        const token = CryptoJS.AES.encrypt(username, CryptoJS.enc.Latin1.parse('litianyang123456'), {
          iv: CryptoJS.enc.Latin1.parse('litianyang123456'),
          mode: CryptoJS.mode.CBC,
          padding: CryptoJS.pad.ZeroPadding
        }).toString()
        return {
          is: 'a',
          href: process.env.VUE_APP_RANDOM_URL + '?token=' + token,
          target: '_blank',
          rel: 'noopener'
        }
      } else if (isExternal(url)) {
        return {
          is: 'a',
          href: url,
          target: '_blank',
          rel: 'noopener'
        }
      }
      return {
        is: 'router-link',
        to: url
      }
    }
  }
}
</script>
