"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _react = _interopRequireWildcard(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _reactIntl = require("react-intl");

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

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

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

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _extends() { _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; }; return _extends.apply(this, arguments); }

const messages = (0, _reactIntl.defineMessages)({
  pageSize: {
    "id": "pageSizeChooser.pageSize",
    "defaultMessage": "{pageSize} per page"
  }
});
const propTypes = {
  pageSize: _propTypes.default.number.isRequired,
  embedded: _propTypes.default.bool,
  pageSizeOptionListName: _propTypes.default.string,
  onPageSizeChange: _propTypes.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;

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

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

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

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

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

  render() {
    const {
      embedded,
      pageSize,
      pageSizeOptionListName
    } = this.props;
    const value = isNaN(pageSize) ? '' : pageSize.toString();

    const chooser = _react.default.createElement(_ComboBoxInputContainer.default, {
      embedded: embedded,
      source: pageSizeOptionListName,
      value: value,
      onAddOption: this.handleAddOption,
      onCommit: this.handleCommit
    });

    return _react.default.createElement("div", {
      className: _PageSizeChooser.default.common
    }, _react.default.createElement(_reactIntl.FormattedMessage, _extends({}, messages.pageSize, {
      tagName: "div",
      values: {
        pageSize: chooser
      }
    })));
  }

}

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