// 点击埋点自定义属性

import { reportLog } from '../logger';
import { getTargetElementWhenClick } from '@/uilts/help';
import Report from "@/log-center/log";

let clickDataMap = new Map();

const clickHandle = (e) => { 
    const target = getTargetElementWhenClick(e);
    const { extParams, ...eventData } = clickDataMap.get(target?.denetClickLogkey);
    return eventData && reportLog({
        businessType: Report.businessType.buttonClick,
        ...eventData
    }, extParams)
}

const clickLog =  {
    mounted: (el, binding) => { 
        const { value } = binding;
        el.denetClickLogkey = el.denetClickLogkey || Math.random().toString(36).slice(-6);
        clickDataMap.set(el.denetClickLogkey, value);
        el.addEventListener('click', clickHandle,true)
    },
    unmounted(el) { 
        // remove EventListener
        el.removeEventListener('click', clickHandle, true)
    }
}

export default clickLog;