import { savePoint, updatePoint } from '@/services/common'
import { routes } from '../../routes'

export default async (router) => {
  const { path, query, scene } = router || {}
  const { id, buildingId, recommender } = query || {}

  const pageInfo = routes.filter(x => path.indexOf(x.page) > -1)[0] || { type: 'other', name: '其他' }

  const trackPayload = {
    event: 'start',
    eventType: pageInfo.type,
    propertyName: pageInfo.name,
    data: '{}',
    id,
    buildingId,
    realScene: scene,
    sceneId: scene,
    sharePersonId: recommender,
  }

  try {
    const { recordId } = await savePoint(trackPayload)
    return function () { updatePoint(recordId); }
  } catch (e) {
    console.error('进入小程序埋点出错:', e);
    return function () { }
  }
}
