'use strict';

exports.__esModule = true;

var _get2 = require('lodash/get');

var _get3 = _interopRequireDefault(_get2);

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _react = require('react');

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _recompose = require('recompose');

var _en_GB = require('./locales/en_GB');

var _en_GB2 = _interopRequireDefault(_en_GB);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _defaults(obj, defaults) { var keys = Object.getOwnPropertyNames(defaults); for (var i = 0; i < keys.length; i++) { var key = keys[i]; var value = Object.getOwnPropertyDescriptor(defaults, key); if (value && value.configurable && obj[key] === undefined) { Object.defineProperty(obj, key, value); } } return obj; }

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : _defaults(subClass, superClass); }

var mergeObject = function mergeObject(list) {
  return list.reduce(function (a, b) {
    a = _extends({}, a, b);
    return a;
  }, {});
};

var withLocale = function withLocale() {
  var combineKeys = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  return function (BaseComponent) {
    var factory = (0, _react.createFactory)(BaseComponent);

    var WithLocale = function (_Component) {
      _inherits(WithLocale, _Component);

      function WithLocale() {
        _classCallCheck(this, WithLocale);

        return _possibleConstructorReturn(this, _Component.apply(this, arguments));
      }

      WithLocale.prototype.render = function render() {
        var _props = this.props,
            innerRef = _props.innerRef,
            rest = _objectWithoutProperties(_props, ['innerRef']);

        var messages = this.context.rsuiteLocale || _en_GB2.default;
        var locales = combineKeys.map(function (key) {
          return (0, _get3.default)(messages, '' + key);
        });

        return factory(_extends({
          ref: innerRef,
          locale: mergeObject(locales)
        }, rest));
      };

      return WithLocale;
    }(_react.Component);

    WithLocale.contextTypes = {
      rsuiteLocale: _propTypes2.default.object
    };
    WithLocale.handledProps = [];


    if (process.env.NODE_ENV !== 'production') {
      return (0, _recompose.setDisplayName)((0, _recompose.wrapDisplayName)(BaseComponent, 'withLocale'))(WithLocale);
    }

    return WithLocale;
  };
};

exports.default = withLocale;