<template>
  <a v-if="reg.isExternal(to)" :href="to" target="_blank" rel="noopener">
    <slot></slot>
  </a>
  <div v-else @click="onPush">
    <slot></slot>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { useRouter } from 'vue-router'
import { reg } from 'lz-utils-lib'
import useStore from '@/stores'

const { app } = useStore()

const sidebar = computed(() => app.sidebar)
const device = computed(() => app.device)

const props = defineProps({
  to: {
    type: String,
    required: true
  }
})

const router = useRouter()
const onPush = () => {
  if (device.value === 'mobile' && sidebar.value.opened == true) {
    app.closeSideBar(false)
  }
  router.push(props.to).catch((err) => {
    console.log(err)
  })
}
</script>
