"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 _immutable = _interopRequireDefault(require("immutable"));
var _CheckboxInput = _interopRequireDefault(require("cspace-input/lib/components/CheckboxInput"));
var _SelectBar = _interopRequireDefault(require("../../../styles/cspace-ui/SelectBar.css"));
var _ButtonBar = _interopRequireDefault(require("../../../styles/cspace-ui/ButtonBar.css"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && 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 ? Object.assign.bind() : 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)({
  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: _propTypes.default.shape({
    listTypes: _propTypes.default.object
  }),
  buttons: _propTypes.default.arrayOf(_propTypes.default.node),
  listType: _propTypes.default.string,
  searchDescriptor: _propTypes.default.instanceOf(_immutable.default.Map),
  searchName: _propTypes.default.string,
  searchResult: _propTypes.default.instanceOf(_immutable.default.Map),
  selectedItems: _propTypes.default.instanceOf(_immutable.default.Map),
  setAllItemsSelected: _propTypes.default.func,
  showCheckboxFilter: _propTypes.default.func
};
class SelectBar extends _react.Component {
  constructor() {
    super();
    this.handleCheckboxCommit = this.handleCheckboxCommit.bind(this);
  }
  handleCheckboxCommit(path, value) {
    const {
      config,
      listType,
      searchName,
      searchDescriptor,
      setAllItemsSelected,
      showCheckboxFilter
    } = this.props;
    if (setAllItemsSelected) {
      setAllItemsSelected(config, searchName, searchDescriptor, listType, value, showCheckboxFilter);
    }
  }
  render() {
    const {
      config,
      listType,
      searchResult,
      selectedItems,
      buttons,
      showCheckboxFilter
    } = this.props;
    if (!searchResult) {
      return null;
    }
    const listTypeConfig = config.listTypes[listType];
    const {
      listNodeName,
      itemNodeName
    } = listTypeConfig;
    let items = searchResult.getIn([listNodeName, itemNodeName]);
    if (!items) {
      return null;
    }
    if (!_immutable.default.List.isList(items)) {
      items = _immutable.default.List.of(items);
    }
    if (showCheckboxFilter) {
      items = items.filter(showCheckboxFilter);
    }
    let buttonBar = null;
    if (buttons && buttons.length > 0) {
      buttonBar = /*#__PURE__*/_react.default.createElement("div", {
        className: _ButtonBar.default.common
      }, buttons);
    }
    const selectedItemCount = selectedItems ? selectedItems.size : 0;
    return /*#__PURE__*/_react.default.createElement("div", {
      className: _SelectBar.default.common
    }, /*#__PURE__*/_react.default.createElement(_CheckboxInput.default, {
      embedded: true,
      readOnly: items.size === 0,
      transition: {
        null: false,
        true: false,
        false: true
      },
      value: isPageSelected(items, selectedItems),
      onCommit: this.handleCheckboxCommit
    }), /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, _extends({}, messages.selected, {
      values: {
        selectedItemCount
      }
    })), buttonBar);
  }
}
exports.default = SelectBar;
SelectBar.propTypes = propTypes;