import { useRef, useEffect } from 'react'
import { getIndexShare } from '@/services/user'
import { getImgURL } from '@/utils/image'

export default function useIndexShareContent(miniApp, paramsRef, router) {
  const shareImg = useRef()
  const shareTitle = useRef()

  useEffect(() => {
    shareTitle.current = `欢迎访问 ${miniApp.name}`

    // 获取首页分享信息
    getIndexShare().then((res) => {
      const setting = (res || []).filter(x => x.imgType === 'index')[0]
      if (setting) {
        if (setting.imgUrl) {
          shareImg.current = getImgURL(setting.imgUrl)
        }
        if (setting.imgDocument) {
          shareTitle.current = setting.imgDocument
        }
      }
    })
  }, [miniApp.name])

  return {
    title: shareTitle.current,
    path: `${router.path}?${paramsRef.current}`,
    image: shareImg.current,
  }
}
