function PVCL_loadScript(url, callback=null) {
     var script = document.createElement('script');
     script.src = url;
     var head = document.getElementsByTagName('head')[0],
     done = false;
     head.appendChild(script);
     script.onload = script.onreadystatechange = function() {
        if (!done && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete')) {
             done = true;
             script.onload = script.onreadystatechange = null;
             head.removeChild(script);
             if (callback !== null) {
                callback();
             }
        }
    };
}

function PVC_getLsWithExpiry(key) {
	const itemStr = localStorage.getItem(key)
	if (!itemStr) {
		return null
	}

	const item = JSON.parse(itemStr)
	const now = new Date()
	if (now.getTime() > item.expiry) {
		localStorage.removeItem(key)
		return null
	}

	return item.value.toString();
}

function PVC_setLsWithExpiry(key, value, ttl) {
    value = value.toString();
	const now = new Date()
	const item = {
		value: value,
		expiry: now.getTime() + ttl,
	}
	localStorage.setItem(key, JSON.stringify(item))
}

function PVCL_main() {
    Date.prototype.addHours = function(h) {
      this.setTime(this.getTime() + (h*60*60*1000));
      return this;
    }

    var now = new Date;
    var shouldSetLS = true;
    var productIdentifier = '';
    if ('2' === '1') {
        productIdentifier = location.host + location.pathname;
        var translationDetection = productIdentifier.match(location.host + "\/[a-zA-Z]{2}\/");
        if (translationDetection !== null && translationDetection.length > 0) {
            productIdentifier = productIdentifier.replace(translationDetection[0], location.host + '/');
            productIdentifier = CryptoJS.MD5(productIdentifier).toString();
        }
    }
    else {
        productIdentifier = ShopifyAnalytics.meta.product.id.toString();
    }

    if ('True' === 'True') {
        var productPVCTs = localStorage.getItem('effectiveAppsPVC_' + productIdentifier + '_378');
        if (productPVCTs !== null) {
            var productPVCDate = new Date(parseInt(productPVCTs));
            var utcNowTimestamp = Date.UTC(now.getUTCFullYear(),now.getUTCMonth(), now.getUTCDate(), now.getUTCHours(),
                           now.getUTCMinutes(), now.getUTCSeconds(), now.getUTCMilliseconds());

            if (utcNowTimestamp < productPVCDate) {
                shouldSetLS = false;
            }
        }

        if (shouldSetLS === true) {
            var futureDate = now.addHours(parseInt('168'));
            var utcFutureTimestamp = Date.UTC(futureDate.getUTCFullYear(),futureDate.getUTCMonth(), futureDate.getUTCDate(),
                                           futureDate.getUTCHours(), futureDate.getUTCMinutes(), futureDate.getUTCSeconds(),
                                           futureDate.getUTCMilliseconds());
            localStorage.setItem('effectiveAppsPVC_' + productIdentifier + '_378', utcFutureTimestamp.toString());
        }
    }

    const isOnboarding = PVC_getLsWithExpiry('pvcOnboarding') === null ? 'false' : 'true';
    PVCL_loadScript('https://app.visitorscounter.app/get_pvc/6461eb66d86911ef9fe6aeba1b5f5c0c.js' + '?u=' + productIdentifier + '_378' + '&q=' + shouldSetLS.toString() + '&o=' + isOnboarding);
}

if (location.href.indexOf('pvcOnboarding=true') > -1) {
    PVC_setLsWithExpiry('pvcOnboarding', 'true', 60000);
}

if ("product" === ShopifyAnalytics.meta.page.pageType || location.href.indexOf("/products") > -1) {
    if (typeof(CryptoJS) === 'undefined' && '2' === '1') {
        PVCL_loadScript('https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js', PVCL_main);
    }
    else {
        PVCL_main();
    }
}