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

// 此处映射需要与 td_biz_event_type 一致
const pathEventTypeMap = {
  // '' : 'agent',  // 暂时不需要
  // '' : 'info',  // 暂时不需要
  'pages/activity/detail/index': { type: 'activity', desc: '活动' },
  'pages/activity/detail/assistance': { type: 'activity', desc: '活动' },
  'pages/activity/detail/assemble': { type: 'activity', desc: '活动' },
  'pages/project/detail/index': { type: 'building', desc: '项目' },
  'pages/card/index': { type: 'card', desc: '名片' },
  'pages/project/index': { type: 'main', desc: '首页' },
  'pages/news/detail/index': { type: 'news', desc: '资讯' },
  'pages/project/h5Page': { type: 'h5', desc: 'H5' },
}

/**
 * 用户来源
 * @param {*} router 
 */
export async function trackUserSource(router) {
  const { path, query, scene } = router || {}

  const pageInfo = pathEventTypeMap[path] || { type: '', desc: '其他' }

  const trackPayload = {
    event: 'start',
    eventType: pageInfo.type,
    propertyName: pageInfo.desc,
    data: '{}',
    id: (query || {}).id,
    buildingId: (query || {}).buildingId || '',
    sceneId: scene,
    sharePersonId: (query || {}).recommender,
  }
  console.log(trackPayload, "trackPayloadtrackPayloadtrackPayload")
  console.log(trackPayload.sceneId, "sceneIdsceneIdsceneIdsceneIdsceneId")
  console.log(trackPayload.eventType, "eventTypeeventTypeeventTypeeventTypeeventType")

  try {
    const sceneList = [1005, 1006, 1053, 1042, 1007, 1008, 1011, 1012, 1013, 1045, 1046, 1047, 1048, 1058, 1067, 1000]
    if (sceneList.indexOf(trackPayload.sceneId) != -1) {

      const { recordId } = await savePoint(trackPayload)
      return function () { updatePoint(recordId); }

    }

  } catch (e) {
    console.error('进入小程序埋点出错:', e);
    return function () { }
  }
}
