"use strict";

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useTranslate = exports.default = void 0;

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

var _react = _interopRequireWildcard(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && 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; }

// Copyright 1999-2020. Plesk International GmbH. All rights reserved.
const applyParams = (message, params = {}) => {
  const result = [];
  let hasElements = false;
  let m;

  while (m = message.match(/%%(\w+)%%/)) {
    const {
      index = 0
    } = m;

    if (index > 0) {
      result.push(message.substr(0, index));
    }

    const param = params[m[1]];

    if (param === undefined) {
      result.push(m[0]);
    } else if (typeof param === 'object' && /*#__PURE__*/(0, _react.isValidElement)(param)) {
      result.push( /*#__PURE__*/(0, _react.cloneElement)(param, {
        key: m[1]
      }));
      hasElements = true;
    } else {
      result.push(param);
    }

    message = message.substr(index + m[0].length);
  }

  if (message !== '') {
    result.push(message);
  }

  return hasElements ? result : result.join('');
};

const getMessage = (fullKey, messages) => {
  const [key, ...rest] = fullKey.split('.');
  const message = messages[key];

  if (typeof message === 'object' && rest.length) {
    return getMessage(rest.join('.'), message);
  }

  return messages[fullKey];
};

const createTranslate = messages => // eslint-disable-next-line default-param-last
(key, params = {}, fallback) => {
  if (typeof key !== 'string') {
    return '';
  }

  let message = getMessage(key, messages);

  if (typeof message !== 'string') {
    message = typeof fallback === 'string' ? fallback : `[${key}]`;
  }

  return applyParams(message, params);
};

const LocaleContext = /*#__PURE__*/(0, _react.createContext)(undefined);

const useTranslate = namespace => {
  const context = (0, _react.useContext)(LocaleContext);
  return (key, params, fallback) => {
    if (context) {
      const {
        locale: {
          lmsg
        }
      } = context;
      return lmsg(namespace ? `${namespace}.${key}` : key, params, fallback);
    }

    return typeof fallback === 'string' ? applyParams(fallback, params) : `[${key}]`;
  };
};

exports.useTranslate = useTranslate;

/**
 * LocaleProvider provides a uniform localization support for built-in text of components.
 * @since 0.0.42
 */
class LocaleProvider extends _react.PureComponent {
  getChildContext() {
    const {
      messages
    } = this.props;
    return {
      locale: {
        lmsg: createTranslate(messages)
      }
    };
  }

  render() {
    const {
      children
    } = this.props;
    return /*#__PURE__*/_react.default.createElement(LocaleContext.Provider, {
      value: this.getChildContext()
    }, children);
  }

}

(0, _defineProperty2.default)(LocaleProvider, "childContextTypes", {
  locale: _propTypes.default.object
});
var _default = LocaleProvider;
exports.default = _default;