import {register} from "@shopify/web-pixels-extension";
const version = '1.29';
//const pixelURL = 'https://dynasort.immersiveecommerce.com/pixel.php';
//const pixelURL = 'https://stats.dynasort.io/pixel.php';
const pixelURLs = [
  'https://shopify0.dynasort.io/pixel/',
  'https://shopify1.dynasort.io/pixel/',
  'https://shopify2.dynasort.io/pixel/',
  'https://shopify3.dynasort.io/pixel/',
  'https://shopify4.dynasort.io/pixel/',
  'https://shopify5.dynasort.io/pixel/',
  'https://shopify6.dynasort.io/pixel/',
  'https://shopify7.dynasort.io/pixel/',
  'https://shopify8.dynasort.io/pixel/',
  'https://shopify9.dynasort.io/pixel/',
];
const dev = true;
/*
this works!
*/
//const token = getOrSetTokenCookie();



register(async ({analytics, browser, settings}) => {
  console.log('dynasort pixel v'+version);
  //return;
    // Retrieve or set your tracking cookies
    let uid = await browser.cookie.get('mmm_cookies');
    if(dev) {console.log('uid 1: '+uid);}
    if(uid == ''){
        uid = getOrGenerateToken();
        setCookie = await browser.cookie.set('mmm_cookies',uid);
        if(dev) {console.log('uid 2: '+uid);}
    }

    //cart_viewed
    analytics.subscribe('cart_viewed', (event) => {
      if(dev) {console.log('Cart viewed event:', event);}
      sendPixelData('cart_viewed', event, uid);
    });
    //checkout_completed
    analytics.subscribe('checkout_completed', (event) => {
      if(dev) {console.log('checkout_completed event:', event);}
      sendPixelData('checkout_completed', event, uid);
    });
    //checkout_started
    analytics.subscribe('checkout_started', (event) => {
      if(dev) {console.log('checkout_started event:', event);}
      sendPixelData('checkout_started', event, uid);
    });
    //collection_viewed
    analytics.subscribe('collection_viewed', (event) => {
      if(dev) {console.log('collection_viewed event:', event);}
      sendPixelData('collection_viewed', event, uid);
    });
    //product_removed_from_cart
    analytics.subscribe('product_removed_from_cart', (event) => {
      if(dev) {console.log('product_removed_from_cart event:', event);}
      sendPixelData('product_removed_from_cart', event, uid);
    });
    //product_added_to_cart
    analytics.subscribe('product_added_to_cart', (event) => {
      if(dev) {console.log('product_added_to_cart event:', event);}
      sendPixelData('product_added_to_cart', event, uid);
    });
    //search_submitted
    analytics.subscribe('search_submitted', (event) => {
      if(dev) {console.log('search_submitted event:', event);}
      sendPixelData('search_submitted', event, uid);
    });
    //product_viewed
    analytics.subscribe('product_viewed', (event) => {
      if(dev) {console.log('product_viewed event:', event);}
      sendPixelData('product_viewed', event, uid);
    });
    //page_viewed
    analytics.subscribe('page_viewed', (event) => {
      if(dev) {console.log('Page view event:', event);}
      sendPixelData('page_view', event, uid);
    });



});

function sendPixelData(eventName, eventData, token) {
  //console.log('sendPixelData: '+eventName);
  const i = Math.floor(Math.random() * 10);
  const pixelURL = pixelURLs[i]+token.slice(0, 8)+'/';
  console.log(pixelURL);
  fetch(pixelURL, {
    method: 'POST',
    body: JSON.stringify({
      event: eventName,
      data: eventData,
      token: token,
    }),
    headers: {
      'Content-Type': 'application/json'
    }
  });
}
function getOrGenerateToken(length = 32) {
    const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    return Array.from(crypto.getRandomValues(new Uint8Array(length)))
      .map(x => chars[x % chars.length])
      .join('');
}
