import React, { useEffect, useRef } from 'react'
import Taro, { useShareAppMessage } from '@tarojs/taro'
import shareTracking from '@/utils/tracking/share'

export default function useShare(shareContent, trackData) {
  const shareRef = useRef()
  const trackRef = useRef()

  shareRef.current = shareContent
  trackRef.current = trackData

  useEffect(() => {
    if (shareContent?.title) {
      Taro.showShareMenu({
        withShareTicket: true,
        menus: ['shareAppMessage', 'shareTimeline']
      })
    }
  }, [shareContent?.title])

  useShareAppMessage((res) => {
    const {
      title,
      path,
      image,
    } = shareRef.current || {}

    const {
      event = 'share',
      eventType,
      propertyName,
      consultantId,
      sharePersonId,
      targetId,
      buildingId,
    } = trackRef.current || {}

    // 分享埋点
    shareTracking({
      event,
      eventType,
      propertyName,
      consultantId,
      sharePersonId,
      targetId,
      buildingId,
    })
    
    return { title, path, imageUrl: image }
  })
}
