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

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)({
  selected: {
    'id': 'selectBar.selected',
    'defaultMessage': '{selectedItemCount, plural, =0 {0 selected} other {# selected}}'
  }
});

var isPageSelected = function isPageSelected(items, selectedItems) {
  var itemCount = items.size;
  var 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'));
  }

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

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

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

  return basis;
};

var 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
};

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

  function SelectBar() {
    _classCallCheck(this, SelectBar);

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

    _this.handleCheckboxCommit = _this.handleCheckboxCommit.bind(_this);
    return _this;
  }

  _createClass(SelectBar, [{
    key: 'handleCheckboxCommit',
    value: function handleCheckboxCommit(path, value) {
      var _props = this.props,
          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);
      }
    }
  }, {
    key: 'render',
    value: function render() {
      var _props2 = this.props,
          config = _props2.config,
          listType = _props2.listType,
          searchResult = _props2.searchResult,
          selectedItems = _props2.selectedItems,
          buttons = _props2.buttons,
          showCheckboxFilter = _props2.showCheckboxFilter;


      if (!searchResult) {
        return null;
      }

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


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

      var buttonBar = null;

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

      var 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: selectedItemCount } })),
        buttonBar
      );
    }
  }]);

  return SelectBar;
}(_react.Component);

exports.default = SelectBar;


SelectBar.propTypes = propTypes;