
import { reactive, ref, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { storeToRefs } from 'pinia'
import useSystemStore from '@/stores/useSystemStore'



export interface ITextLink {
  text: string;
  id?: string | number;
  path?: string;
  url?: string;
  textColor?: string;
}


function useLinkList() {
  const textLinkList = reactive<Array<ITextLink>>([
    { text: '常见问题', path: '/faq' },
    {
      text: '与 VAVABUY 合作',
      path: '/article/article_details',
      id: 'connect',
    },
  ])

  const textLinkList2 = reactive<Array<ITextLink>>([
    {
      text: '开始在 VAVABUY 购物',
      url: 'https://vavabuy.net/#/',
      textColor: '#FF0F23'
    },
    {
      text: '如何运作',
      path: '/article/article_details',
      id: 'works',
    },
    {
      text: '为什么选择 VAVABUY',
      path: '/article/article_details',
      id: 'about',
    },
    {
      text: '优势',
      path: '/article/article_details',
      id: 'superiority',
    },
  ])

  const route = useRoute()
  const router = useRouter()
  const currentPath = ref<string>('')
  const currentId = ref<string | number | null>(null)


  // 获取最新路由信息（路径&id）
  watch(
    () => route.fullPath,
    (newVal, oldVal) => {
      currentPath.value = route.path
      currentId.value = !!route.query.id ? (route.query.id as (string | number)) : null
    },
    { immediate: true }
  )


  // 跳转回调
  const handleTextLinkClick = function (linkInfo: ITextLink, id?: string | number) {

    if (!linkInfo || (!linkInfo.path && !linkInfo.url)) return

    if (linkInfo.url) {
      const systemStore = useSystemStore()
      const { locale } = storeToRefs(systemStore)
      window.open(`${linkInfo.url}?lang=${locale.value}`, '_blank')
    }
    else if (linkInfo.id) {
      router.push({
        path: linkInfo.path,
        query: {
          id: linkInfo.id ? linkInfo.id : '',
          type: 'header'
        }
      })
    }
    else {
      router.push({ path: linkInfo.path })
    }
  }



  return {
    textLinkList,
    textLinkList2,
    route,
    router,
    currentPath,
    currentId,
    handleTextLinkClick
  }
}


export default useLinkList