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

// 此处映射需要与 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 
 */
// eslint-disable-next-line import/prefer-default-export
export async function trackUserSource(router) {
  const { path, query, scene } = router || {}

  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: (query || {}).id,
    buildingId: (query || {}).buildingId || '',
    realScene: scene,
    sceneId: scene,
    sharePersonId: (query || {}).recommender,
  }

  console.log('---app track--->', trackPayload)

  try {
    const sceneList = [1005, 1006, 1053, 1042, 1007, 1008, 1011, 1012, 1013, 1045, 1046, 1047, 1048, 1058, 1067, 1000]

    // 任意页面进入小程序, 都需要埋点
    // 2020年4月27日 v3.5.21 统计版本
    if (sceneList.indexOf(trackPayload.sceneId) === -1) {
      trackPayload.sceneId = 1000
    }
    const { recordId } = await savePoint(trackPayload)
    return function () { updatePoint(recordId); }
  } catch (e) {
    console.error('进入小程序埋点出错:', e);
    return function () { }
  }
}
