"use strict";

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useLocalize = useLocalize;

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

var _react = require("react");

var _reactIntl = require("react-intl");

var _collection = require("lodash/collection");

function useLocalize() {
  var _useIntl = (0, _reactIntl.useIntl)(),
      locale = _useIntl.locale,
      defaultLocale = _useIntl.defaultLocale;

  var _locale$split = locale.split('-'),
      _locale$split2 = (0, _slicedToArray2["default"])(_locale$split, 1),
      lang = _locale$split2[0];

  var _defaultLocale$split = defaultLocale.split('-'),
      _defaultLocale$split2 = (0, _slicedToArray2["default"])(_defaultLocale$split, 1),
      defaultLang = _defaultLocale$split2[0];

  var getSomeTranslation = function getSomeTranslation(messages) {
    if (!(0, _collection.some)(messages)) {
      return null;
    }

    var someLang;

    Object.filter = function (obj, predicate) {
      return Object.keys(obj).filter(function (key) {
        return predicate(obj[key]);
      }).reduce(function (res, key) {
        return res[key] = obj[key], res;
      }, {});
    };

    var newMessages = Object.filter(messages, function (value) {
      return value !== "";
    });
    someLang = Object.getOwnPropertyNames(newMessages).toString();
    return someLang.length > 2 ? messages[someLang.slice(0, 2)] : messages[someLang];
  };

  return (0, _react.useCallback)(function () {
    var _ref, _ref2, _messages$lang;

    var messages = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
    return (_ref = (_ref2 = (_messages$lang = messages[lang]) !== null && _messages$lang !== void 0 ? _messages$lang : messages[defaultLang]) !== null && _ref2 !== void 0 ? _ref2 : getSomeTranslation(messages)) !== null && _ref !== void 0 ? _ref : '';
  }, [defaultLang, lang]);
}
//# sourceMappingURL=useLocalize.cjs.map