<template>
  <component :is="type" v-bind="linkProps(to)">
    <slot />
  </component>
</template>

<script>

export default {
  name:"AppLink",
  props: {
    to: {
      type: Object,
      required: true,
    },
  },
  computed: {
    isExternal() {
      return /^(https?:|mailto:|tel:)/.test(this.to.path) ;
    },
    type() {
      if (this.isExternal) {
        return "a";
      }
      return "nuxt-link";
    },
  },
  methods: {
    linkProps(item) {
      if (this.isExternal) {
        return {
          href: item.path,
          target: "_blank",
          rel: "noopener",
        };
      }
      let toRoute =  {
        to : item.path,
        meta: {
          title: item.title,
        },
      }

      return toRoute;
    },
  },
};
</script>