"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

exports.__esModule = true;
exports.translate = translate;
exports.default = void 0;

var _formatMessage = _interopRequireDefault(require("format-message"));

var config = _interopRequireWildcard(require("./config"));

var constants = _interopRequireWildcard(require("./constants"));

var defaultBrandId = '0';
var icuTemplates = {
  en_US: "=0 '{'{other}'}' =1'{'{one}'}' other'{'{other}'}'",
  en_ZZ: "=0 '{'{other}'}' =1'{'{one}'}' other'{'{other}'}'",
  en_GB: "=0 '{'{other}'}' =1'{'{one}'}' other'{'{other}'}'",
  en_AU: "=0 '{'{other}'}' =1'{'{one}'}' other'{'{other}'}'",
  fr_CA: "=0 '{'{one}'}' =1'{'{one}'}' other'{'{other}'}'",
  fr_FR: "=0 '{'{one}'}' =1'{'{one}'}' other'{'{other}'}'",
  de_DE: "=0 '{'{other}'}' =1'{'{one}'}' other'{'{other}'}'",
  es_ES: "=0 '{'{other}'}' =1'{'{one}'}' other'{'{other}'}'",
  it_IT: "=0 '{'{other}'}' =1'{'{one}'}' other'{'{other}'}'",
  es_419: "=0 '{'{other}'}' =1'{'{one}'}' other'{'{other}'}'",
  ja_JP: "=0 '{'{one}'}' =1'{'{one}'}' other'{'{one}'}'",
  pt_BR: "=0 '{'{one}'}' =1'{'{one}'}' other'{'{other}'}'",
  zh_CN: "=0 '{'{other}'}' =1'{'{other}'}' other'{'{other}'}'",
  zh_TW: "=0 '{'{other}'}' =1'{'{other}'}' other'{'{other}'}'",
  zh_HK: "=0 '{'{other}'}' =1'{'{other}'}' other'{'{other}'}'"
};

function isBrandedMessageResource(obj) {
  return isValidKeysByReg(/^0$|^\d{4}$|^\d{2}$/, obj);
}

function isPluralMessageResource(obj) {
  return isValidKeysByReg(/^one$|^other$|^many$/, obj);
}

function isValidKeysByReg(reg, obj) {
  var keys = Object.keys(obj);
  var hasInvalidKey = Object.keys(obj).some(function (key) {
    return !reg.test(key);
  });
  return keys.length > 0 && !hasInvalidKey;
}

function covertMessageResourceToICUString(obj) {
  var icuMsgFormat = icuTemplates[config.getConfig().userLanguage] || icuTemplates.en_US;
  var msg = (0, _formatMessage.default)(icuMsgFormat, obj);
  return '{count, plural, ' + msg + '}';
}
/**
 * TODO rename. actually is formatMessage not a translate
 * @param {string|object} str
 * @param {object} [args]
 * @return {string}
 */


function translate(str, args) {
  if (!str) {
    return constants.notTranslatedPrefix;
  }

  if (typeof str === 'object') {
    if (isBrandedMessageResource(str)) {
      str = str[config.getConfig().brandId] || str[defaultBrandId];
    } else if (isPluralMessageResource(str)) {
      //TODO Support both brand and plural at the same time
      var errorMessage = 'You are using translate() to handle plural strings, args should be an object like {count:1} or a number.';

      if (!args && args !== 0) {
        throw new Error(errorMessage);
      }

      if (!isNaN(args)) {
        args = {
          count: args
        };
      }

      if (!args.hasOwnProperty('count')) {
        throw new Error(errorMessage);
      }

      str = covertMessageResourceToICUString(str);
    } else {
      throw new Error('Object ' + JSON.stringify(str) + ' is not a valid message resource.');
    }
  }

  if (str.indexOf(constants.notTranslatedPrefix + ':') === 0) {
    str = str.replace(constants.notTranslatedPrefix + ':', '');
  }

  return (0, _formatMessage.default)(str, args);
}

var _default = translate;
exports.default = _default;