import { _ as _applyDecoratedDescriptor, b as _defineProperty } from '../_rollupPluginBabelHelpers-9wjJaosZ.js';
import { action } from '@ember/object';
import Service from '@ember/service';
import { processStringToChildren } from '../i18n/util.js';

var _class;
function flatten(acc = {}, obj, path = '') {
  path = path.length > 0 ? path + '.' : path;
  for (const key in obj) {
    if (typeof obj[key] === 'object' && obj[key] !== null) {
      flatten(acc, obj[key], path + key);
    } else {
      acc[path + key] = obj[key];
    }
  }
  return acc;
}
let EuiI18n = (_class = class EuiI18n extends Service {
  constructor(...args) {
    super(...args);
    _defineProperty(this, "translations", {});
  }
  addTranslations(translations) {
    // TODO: deep merge?
    this.translations = {
      ...this.translations,
      ...flatten({}, translations)
    };
  }
  get mapping() {
    return this.translations;
  }
  lookupToken(token, valueDefault, values) {
    return this._lookupToken({
      token,
      valueDefault,
      values
    });
  }
  getTokenFromMapping(token, valueDefault, _values, i18nMapping) {
    return i18nMapping && i18nMapping[token] || valueDefault;
  }
  _lookupToken(options) {
    const {
      token,
      i18nMapping = this.mapping,
      valueDefault,
      values = {}
    } = options;
    let renderable = this.getTokenFromMapping(token, valueDefault, values, i18nMapping);
    if (typeof renderable === 'function') {
      renderable = renderable(values);
    }
    const children = processStringToChildren(renderable, values);
    return children;
  }
}, (_applyDecoratedDescriptor(_class.prototype, "addTranslations", [action], Object.getOwnPropertyDescriptor(_class.prototype, "addTranslations"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "lookupToken", [action], Object.getOwnPropertyDescriptor(_class.prototype, "lookupToken"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "_lookupToken", [action], Object.getOwnPropertyDescriptor(_class.prototype, "_lookupToken"), _class.prototype)), _class);

export { EuiI18n as default };
//# sourceMappingURL=eui-i18n.js.map
