'use strict';

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

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 _reactIntl = require('react-intl');

var _ComboBoxInputContainer = require('../../containers/input/ComboBoxInputContainer');

var _ComboBoxInputContainer2 = _interopRequireDefault(_ComboBoxInputContainer);

var _PageSizeChooser = require('../../../styles/cspace-ui/PageSizeChooser.css');

var _PageSizeChooser2 = _interopRequireDefault(_PageSizeChooser);

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 messages = (0, _reactIntl.defineMessages)({
  pageSize: {
    'id': 'pageSizeChooser.pageSize',
    'defaultMessage': '{pageSize} per page'
  }
});

var propTypes = {
  pageSize: _propTypes2.default.number.isRequired,
  embedded: _propTypes2.default.bool,
  pageSizeOptionListName: _propTypes2.default.string,
  onPageSizeChange: _propTypes2.default.func
};

var defaultProps = {
  pageSizeOptionListName: 'searchResultPagePageSizes'
};

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

  function PageSizeChooser(props) {
    _classCallCheck(this, PageSizeChooser);

    var _this = _possibleConstructorReturn(this, (PageSizeChooser.__proto__ || Object.getPrototypeOf(PageSizeChooser)).call(this, props));

    _this.handleCommit = _this.handleCommit.bind(_this);
    _this.handleAddOption = _this.handleAddOption.bind(_this);
    return _this;
  }

  _createClass(PageSizeChooser, [{
    key: 'handleAddOption',
    value: function handleAddOption(label) {
      var onPageSizeChange = this.props.onPageSizeChange;


      if (onPageSizeChange) {
        var value = parseInt(label, 10);

        if (!isNaN(value) && value > 0) {
          onPageSizeChange(value);
        }
      }
    }
  }, {
    key: 'handleCommit',
    value: function handleCommit(path, value) {
      var _props = this.props,
          pageSize = _props.pageSize,
          onPageSizeChange = _props.onPageSizeChange;


      if (onPageSizeChange) {
        var newPageSize = parseInt(value, 10);

        if (newPageSize !== pageSize) {
          onPageSizeChange(newPageSize);
        }
      }
    }
  }, {
    key: 'render',
    value: function render() {
      var _props2 = this.props,
          embedded = _props2.embedded,
          pageSize = _props2.pageSize,
          pageSizeOptionListName = _props2.pageSizeOptionListName;


      var value = isNaN(pageSize) ? '' : pageSize.toString();

      var chooser = _react2.default.createElement(_ComboBoxInputContainer2.default, {
        embedded: embedded,
        source: pageSizeOptionListName,
        value: value,
        onAddOption: this.handleAddOption,
        onCommit: this.handleCommit
      });

      return _react2.default.createElement(
        'div',
        { className: _PageSizeChooser2.default.common },
        _react2.default.createElement(_reactIntl.FormattedMessage, _extends({}, messages.pageSize, {
          tagName: 'div',
          values: { pageSize: chooser }
        }))
      );
    }
  }]);

  return PageSizeChooser;
}(_react.Component);

exports.default = PageSizeChooser;


PageSizeChooser.propTypes = propTypes;
PageSizeChooser.defaultProps = defaultProps;