"use strict";
// dark theme stuff
const setTheme = (theme) => {
  document.documentElement.setAttribute("data-bs-theme", theme);
  if (theme === "dark") {
    var to_invert = document.querySelectorAll(".darkmode-invert");
    to_invert.forEach((element) => {
      element.style.filter = "invert(100%)";
    });
  }
  if (theme === "light") {
    var to_invert = document.querySelectorAll(".darkmode-invert");
    to_invert.forEach((element) => {
      element.style.filter = "invert(0%)";
    });
  }
};
setTheme(localStorage.getItem("theme"));
document.getElementById("toggleThemeButton").onclick = function () {
  const storedTheme = localStorage.getItem("theme");
  if (storedTheme == null) {
    localStorage.setItem("theme", "dark");
    document.getElementById("toggleThemeIcon").className = "bi bi-sun";
    setTheme("dark");
  } else if (storedTheme == "dark") {
    localStorage.setItem("theme", "light");
    document.getElementById("toggleThemeIcon").className = "bi bi-sun-fill";
    setTheme("light");
  } else if (storedTheme == "light") {
    localStorage.setItem("theme", "dark");
    document.getElementById("toggleThemeIcon").className = "bi bi-sun";
    setTheme("dark");
  }
};

// i18n stuff
function get_current_lang() {
  return location.pathname.split("/")[1];
}

// reset some forms to empty instead of the defaults in value attrs
document.querySelectorAll(".reset-empty-button").forEach((element) => {
  element.onclick = function () {
    var items = element.form.querySelectorAll("input, select, .tagify");
    items.forEach((element) => {
      if (element.type === "reset") {
      } else {
        if (element instanceof HTMLInputElement) {
          element.setAttribute("value", element.getAttribute("defaultValue"));
        } else if (element instanceof HTMLSelectElement) {
          element.options
            .item(element.selectedIndex)
            .removeAttribute("selected");
          element.options.item(0).setAttribute("selected", "");
        }
      }
    });
    element.form.reset();
  };
});

// smaller cards depending on viewport
document.querySelectorAll(".cardrow").forEach((element) => {
  var level = Math.min(Math.floor(screen.width / 200), 6);
  element.classList.add("row-cols-" + level);
});
