import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import { useCallback } from 'react';
import { useIntl } from 'react-intl';
import { some } from 'lodash/collection';
export function useLocalize() {
  var _useIntl = useIntl(),
      locale = _useIntl.locale,
      defaultLocale = _useIntl.defaultLocale;

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

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

  var getSomeTranslation = function getSomeTranslation(messages) {
    if (!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 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.js.map