"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 = require("react");
var _propTypes = _interopRequireDefault(require("prop-types"));
var _jsxRuntime = require("react/jsx-runtime");
// Copyright 1999-2023. Plesk International GmbH. All rights reserved.

const applyParams = function (message) {
  let params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  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
function (key) {
  let params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  let fallback = arguments.length > 2 ? arguments[2] : undefined;
  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 (0, _react.useCallback)((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}]`;
  }, [context, namespace]);
};
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__*/(0, _jsxRuntime.jsx)(LocaleContext.Provider, {
      value: this.getChildContext(),
      children: children
    });
  }
}
(0, _defineProperty2.default)(LocaleProvider, "childContextTypes", {
  locale: _propTypes.default.object
});
var _default = exports.default = LocaleProvider;