const mirrorCF = {"cityName":"Ashburn","countryRegionCode":false,"countryRegionName":false,"postalCode":false,"countryName":false,"timeZone":false,"isDesktop":"true","isMobile":"false","isAndroid":"false","isIOs":false}

var url = new URL(window.location.href);
Date.prototype.monthNames = [
    'January', 'February', 'March',
    'April', 'May', 'June',
    'July', 'August', 'September',
    'October', 'November', 'December'
];
let anchorUrlParams;

Date.prototype.getMonthName = function(addition) {
    return this.monthNames[this.getMonth()+addition];
};

function replaceUrlInAnchors(className) {
    let getUrlParams = (url) => {
        let params = {};
        (url.split('?')[1] || '').split('&').forEach(param => {
            let parts = param.split('=');
            if(parts[0] && parts[1]) {
                params[parts[0]] = decodeURIComponent(parts[1]);
            }
        });
        return params;
    }

    let availableUrlParams = getUrlParams(window.location.href);
    let anchors = document.querySelectorAll('.'+className);

    for(let i = 0; i < anchors.length; i++) {
        let anchor = anchors[i];
        let linktext = anchor.textContent;
        let linkurl = anchor.href;
        let macroreplacements = new RegExp(/([^[]+(?=]))/gm);
        let textreplacements = linktext.match(macroreplacements);

        if(textreplacements) {
            for(const replacement of textreplacements) {
                let effectivereplacement = '';
                const options = { weekday: 'long' };

                switch(replacement) {
                    case 'year':
                        effectivereplacement = new Date().getFullYear();
                        break;
                    case 'nextyear':
                        effectivereplacement = new Date().getFullYear()+1;
                        break;
                    case 'month':
                        effectivereplacement = new Date().getMonthName(0);
                        break;
                    case 'nextmonth':
                        effectivereplacement = new Date().getMonthName(1);
                        break;
                    case 'nextnextmonth':
                        effectivereplacement = new Date().getMonthName(1);
                        break;
                    case 'today':
                        effectivereplacement = new Intl.DateTimeFormat('en-US', options).format(new Date());
                        break;
                    case 'nextday':
                        const nextday  = new Date(); 
                        nextday.setDate(nextday.getDate() + 1);
                        effectivereplacement = new Intl.DateTimeFormat('en-US', options).format(nextday);
                        break;
                    case 'nextnextday':
                        const nextnextday  = new Date(); 
                        nextnextday.setDate(nextnextday.getDate() + 2);
                        effectivereplacement = new Intl.DateTimeFormat('en-US', options).format(nextnextday);
                        break;
                    case 'city':
                        effectivereplacement = (mirrorCF['cityName'])? mirrorCF['cityName'] : 'Your City';
                        break;
                    case 'statecode':
                        effectivereplacement = (mirrorCF['countryRegionCode'])? mirrorCF['countryRegionCode'] : 'Your State';
                        break;
                    case 'state':
                        effectivereplacement = (mirrorCF['countryRegionName'])? mirrorCF['countryRegionName'] : 'Your State';
                        break;
                    case 'zip':
                        effectivereplacement = (mirrorCF['postalCode'])? mirrorCF['postalCode'] : 'Your Zip Code';
                        break;
                    case 'country':
                        effectivereplacement = (mirrorCF['countryName'])? mirrorCF['countryName'] : 'Your Country';
                        break;
                    case 'device':
                        effectivereplacement = (mirrorCF['isDesktop'])? 'Computer' : ((mirrorCF['isAndroid'])? 'Android' : ((mirrorCF['isIOs'])? 'iPhone' : 'Smartphone'));
                        break;
                }

                anchor.textContent = linktext.replace('['+replacement+']', effectivereplacement);
                anchor.href = linkurl.replace('%5B'+replacement+'%5D', effectivereplacement);
            }
        }

        anchorUrlParams = getBracketedParams(anchor.href);
        for(let param of anchorUrlParams) {
            let bracketedParam = '[' + param + ']';
            if(availableUrlParams[param]) {
                anchor.href = anchor.href.replace(bracketedParam, availableUrlParams[param]);
            }else if(param === 'atxt' || param === 'cta_text') {
                anchor.href = anchor.href.replace(bracketedParam, anchor.textContent);
            } 
        }
    }
}


function getBracketedParams(str) {
    let regex = /\[([^\]]+)\]/g;
    let params = [];
    let match;
    while((match = regex.exec(str)) !== null) {
        params.push(match[1]);
    }
    //console.log(params);
    return params;
}


if (window.location.host === 'factavenue.net') {
    replaceUrlInAnchors('r4Link');
    window.addEventListener('load', function() {
      if (typeof mirrorCF !== 'undefined') {
        replaceUrlInAnchors('r4Link');
      }
    });
  }
  


if (window.ee) {
    window.ee.geo = mirrorCF;
    window.ee.go = window.ee.go || {};
    window.ee.go.geo = Date.now();
    document.dispatchEvent(new Event('geoloaded'));
}


