let path = window.location.pathname
let should_collect = localStorage.getItem("tinylytics_ignore") == null;
let current_url = encodeURIComponent(window.location.href)
let collect_url = "https://tinylytics.app/collector/2cdzWymksXsrb2EEUwzV"
let set_ignore_param = new URLSearchParams(document.location.search)?.get("tiny_ignore") || new URLSearchParams(document.location.search)?.get("ti")
let referrer = document.referrer.indexOf(window.location.href) < 0 ? document.referrer : ""
if(set_ignore_param){
  if(set_ignore_param === "true" && localStorage.getItem("tinylytics_ignore") == null){
    localStorage.setItem("tinylytics_ignore", true)
    should_collect = false
    alert("tinylytics will no longer track your own hits in this browser.")
  }
  else if (set_ignore_param === "false" && localStorage.getItem("tinylytics_ignore") != null){
    localStorage.removeItem("tinylytics_ignore")
    should_collect = true
    alert("tinylytics has been enabled for this website, for this browser.")
  }
  if(set_ignore_param === "true"){
    localStorage.setItem("tinylytics_ignore", true)
  }
}
if(should_collect){
  fetch(`${collect_url}?url=${current_url}&path=${path}&referrer=${referrer}`, {method: "post"})
}
let hit_containers = document.querySelectorAll(".tinylytics_hits")
if(hit_containers){
  let hits_count_url = "https://tinylytics.app/hits/2cdzWymksXsrb2EEUwzV"
  hit_containers.forEach((hits_container) => {
    let hits_path = hits_container.getAttribute("data-path")
  if(hits_path){
    fetch(`${hits_count_url}?path=${hits_path}`).then(response => response.json()).then((data) => {if(data.count != null){hits_container.innerHTML=`${data.count}`}})
  }
  else{
    hits_container.innerHTML = "13,126"
  }
  })
}
let kudos_buttons = document.querySelectorAll(".tinylytics_kudos")
if(kudos_buttons){
  let kudos_collect_url = "https://tinylytics.app/kudos/2cdzWymksXsrb2EEUwzV"
  kudos_buttons.forEach((kudos_button) => {
    let kudos_path = kudos_button.getAttribute("data-path") ?? path
    let kudos_count = 0
    let private_kudos = kudos_button.getAttribute("data-private")
    fetch(`${kudos_collect_url}.json?path=${kudos_path}`).then(response => response.json()).then((data) => {if(data.count != null){kudos_button.innerHTML=`👍${private_kudos == null ? ` ${data.count}` : ""}`; if(private_kudos == null){kudos_button.setAttribute("data-count", data.count);kudos_count = data.count}}}) 
    let existing_kudos = localStorage.getItem(`tiny_kudos_${kudos_path}`)
    if(existing_kudos){
      kudos_button.disabled = "disabled"
      kudos_button.classList.add("did_select")
    }
    else{
      kudos_button.addEventListener("click", () => {
        let no_store = kudos_button.getAttribute("data-ignore")
        kudos_button.disabled = "disabled"
        fetch(`${kudos_collect_url}?path=${kudos_path}`, {method: "post"}).then(response => response.json()).then((data) => {if(data.id != null && !no_store){localStorage.setItem(`tiny_kudos_${kudos_path}`, data.id)}})
        kudos_button.innerHTML = `👍${private_kudos == null ? ` ${kudos_count + 1}` : ""}`
        kudos_button.classList.add("did_select")
      })
    }
  })
}
let webring_buttons = document.querySelectorAll(".tinylytics_webring")
if(webring_buttons){
  let webring_avatars = true
  let webring_url = "https://tinylytics.app/webring/random/2cdzWymksXsrb2EEUwzV?show_avatar"
  webring_buttons.forEach((webring_button) => {
    fetch(`${webring_url}`).then(response => response.json()).then((data) =>
    {if(data.url != null){
      webring_button.href=`${data.url}`;
      if(data.avatar != null){
        const webring_avatar_tag = webring_button.querySelector(".tinylytics_webring_avatar")
        if(webring_avatar_tag){
          webring_avatar_tag.src = data.avatar
          webring_avatar_tag.style.display = "inline"
          if(data.avatar_2x != null){
            webring_avatar_tag.srcset = `${data.avatar_2x} 2x`
          }
        }
      }
    }}) 
  })
}
let countries_container = document.querySelector(".tinylytics_countries")
if(countries_container){
  countries_container.innerHTML = "🇦🇪🇦🇷🇦🇹🇦🇺🇧🇪🇧🇬🇧🇷🇨🇦🇨🇭🇨🇱🇨🇳🇨🇴🇨🇷🇨🇺🇨🇿🇩🇪🇩🇰🇪🇨🇪🇪🇪🇸🇫🇮🇫🇷🇬🇧🇬🇪🇬🇬🇬🇷🇭🇰🇭🇷🇭🇺🇮🇩🇮🇪🇮🇱🇮🇳🇮🇹🇯🇲🇯🇵🇰🇪🇰🇬🇰🇭🇰🇷🇰🇿🇱🇹🇱🇺🇲🇽🇲🇾🇲🇿🇳🇱🇳🇴🇳🇿🇵🇦🇵🇪🇵🇭🇵🇰🇵🇱🇵🇷🇵🇹🇵🇾🇷🇴🇷🇸🇷🇺🇸🇦🇸🇪🇸🇬🇸🇮🇸🇷🇹🇭🇹🇷🇹🇼🇺🇦🇺🇸🇻🇪🇻🇮🇻🇳🇼🇸🇿🇦"
}
