import { useEffect, useState } from 'react'
import { getMiniQrcode } from '@/services/common'

export default function usePoster(person, poster, router, paramsRef) {
  const [posterData, setPosterData] = useState()

  useEffect(() => {
    if (poster) {
      const page = router.path.indexOf('/') === 0 ? router.path.substring(1) : router.path
      const scene = paramsRef.current
  
      getMiniQrcode({ page, scene }).then((miniCode) => {
        setPosterData({
          poster,
          miniCode,
          name: person.name || person.nickname,
          avatar: person.avatarurl,
        })
      }).catch(() => {
        setPosterData({
          poster,
          miniCode: undefined,
          name: person.name || person.nickname,
          avatar: person.avatarurl,
        })
      })
    }
  // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [router.path, poster])

  return posterData
}
