
import { objectAssign } from '@spark/utils';
import sensors from 'sa-sdk-javascript';
import { ACTIVITY_SOURCE_CHANNEL, SYS_SOURCE_CHANNEL } from './constants';
import pageleave from './pageleave';

export const initSensors = (uid, openId, newEmpNo, friendOpenId) => {
  sensors.init({
    server_url: CFG.sensorUrl,
    is_track_single_page: true,
    show_log: true,
    heatmap: {
      clickmap: 'default',
      scroll_notice_map: 'not_collect',
      collect_tags: {
        div: true
      },
    },
  });

  sensors.login(uid);

  const shareTypeMap = {
    1: "客户邀请",
    2: "业务员邀请"
  }
  const skinId = getUrlParam("skinId");
  // 本次没区分业务员/客户 就null
  const shareType = !!skinId ? shareTypeMap[skinId] : null;

  const publicObjec = objectAssign({
    currentUrl: location.href,
    referrer: document.referrer,
    activityId: "25DragonBoat",
    unionid: uid,
    openid: openId,
    source: CFG.channel,
    systemName: '寿险2013版微信公众平台',
    activityName: CFG.activityName,
    c_date: "2025/5/19-2025/6/13",
    empNp: newEmpNo, // 用户实际绑定业务员工号(与转发无关)
    activitySource: ACTIVITY_SOURCE_CHANNEL[CFG.channel] || "",
    sysSourceName: SYS_SOURCE_CHANNEL[CFG.channel] || "",
    friendDistinctId: getUrlParam("uid") || "", // 转发人unionId
    shareMark: !!getUrlParam("uid") ? "是" : "否",
    shareType: shareType,
    friendEmpNo: getUrlParam("shareNewEmpNo"), // 邀请人的业务员工号(A转发给B,传A的业务员工号)
    friendOpenId:  getUrlParam("shareOpenId"), // 邀请人的openId
    empno: CFG.empno
  }, window['__tbSensorsObj__']);
  sensors.registerPage(publicObjec);
  sensors.quick('autoTrack');
};

export const pageLeave = (pagaName, duration) => {
  sensors.use(pageleave, {
    custom_props: {
      pagaName
    },
    heartbeat_intervaltime: duration,
    max_duration: duration * 24 * 60 * 60,
    isCollectUrl: function (url) {
      return true
    }
  })
}

/**
 * 页面曝光埋点
 * @param {string} eventName
 * @param {{pageName:string,activityName:string,prize:string}} option
 */
export const sensorMdExpouse = (eventName, option) => {
  sensors.track(eventName, option);
};

/**
 * 页面点击埋点
 * @param {string} eventName
 * @param {{pageName:string,activityName:string,buttonName:string,prize:string}} option
 */
export const sensorMdClick = (eventName, option) => {
  sensors.track(eventName, option);
};
