/* global React, ForsvarI18n, MESSAGES, I18N_lookup */
const LangContext = React.createContext(null);

function ensureMetaProperty(property, content) {
  if (content == null || content === "") return;
  var sel = 'meta[property="' + property + '"]';
  var el = document.querySelector(sel);
  if (!el) {
    el = document.createElement("meta");
    el.setAttribute("property", property);
    document.head.appendChild(el);
  }
  el.setAttribute("content", content);
}

function ensureMetaName(name, content) {
  if (content == null || content === "") return;
  var sel = 'meta[name="' + name + '"]';
  var el = document.querySelector(sel);
  if (!el) {
    el = document.createElement("meta");
    el.setAttribute("name", name);
    document.head.appendChild(el);
  }
  el.setAttribute("content", content);
}

function LangProvider({ children, pageId }) {
  var getInitial = function () {
    return typeof ForsvarI18n !== "undefined" ? ForsvarI18n.getLang() : "es";
  };
  var _a = React.useState(getInitial);
  var lang = _a[0];
  var setLangState = _a[1];

  React.useEffect(function () {
    var onEvt = function (e) {
      setLangState(e.detail.lang);
    };
    window.addEventListener("forsvar-lang-change", onEvt);
    return function () { window.removeEventListener("forsvar-lang-change", onEvt); };
  }, []);

  var setLang = React.useCallback(function (code) {
    if (typeof ForsvarI18n !== "undefined") {
      ForsvarI18n.setLang(code);
    }
    setLangState(typeof ForsvarI18n !== "undefined" ? ForsvarI18n.getLang() : "es");
  }, []);

  var t = React.useCallback(function (path) {
    return typeof I18N_lookup !== "undefined" ? I18N_lookup(lang, path) : path;
  }, [lang]);

  React.useEffect(function () {
    var L = lang;
    document.documentElement.lang = L === "pt" ? "pt" : L === "en" ? "en" : "es";
    if (pageId && typeof I18N_lookup !== "undefined" && typeof MESSAGES !== "undefined") {
      var metaTitle = I18N_lookup(lang, "meta." + pageId + ".title");
      var metaDesc = I18N_lookup(lang, "meta." + pageId + ".description");
      if (metaTitle && metaTitle.indexOf("meta.") !== 0) {
        document.title = metaTitle;
      }
      var descEl = document.querySelector('meta[name="description"]');
      if (descEl && metaDesc && metaDesc.indexOf("meta.") !== 0) {
        descEl.setAttribute("content", metaDesc);
      }
      var ogUrl = typeof window !== "undefined"
        ? window.location.origin + window.location.pathname
        : "";
      ensureMetaProperty("og:type", "website");
      if (ogUrl) ensureMetaProperty("og:url", ogUrl);
      if (metaTitle && metaTitle.indexOf("meta.") !== 0) {
        ensureMetaProperty("og:title", metaTitle);
        ensureMetaName("twitter:title", metaTitle);
      }
      if (metaDesc && metaDesc.indexOf("meta.") !== 0) {
        ensureMetaProperty("og:description", metaDesc);
        ensureMetaName("twitter:description", metaDesc);
      }
    }
  }, [lang, pageId]);

  var val = React.useMemo(function () {
    return { lang: lang, setLang: setLang, t: t, pageId: pageId };
  }, [lang, setLang, t, pageId]);

  return <LangContext.Provider value={val}>{children}</LangContext.Provider>;
}

function useLang() {
  var ctx = React.useContext(LangContext);
  if (!ctx) {
    return {
      lang: typeof ForsvarI18n !== "undefined" ? ForsvarI18n.getLang() : "es",
      setLang: function (c) { if (typeof ForsvarI18n !== "undefined") ForsvarI18n.setLang(c); },
      t: function (p) { return typeof I18N_lookup !== "undefined" ? I18N_lookup(ForsvarI18n.getLang(), p) : p; },
      pageId: null
    };
  }
  return ctx;
}

function useT() {
  return useLang().t;
}

window.LangProvider = LangProvider;
window.useLang = useLang;
window.useT = useT;
window.LangContext = LangContext;
