"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.loadLocaleData = loadLocaleData;
exports.getDefaultLocale = getDefaultLocale;
exports.getTimeFormat = getTimeFormat;

require("core-js/modules/web.dom.iterable");

var _flat = _interopRequireDefault(require("flat"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

/**
 * List of the supported locales.
 * FIXME: generate this list from available yml files in the i18n folder instead
 * (even if we end up loading locale data using fetch).
 */
const locales = {
  en: {
    us: 'en-US'
  },
  fr: {
    fr: 'fr-FR'
  }
};
/**
 * Match the provided locale (language and region) to the list of supported locales.
 * Default to english if unsupported.
 */

function getMatchingLocaleString(locale = '', defaultLocale = 'en-US') {
  const [lang, region] = locale.toLowerCase().split('-');
  const language = locales[lang];
  if (!language) return defaultLocale;
  const defaultRegion = Object.keys(language)[0];
  return language[region] || language[defaultRegion];
}
/*
 * Load the localized strings for all languages here,
 * merge with the provided customized strings from state.otp.config.language
 * (overrides can be defined per language or for all languages)
 * and flatten to a map of
 *   id => string.
 * FIXME: Load languages on demand using fetch.
 */


async function loadLocaleData(locale, customMessages) {
  const matchedLocale = getMatchingLocaleString(locale);
  let messages;

  switch (matchedLocale) {
    case 'fr-FR':
      messages = await Promise.resolve().then(() => _interopRequireWildcard(require('../../i18n/fr-FR.yml')));
      break;

    default:
      messages = await Promise.resolve().then(() => _interopRequireWildcard(require('../../i18n/en-US.yml')));
      break;
  } // Merge custom strings into the standard language strings.


  const mergedMessages = { ...(0, _flat.default)(messages),
    // Override the predefined strings with the custom ones, if any provided.
    ...(0, _flat.default)(customMessages['allLanguages'] || {}),
    ...(0, _flat.default)(customMessages[locale] || {})
  };
  return mergedMessages;
}
/**
 * Gets the localization > defaultLocale configuration setting, or 'en-US' if not set.
 */


function getDefaultLocale(config) {
  const {
    localization = {}
  } = config;
  return localization.defaultLocale || 'en-US';
}
/**
 * Obtains the time format (12 or 24 hr) based on the redux user state.
 * FIXME: Remove after OTP-UI components can determine the time format on their own.
 */


function getTimeFormat(state) {
  var _state$user$loggedInU, _state$user$loggedInU2;

  const use24HourFormat = (_state$user$loggedInU = (_state$user$loggedInU2 = state.user.loggedInUser) === null || _state$user$loggedInU2 === void 0 ? void 0 : _state$user$loggedInU2.use24HourFormat) !== null && _state$user$loggedInU !== void 0 ? _state$user$loggedInU : false;
  return use24HourFormat ? 'H:mm' : 'h:mm a';
}

//# sourceMappingURL=i18n.js