<script>
  
      
function getProductElements(element) {
    return [...element.querySelectorAll(`.fs-results-product-card`)];
}
     
function setNewPrice(productElement, newPrice, newCompare, country) {    
  let priceElement = productElement.querySelector('.price, .fs-price');
  let compareElement = null;
  if(newCompare) {
    compareElement = productElement.querySelector('.compare, .fs-price.compare');
  }
  
  if(newPrice && priceElement) {    
    if(compareElement) {
      priceElement.textContent = "$" + newPrice;  
    } else {
      priceElement.textContent = "$" + newPrice;
      if (priceElement.querySelectorAll(`.tax-title`).length === 0) {
        priceElement.insertAdjacentHTML('beforeend', '<span class="tax-title">+ gst</span>');
      }
    }    
  }
  if(newCompare) {    
    if(compareElement) {      
      compareElement.textContent = "$" + newCompare;
      if (compareElement.querySelectorAll(`.tax-title`).length === 0) {
        compareElement.insertAdjacentHTML('beforeend', '<span class="tax-title">+ gst</span>');
      }
    }
  }
}
async function setProductSpecialPrice(productElement, pId, productURL){
      const country = Shopify?.country;
      if(!productURL) {
          return;
      }
      const splattedURL = productURL.split('?');
      let fullURL = `${splattedURL[0]}.json${splattedURL?.[1] ? `?${splattedURL[1]}` : ''}`;
      const res = await fetch(fullURL);
      const data = await res.json();
  
      const product = data.product;
  
      const variantID = productURL.split('&').find(param => param.includes('variant'))?.split('=')?.[1];
  
      const variant = product.variants.find((variant) => variant.id === Number(variantID)) || product.variants?.[0];
  
      if (variant) {
        
        const compare = variant?.compare_at_price ? variant?.compare_at_price : undefined;
        
        const price = variant?.price ? variant?.price : 0;
        
        setNewPrice(productElement, price, compare, country);
        
      }
}
  
function hooks() {   
    console.log("country - " + Shopify?.country);
    SerpOptions.registerHook('serp-product-grid', ({products, element}) => {
      for (const productElement of getProductElements(element)) {
        
        const productID = productElement.dataset.productId;
        const data = products[productID];
        if ( typeof(data) !== "undefined" && data !== null ) {        
          let productURL = data.productURL || '';
                        
          setProductSpecialPrice(productElement, productID, productURL);
        }              
     }       
    });
}
function AChooks() {
    FastAutocomplete.registerHook('render-autocomplete-product-item', ({product, index, element}) => {
          
          let productURL = product.u || '';
          let productID = product.id;
          setProductSpecialPrice(element, productID, productURL);
    });
}
if (window.FastAutocomplete) {
    AChooks();
} else {
    window.addEventListener('fast-autocomplete-ready', function () {
        AChooks();
    });
}
  
if (window.SerpOptions) {
    hooks();
} else {
    window.addEventListener('fast-serp-ready', function () {
        hooks();
    });
}  
 
</script>