'use strict';
function getUrlParams() {
	var params = {};
	var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
		params[key] = value;
	});
	params['referrer'] = document.referrer;
	return params;
}
function getCookie(name) {
	var value = "; " + document.cookie;
	var parts = value.split("; " + name + "=");
	if (parts.length == 2) return parts.pop().split(";").shift();
}
function profitfillRequestWebsession() {

    var nonceval = document.getElementById('pf-session').getAttribute('value');
    let siteURL = document.getElementById('pf-pluginurl').getAttribute('value');
    let ga4id = document.getElementById('profitfill-ga4').getAttribute('value');
    let pluginURL = siteURL  + '/wp-content/plugins/profitfill_emails/';
    let allParams = {};
    allParams.utm_source = (null !== getCookie('pf_utm_source')) ? getCookie('pf_utm_source') : '';
    allParams.utm_medium = (null !== getCookie('pf_utm_medium')) ? getCookie('pf_utm_medium') : '';
    allParams.utm_campaign = (null !== getCookie('pf_utm_campaign')) ? getCookie('pf_utm_campaign') : '';
    allParams.utm_adgroup = (null !== getCookie('pf_utm_adgroup')) ? getCookie('pf_utm_adgroup') : '';
    allParams.utm_adgroupname = (null !== getCookie('pf_utm_adgroupname')) ? getCookie('pf_utm_adgroupname') : '';
    allParams.utm_campaignname = (null !== getCookie('pf_utm_campaignname')) ? getCookie('pf_utm_campaignname') : '';
    allParams.utm_id = (null !== getCookie('pf_utm_id')) ? getCookie('pf_utm_id') : '';
    allParams.utm_term = (null !== getCookie('pf_utm_term')) ? getCookie('pf_utm_term') : '';
    allParams.adgroupid = (null !== getCookie('pf_adgroupid')) ? getCookie('pf_adgroupid') : '';
    allParams.campaignid = (null !== getCookie('pf_campaignid')) ? getCookie('pf_campaignid') : '';
    allParams.utm_content = (null !== getCookie('pf_utm_content')) ? getCookie('pf_utm_content') : '';
    allParams.utm_promotion = (null !== getCookie('pf_utm_promotion')) ? getCookie('pf_utm_promotion') : '';
    allParams.utm_device = (null !== getCookie('pf_utm_device')) ? getCookie('pf_utm_device') : '';
    allParams.utm_source_platform = (null !== getCookie('pf_utm_source_platform')) ? getCookie('pf_utm_source_platform') : '';
    allParams.keyword = (getCookie('pf_keyword')) ? getCookie('pf_keyword') : '';
    allParams.gclid = (null !== getCookie('pf_gclid')) ? getCookie('pf_gclid') : '';
    allParams.wbraid = (null !== getCookie('pf_wbraid')) ? getCookie('pf_wbraid') : '';
    allParams.gbraid = (null !== getCookie('pf_gbraid')) ? getCookie('pf_gbraid') : '';
    allParams.msclkid = (null !== getCookie('pf_msclkid')) ? getCookie('pf_msclkid') : '';
    allParams.referrer = (null !== getCookie('pf_referrer')) ? getCookie('pf_referrer') : '';
    allParams.querystring = (null !== getCookie('pf_querystring')) ? getCookie('pf_querystring') : '';
    allParams.fbclid = (null !== getCookie('pf_fbclid')) ? getCookie('pf_fbclid') : '';

    allParams.user_id = (null !== getCookie('pf_uid')) ? getCookie('pf_uid') : null;

    let uetvid = (null !== getCookie('_uetvid')) ? getCookie('_uetvid') : null;
    let fbp = (null !== getCookie('_fbp')) ? getCookie('_fbp') : null;
    let ga = (null !== getCookie('_ga')) ? getCookie('_ga') : null;
    let gclAw = (null !== getCookie('_gcl_aw')) ? getCookie('_gcl_aw') : null;
    let pfSessionID = (null !== getCookie('profitfill_session_id')) ? getCookie('profitfill_session_id') : null;
    if(null !== gtag){
        // console.log('getting session id...');
        gtag('get', ga4id, 'session_id', (id) => { 
            let gasid = id; 
            const post = {
                _ga: ga,
                _gasid: gasid,
                _fbp: fbp,
                _uetvid: uetvid,
                _gcl_aw: gclAw,
                urlParams: allParams,
                profitfill_session_id: pfSessionID,
                pf_websession: nonceval,
                profitfill_user_id: allParams.user_id
            }
            console.log(post);
            const url = "/wp-json/profitfill/v1/websession";
            fetch(url, {
                method: 'POST',
                headers: {
                    'Accept': 'application/json',
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify(post)
            }).then(response => response.json()).then(response => { 
                // console.log(response);
                if(gtag){
                    gtag('set', {'user_id':allParams.user_id});
                } 
                allParams = null;
                profitfill_dni_start(response.data);
            });
        });
    }
}
function profitFillWebsession() {
    profitfillRequestWebsession();
}
if (window.addEventListener)
	window.addEventListener("load", profitFillWebsession, false);
else if (window.attachEvent)
    window.attachEvent("onload", profitFillWebsession);
else window.onload = profitFillWebsession;