(function () {
  if (document.body.dataset["marketwebComponentsSalesforceInit"]) {
    return;
  }
  document.body.dataset["marketwebComponentsSalesforceInit"] = true;

  const header = document.createElement("pn-marketweb-siteheader");
  const footer = document.createElement("pn-marketweb-sitefooter");
  let currentLanguage = document.querySelector("html").getAttribute("lang");
  if (currentLanguage === "en-US" || currentLanguage === "en_US") {
    currentLanguage = "en";
  }

  [header, footer].forEach((comp) => {
    comp.setAttribute("market", "dk");
    comp.setAttribute("language", currentLanguage);
    comp.setAttribute("site-domain-in-urls", "true");
    comp.setAttribute("hide-site-selector", "true");
    comp.setAttribute("spa-mode", "true");
    const environment =
      window.location.href.indexOf("kundeservice.postnord.dk") !== -1 ? "production" : "preproduction";
    comp.setAttribute("environment", environment);
  });
  document.body.insertBefore(header, document.body.firstElementChild);
  document.body.appendChild(footer);

  const currentMinorVersion = "4.3.0";
  const requestedComponentVersion = new URLSearchParams(window.location.search).get("componentversion");
  const versionToLoad = requestedComponentVersion ? requestedComponentVersion : currentMinorVersion;

  const s = document.createElement("script");
  s.setAttribute("type", "module");
  s.setAttribute(
    "src",
    `https://cdn.jsdelivr.net/npm/@postnord/pn-marketweb-components@${versionToLoad}/umd/standalone-loader-salesforce.js`
  );
  document.querySelector("head").appendChild(s);

  const style = document.createElement("link");
  style.setAttribute("rel", "stylesheet");
  style.setAttribute(
    "href",
    `https://cdn.jsdelivr.net/npm/@postnord/pn-marketweb-components@${versionToLoad}/umd/pn-marketweb-wrongrembase.css`
  );
  document.querySelector("head").appendChild(style);

  window["bindLanguageSelector"] = function () {
    if (window["haslangchange"]) {
      return;
    }
    window.addEventListener("marketweb-languagechange", (e) => {
      let lang = e.detail;
      if (lang === "en") {
        lang = "en_US";
      }
      const queryParams = new URLSearchParams(window.location.search);
      const currentLanguageValue = queryParams.get("language");

      if (lang.indexOf("en") !== -1) {
        const rootUrl = window.location.href.indexOf("kundeservice-test.postnord.dk" !== -1)
          ? "https://www.postnord.dk"
          : "https://dk-prep.postnord.com";
        window.location.href = `${rootUrl}/en/help-and-support/private`;
      }
      // if (currentLanguageValue === lang) {
      //     return;
      // }
      // queryParams.set("language", lang);
      // let newUrl = window.location.origin + window.location.pathname + '?'+ queryParams.toString();
      // if (newUrl.indexOf("/article/") !== -1) {
      //     if (lang === "en_US" && newUrl.indexOf("-da-") !== -1) {
      //         newUrl = newUrl.replace("-da-", "-en-");
      //     }
      //     if (lang === "da" && newUrl.indexOf("-en-") !== -1) {
      //         newUrl = newUrl.replace("-en-", "-da-");
      //     }
      // }

      window.location.href = newUrl;
    });
    window["haslangchange"] = true;
  };
  document.addEventListener("DOMContentLoaded", () => {
    window["bindLanguageSelector"]();
  });
  window.setTimeout(() => {
    window["bindLanguageSelector"]();
  }, 200);
  window["renderpncomponent-interval"] = window.setInterval(renderPNComponents, 150);
  renderPNComponents();
})();

function renderPNComponents() {
  const componentsDivs = [...document.querySelectorAll("[data-component]:not([data-component-rendered])")];
  componentsDivs.forEach((componentDiv) => {
    const component = document.createElement(componentDiv.getAttribute("data-component"));
    [...componentDiv.attributes].map((dataAttribute) => {
      if (dataAttribute.name.indexOf("data-component-") === -1) {
        return;
      }
      component.setAttribute(dataAttribute.name.replace("data-component-", ""), dataAttribute.value);
    });
    component.innerHTML = componentDiv.innerHTML;
    componentDiv.innerHTML = "";
    componentDiv.appendChild(component);
    componentDiv.setAttribute("data-component-rendered", "");
  });
}
