//module for switching languages
import $ from "./jquery.module.js";

const lang1Event = new Event("lang1Event7Gen");
const lang2Event = new Event("lang2Event7Gen");

let currentLanguageKey = "currentLanguage7Gen";
function langSwitch(){
  const $lang1 = $(".lang1");
  const $lang2 = $(".lang2");
  if ($lang2.hasClass("d-none")) {
    $lang1.addClass("d-none");
    $lang2.removeClass("d-none");
    window.dispatchEvent(lang1Event);
    currentLanguage( "lang2");
  } else {
    $lang2.addClass("d-none");
    $lang1.removeClass("d-none");
    window.dispatchEvent(lang2Event);
    currentLanguage( "lang1");
  }
}

function setLang(lang) {
  const $lang1 = $(".lang1");
  const $lang2 = $(".lang2");

  switch(lang) {
    case "lang1":
      $lang2.addClass("d-none");
      $lang1.removeClass("d-none");
      break;

    case "lang2":
      $lang1.addClass("d-none");
      $lang2.removeClass("d-none");
      break;
  }
  currentLanguage( lang);
}
function currentLanguage(newLang) {
  if (newLang) {
    sessionStorage.setItem(currentLanguageKey, newLang);
    return newLang;
  }
  else {
    return sessionStorage.getItem(currentLanguageKey);
  }
}
// Automatically attach script to any element with class lang_style
$(() => {
  $(".lang_style").on("click", langSwitch);
});

export {langSwitch, setLang, currentLanguage};
