var _detectDocumentLangua;

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

import en from './locale/en.json';
import es from './locale/es.json';
import get from 'lodash/get';

function detectDocumentLanguage() {
  var _document$querySelect, _document$querySelect2;

  if (typeof document === 'undefined') {
    return undefined;
  }

  var detectedLang = (_document$querySelect = (_document$querySelect2 = document.querySelector('html')) === null || _document$querySelect2 === void 0 ? void 0 : _document$querySelect2.lang) !== null && _document$querySelect !== void 0 ? _document$querySelect : '';

  if (['en', 'es'].some(function (lang) {
    return languageMatches(lang, detectedLang);
  })) {
    return detectedLang;
  } else {
    return undefined;
  }
}

var language = (_detectDocumentLangua = detectDocumentLanguage()) !== null && _detectDocumentLangua !== void 0 ? _detectDocumentLangua : 'en';
export function getLanguage() {
  return language;
}
export function setLanguage(lang) {
  language = lang;
}
export function getTranslations() {
  var lang = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : getLanguage();
  return languageMatches('en', lang) ? en : es;
}
export function addTranslations(lang, translations) {
  _extends(getTranslations(lang), translations);
}
/**
 * Because language strings can contain region subtags, we need a way to compare
 * just the language portion of two language strings. This function compares two
 * locale strings that may or may not contain subtags according to IETF BCP 47.
 * The second string defaults to our globally set language.
 */

export function languageMatches(localeStringA) {
  var localeStringB = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : getLanguage();
  var langA = localeStringA.split('-')[0];
  var langB = localeStringB.split('-')[0];
  return langA === langB;
}
/**
 * Falls back to a more generic locale if the more specific one isn't
 * available in this browser. Testing platforms tend to have only a
 * few locales.
 */

export function fallbackLocale(language, subtag) {
  try {
    var locale = "".concat(language, "-").concat(subtag); // If the locale string is invalid, the following line will fail and jump to
    // our catch block; otherwise, it is valid and safe to return it.

    new Date().toLocaleString(locale);
    return locale;
  } catch (error) {
    return language;
  }
}
/**
 * Returns the translation for a given key for a given language. For most
 * use cases, the `t` function will be more appropriate, where the language
 * is not a required parameter. Use this when you need a translation from
 * a specific language.
 */

export function translate() {
  var lang = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : getLanguage();
  var key = arguments.length > 1 ? arguments[1] : undefined;
  var data = arguments.length > 2 ? arguments[2] : undefined;
  var rawTranslation = get(getTranslations(lang), key);

  if (typeof rawTranslation !== 'string') {
    throw new Error("Translation key '".concat(key, "' does not resolve to a string."));
  }

  if (data) {
    // Replace template strings with provided data
    var interpolatedTranslation = Object.keys(data).reduce(function (interpolatedString, dataKey) {
      return interpolatedString.replace("{{".concat(dataKey, "}}"), data[dataKey]);
    }, rawTranslation);
    return interpolatedTranslation;
  } else {
    return rawTranslation;
  }
}
/**
 * Returns the translation for a given key in the currently set language.
 */

export function t(key, data) {
  return translate(getLanguage(), key, data);
}

/**
 * Returns a translation function bound to a specific language.
 *
 * Note that we don't want to use this to create the default `t` function
 * because it will bind with whatever the default language is AT THAT TIME,
 * so if the global language changes after we call this function,
 * translations coming out if it wouldn't pick up on the change.
 */
export function tWithLanguage(lang) {
  return function t(key, data) {
    return translate(lang, key, data);
  };
}