'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 _immutable = require('immutable');

var _immutable2 = _interopRequireDefault(_immutable);

var _CheckboxInput = require('cspace-input/lib/components/CheckboxInput');

var _CheckboxInput2 = _interopRequireDefault(_CheckboxInput);

var _SelectBar = require('../../../styles/cspace-ui/SelectBar.css');

var _SelectBar2 = _interopRequireDefault(_SelectBar);

var _ButtonBar = require('../../../styles/cspace-ui/ButtonBar.css');

var _ButtonBar2 = _interopRequireDefault(_ButtonBar);

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

const messages = (0, _reactIntl.defineMessages)({
  selected: {
    'id': 'selectBar.selected',
    'defaultMessage': '{selectedItemCount, plural, =0 {0 selected} other {# selected}}'
  }
});

const isPageSelected = (items, selectedItems) => {
  const itemCount = items.size;
  const selectedItemCount = selectedItems ? selectedItems.size : 0;

  if (itemCount === 0 || selectedItemCount === 0) {
    return false;
  }

  if (itemCount === 1) {
    // Only one item on the page. Return its selected state.

    return selectedItems.has(items.first().get('csid'));
  }

  const firstItem = items.first();
  const remainingItems = items.shift();

  const basis = selectedItems.has(firstItem.get('csid'));

  if (remainingItems.find(item => selectedItems.has(item.get('csid')) !== basis)) {
    return null;
  }

  return basis;
};

const propTypes = {
  config: _propTypes2.default.object,
  buttons: _propTypes2.default.arrayOf(_propTypes2.default.node),
  listType: _propTypes2.default.string,
  searchDescriptor: _propTypes2.default.instanceOf(_immutable2.default.Map),
  searchName: _propTypes2.default.string,
  searchResult: _propTypes2.default.instanceOf(_immutable2.default.Map),
  selectedItems: _propTypes2.default.instanceOf(_immutable2.default.Map),
  setAllItemsSelected: _propTypes2.default.func,
  showCheckboxFilter: _propTypes2.default.func
};

class SelectBar extends _react.Component {
  constructor() {
    super();

    this.handleCheckboxCommit = this.handleCheckboxCommit.bind(this);
  }

  handleCheckboxCommit(path, value) {
    var _props = this.props;
    const config = _props.config,
          listType = _props.listType,
          searchName = _props.searchName,
          searchDescriptor = _props.searchDescriptor,
          setAllItemsSelected = _props.setAllItemsSelected,
          showCheckboxFilter = _props.showCheckboxFilter;


    if (setAllItemsSelected) {
      setAllItemsSelected(config, searchName, searchDescriptor, listType, value, showCheckboxFilter);
    }
  }

  render() {
    var _props2 = this.props;
    const config = _props2.config,
          listType = _props2.listType,
          searchResult = _props2.searchResult,
          selectedItems = _props2.selectedItems,
          buttons = _props2.buttons,
          showCheckboxFilter = _props2.showCheckboxFilter;


    if (!searchResult) {
      return null;
    }

    const listTypeConfig = config.listTypes[listType];
    const listNodeName = listTypeConfig.listNodeName,
          itemNodeName = listTypeConfig.itemNodeName;


    let items = searchResult.getIn([listNodeName, itemNodeName]);

    if (!items) {
      return null;
    }

    if (!_immutable2.default.List.isList(items)) {
      items = _immutable2.default.List.of(items);
    }

    if (showCheckboxFilter) {
      items = items.filter(showCheckboxFilter);
    }

    let buttonBar = null;

    if (buttons && buttons.length > 0) {
      buttonBar = _react2.default.createElement(
        'div',
        { className: _ButtonBar2.default.common },
        buttons
      );
    }

    const selectedItemCount = selectedItems ? selectedItems.size : 0;

    return _react2.default.createElement(
      'div',
      { className: _SelectBar2.default.common },
      _react2.default.createElement(_CheckboxInput2.default, {
        embedded: true,
        readOnly: items.size === 0,
        tristate: true,
        value: isPageSelected(items, selectedItems),
        onCommit: this.handleCheckboxCommit
      }),
      _react2.default.createElement(_reactIntl.FormattedMessage, _extends({}, messages.selected, { values: { selectedItemCount } })),
      buttonBar
    );
  }
}

exports.default = SelectBar;
SelectBar.propTypes = propTypes;