text: 'use strict';
  
  Object.defineProperty(exports, "__esModule", {
    value: true
  });
  
  var _fill2 = require('lodash/fill');
  
  var _fill3 = _interopRequireDefault(_fill2);
  
  var _find2 = require('lodash/find');
  
  var _find3 = _interopRequireDefault(_find2);
  
  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 _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  
  var _react = require('react');
  
  var _react2 = _interopRequireDefault(_react);
  
  var _propTypes = require('prop-types');
  
  var _propTypes2 = _interopRequireDefault(_propTypes);
  
  var _classnames = require('classnames');
  
  var _classnames2 = _interopRequireDefault(_classnames);
  
  var _reactInstantsearchCore = require('react-instantsearch-core');
  
  var _utils = require('../core/utils');
  
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  
  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) : subClass.__proto__ = superClass; }
  
  var cx = (0, _utils.createClassNames)('RatingMenu');
  
  var RatingMenu = function (_Component) {
    _inherits(RatingMenu, _Component);
  
    function RatingMenu() {
      _classCallCheck(this, RatingMenu);
  
      return _possibleConstructorReturn(this, (RatingMenu.__proto__ || Object.getPrototypeOf(RatingMenu)).apply(this, arguments));
    }
  
    _createClass(RatingMenu, [{
      key: 'onClick',
      value: function onClick(min, max, e) {
        e.preventDefault();
        e.stopPropagation();
        if (min === this.props.currentRefinement.min && max === this.props.currentRefinement.max) {
          this.props.refine({ min: this.props.min, max: this.props.max });
        } else {
          this.props.refine({ min: min, max: max });
        }
      }
    }, {
      key: 'buildItem',
      value: function buildItem(_ref) {
        var max = _ref.max,
            lowerBound = _ref.lowerBound,
            count = _ref.count,
            translate = _ref.translate,
            createURL = _ref.createURL,
            isLastSelectableItem = _ref.isLastSelectableItem;
  
        var disabled = !count;
        var isCurrentMinLower = this.props.currentRefinement.min < lowerBound;
        var selected = isLastSelectableItem && isCurrentMinLower || !disabled && lowerBound === this.props.currentRefinement.min && max === this.props.currentRefinement.max;
  
        var icons = [];
        var rating = 0;
        for (var icon = 0; icon < max; icon++) {
          if (icon < lowerBound) {
            rating++;
          }
          icons.push([_react2.default.createElement(
            'svg',
            {
              key: icon,
              className: cx('starIcon', icon >= lowerBound ? 'starIcon--empty' : 'starIcon--full'),
              'aria-hidden': 'true',
              width: '24',
              height: '24'
            },
            _react2.default.createElement('use', {
              xlinkHref: '#' + cx(icon >= lowerBound ? 'starEmptySymbol' : 'starSymbol')
            })
          ), ' ']);
        }
  
        // The last item of the list (the default item), should not
        // be clickable if it is selected.
        var isLastAndSelect = isLastSelectableItem && selected;
        var onClickHandler = disabled || isLastAndSelect ? {} : {
          href: createURL({ min: lowerBound, max: max }),
          onClick: this.onClick.bind(this, lowerBound, max)
        };
  
        return _react2.default.createElement(
          'li',
          {
            key: lowerBound,
            className: cx('item', selected && 'item--selected', disabled && 'item--disabled')
          },
          _react2.default.createElement(
            'a',
            _extends({
              className: cx('link'),
              'aria-label': '' + rating + translate('ratingLabel')
            }, onClickHandler),
            icons,
            _react2.default.createElement(
              'span',
              { className: cx('label'), 'aria-hidden': 'true' },
              translate('ratingLabel')
            ),
            ' ',
            _react2.default.createElement(
              'span',
              { className: cx('count') },
              count
            )
          )
        );
      }
    }, {
      key: 'render',
      value: function render() {
        var _this2 = this;
  
        var _props = this.props,
            min = _props.min,
            max = _props.max,
            translate = _props.translate,
            count = _props.count,
            createURL = _props.createURL,
            canRefine = _props.canRefine,
            className = _props.className;
  
        // min & max are always set when there is a results, otherwise it means
        // that we don't want to render anything since we don't have any values.
  
        var limitMin = min !== undefined && min >= 0 ? min : 1;
        var limitMax = max !== undefined && max >= 0 ? max : 0;
        var inclusiveLength = limitMax - limitMin + 1;
        var safeInclusiveLength = Math.max(inclusiveLength, 0);
  
        var values = count.map(function (item) {
          return _extends({}, item, { value: parseFloat(item.value) });
        }).filter(function (item) {
          return item.value >= limitMin && item.value <= limitMax;
        });
  
        var range = (0, _fill3.default)(new Array(safeInclusiveLength), null).map(function (_, index) {
          var element = (0, _find3.default)(values, function (item) {
            return item.value === limitMax - index;
          });
          var placeholder = { value: limitMax - index, count: 0, total: 0 };
  
          return element || placeholder;
        }).reduce(function (acc, item, index) {
          return acc.concat(_extends({}, item, {
            total: index === 0 ? item.count : acc[index - 1].total + item.count
          }));
        }, []);
  
        var items = range.map(function (item, index) {
          return _this2.buildItem({
            lowerBound: item.value,
            count: item.total,
            isLastSelectableItem: range.length - 1 === index,
            max: limitMax,
            translate: translate,
            createURL: createURL
          });
        });
  
        return _react2.default.createElement(
          'div',
          {
            className: (0, _classnames2.default)(cx('', !canRefine && '-noRefinement'), className)
          },
          _react2.default.createElement(
            'svg',
            { xmlns: 'http://www.w3.org/2000/svg', style: { display: 'none' } },
            _react2.default.createElement(
              'symbol',
              { id: cx('starSymbol'), viewBox: '0 0 24 24' },
              _react2.default.createElement('path', { d: 'M12 .288l2.833 8.718h9.167l-7.417 5.389 2.833 8.718-7.416-5.388-7.417 5.388 2.833-8.718-7.416-5.389h9.167z' })
            ),
            _react2.default.createElement(
              'symbol',
              { id: cx('starEmptySymbol'), viewBox: '0 0 24 24' },
              _react2.default.createElement('path', { d: 'M12 6.76l1.379 4.246h4.465l-3.612 2.625 1.379 4.246-3.611-2.625-3.612 2.625 1.379-4.246-3.612-2.625h4.465l1.38-4.246zm0-6.472l-2.833 8.718h-9.167l7.416 5.389-2.833 8.718 7.417-5.388 7.416 5.388-2.833-8.718 7.417-5.389h-9.167l-2.833-8.718z' })
            )
          ),
          _react2.default.createElement(
            'ul',
            { className: cx('list', !canRefine && 'list--noRefinement') },
            items
          )
        );
      }
    }]);
  
    return RatingMenu;
  }(_react.Component);
  
  RatingMenu.propTypes = {
    translate: _propTypes2.default.func.isRequired,
    refine: _propTypes2.default.func.isRequired,
    createURL: _propTypes2.default.func.isRequired,
    min: _propTypes2.default.number,
    max: _propTypes2.default.number,
    currentRefinement: _propTypes2.default.shape({
      min: _propTypes2.default.number,
      max: _propTypes2.default.number
    }),
    count: _propTypes2.default.arrayOf(_propTypes2.default.shape({
      value: _propTypes2.default.string,
      count: _propTypes2.default.number
    })),
    canRefine: _propTypes2.default.bool.isRequired,
    className: _propTypes2.default.string
  };
  RatingMenu.defaultProps = {
    className: ''
  };
  exports.default = (0, _reactInstantsearchCore.translatable)({
    ratingLabel: ' & Up'
  })(RatingMenu);