"use strict";

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

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

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

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

var _LocaleProvider = require("../LocaleProvider");

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 isLikeText = node => {
  if (typeof node === 'string') {
    return true;
  }

  if ( /*#__PURE__*/(0, _react.isValidElement)(node) && node.type === Translate) {
    const {
      component,
      children
    } = node.props;

    if (component === 'span' && (typeof children === 'string' || typeof children === 'undefined' || children === null)) {
      return true;
    }
  }

  return false;
};
/**
 * `Translate` component.
 * @since 0.0.42
 */


exports.isLikeText = isLikeText;

const Translate = ({
  component: Tag,
  content,
  children,
  params,
  unsafe,
  fallback,
  namespace,
  translators,
  ...props
}) => {
  const translate = (0, _LocaleProvider.useTranslate)(namespace);
  const key = content || children;
  const translator = translators && translators[key];

  if (translator) {
    if (typeof translator === 'function') {
      return translator(params);
    }

    return translator;
  }

  const message = translate(key, params, fallback);

  if (unsafe) {
    props.dangerouslySetInnerHTML = {
      __html: message
    };
  } else {
    props.children = message;
  }

  return /*#__PURE__*/_react.default.createElement(Tag, props);
};

Translate.propTypes = {
  /**
   * A some prefix for the message key.
   * @since 2.6.0
   */
  namespace: _propTypes.default.string,

  /**
   * Translatable content or message key.
   * @since 0.0.42
   */
  content: _propTypes.default.string,

  /**
   * Message params
   * @since 0.0.42
   */
  params: _propTypes.default.object,

  /**
   * Fallback translation value.
   * @since 0.0.47
   */
  fallback: _propTypes.default.string,

  /**
   * Component to render as the root element. Useful when rendering a `Translate` as `<option>`.
   * @since 0.0.42
   */
  component: _propTypes.default.elementType,

  /**
   * A set of translation messages. If the set contains a message key, the component uses an item from the set for rendering.
   * @ignore
   */
  translators: _propTypes.default.objectOf(_propTypes.default.oneOfType([_propTypes.default.func, _propTypes.default.node])),

  /**
   * @ignore
   */
  unsafe: _propTypes.default.bool,

  /**
   * @ignore
   */
  children: _propTypes.default.string
};
Translate.defaultProps = {
  namespace: undefined,
  content: undefined,
  params: undefined,
  component: 'span',
  unsafe: undefined,
  fallback: undefined,
  children: undefined,
  translators: undefined
};
var _default = Translate;
exports.default = _default;