'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 _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 }; }

const messages = (0, _reactIntl.defineMessages)({
  pageSize: {
    'id': 'pageSizeChooser.pageSize',
    'defaultMessage': '{pageSize} per page'
  }
});

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

const defaultProps = {
  pageSizeOptionListName: 'searchResultPagePageSizes'
};

class PageSizeChooser extends _react.Component {
  constructor(props) {
    super(props);

    this.handleCommit = this.handleCommit.bind(this);
    this.handleAddOption = this.handleAddOption.bind(this);
  }

  handleAddOption(label) {
    const onPageSizeChange = this.props.onPageSizeChange;


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

      if (!isNaN(value) && value > 0) {
        onPageSizeChange(value);
      }
    }
  }

  handleCommit(path, value) {
    var _props = this.props;
    const pageSize = _props.pageSize,
          onPageSizeChange = _props.onPageSizeChange;


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

      if (newPageSize !== pageSize) {
        onPageSizeChange(newPageSize);
      }
    }
  }

  render() {
    var _props2 = this.props;
    const embedded = _props2.embedded,
          pageSize = _props2.pageSize,
          pageSizeOptionListName = _props2.pageSizeOptionListName;


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

    const 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 }
      }))
    );
  }
}

exports.default = PageSizeChooser;
PageSizeChooser.propTypes = propTypes;
PageSizeChooser.defaultProps = defaultProps;