'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _stringify = require('babel-runtime/core-js/json/stringify');

var _stringify2 = _interopRequireDefault(_stringify);

var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');

var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _recompose = require('recompose');

var _theme = require('../../theme');

var _theme2 = _interopRequireDefault(_theme);

var _styledComponents = require('styled-components');

var _styledComponents2 = _interopRequireDefault(_styledComponents);

var _ToggleButton = require('../ToggleButton');

var _ToggleButton2 = _interopRequireDefault(_ToggleButton);

var _BulkSelectBorder = require('./styles/BulkSelectBorder');

var _BulkSelectBorder2 = _interopRequireDefault(_BulkSelectBorder);

var _BulkSelectDivider = require('./styles/BulkSelectDivider');

var _BulkSelectDivider2 = _interopRequireDefault(_BulkSelectDivider);

var _BulkSelectItemContainer = require('./styles/BulkSelectItemContainer');

var _BulkSelectItemContainer2 = _interopRequireDefault(_BulkSelectItemContainer);

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

/**
 * @deprecated BulkSelect has been deprecated in favor of using [Grid](/#!/Grid) with
 * [ToggleButton](/#!/ToggleButton), optionally with [Selectable](/#!/Selectable) and/or [DragBox](/#!/DragBox).
 */
var BulkSelect = function (_React$Component) {
  (0, _inherits3.default)(BulkSelect, _React$Component);

  function BulkSelect() {
    var _ref;

    var _temp, _this, _ret;

    (0, _classCallCheck3.default)(this, BulkSelect);

    for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, (_ref = BulkSelect.__proto__ || (0, _getPrototypeOf2.default)(BulkSelect)).call.apply(_ref, [this].concat(args))), _this), _this.createItemClickHandler = function (item) {
      return function () {
        if (item.selected) {
          _this.props.onItemDeselected(item.data);
        } else {
          _this.props.onItemSelected(item.data);
        }
      };
    }, _this.renderSection = function (section) {
      var _this$props = _this.props,
          Divider = _this$props.Divider,
          Item = _this$props.Item,
          ItemContainer = _this$props.ItemContainer,
          computeItemKey = _this$props.computeItemKey,
          renderItem = _this$props.renderItem,
          renderItemContents = _this$props.renderItemContents;


      var resolvedRenderItemContents = renderItemContents || renderItem;

      return _react2.default.createElement(
        'div',
        { key: section.title || 'all' },
        section.title && _react2.default.createElement(
          Divider,
          null,
          _react2.default.createElement(
            'h3',
            null,
            section.title
          ),
          _react2.default.createElement('span', null)
        ),
        _react2.default.createElement(
          ItemContainer,
          null,
          section.items.map(function (item) {
            return _react2.default.createElement(
              Item,
              {
                key: computeItemKey(item.data),
                selected: item.selected,
                disabled: item.disabled,
                onClick: _this.createItemClickHandler(item)
              },
              resolvedRenderItemContents(item.data, item.selected)
            );
          })
        )
      );
    }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  }

  (0, _createClass3.default)(BulkSelect, [{
    key: 'render',
    value: function render() {
      var _props = this.props,
          sections = _props.sections,
          Border = _props.Border;


      return _react2.default.createElement(
        Border,
        null,
        sections.map(this.renderSection)
      );
    }
  }]);
  return BulkSelect;
}(_react2.default.Component);

BulkSelect.propTypes = {
  sections: _propTypes2.default.arrayOf(_propTypes2.default.shape({
    title: _propTypes2.default.string,
    items: _propTypes2.default.arrayOf(_propTypes2.default.shape({
      selected: _propTypes2.default.bool.isRequired,
      data: _propTypes2.default.any.isRequired
    })).isRequired
  })),
  /**
   * DEPRECATED: see renderItemContents
   */
  renderItem: _propTypes2.default.func,
  /**
   * Renders the node inside an item. Passed (item, isSelected).
   */
  renderItemContents: _propTypes2.default.func,
  /**
   * Should return a unique key for an item
   */
  computeItemKey: _propTypes2.default.func,
  onItemSelected: _propTypes2.default.func,
  onItemDeselected: _propTypes2.default.func,
  /**
   * A component to render an item. Receives 'selected' prop.
   */
  Item: _propTypes2.default.func,
  /**
   * A component to render the border box around the BulkSelect
   */
  Border: _propTypes2.default.func,
  /**
   * A component to render the divider between sections
   */
  Divider: _propTypes2.default.func,
  /**
   * A component to render the element that contains items
   */
  ItemContainer: _propTypes2.default.func
};
BulkSelect.defaultProps = {
  sections: [],
  computeItemKey: function computeItemKey(item) {
    return item.get ? item.get('id') : item.id || (0, _stringify2.default)(item);
  },
  onItemSelected: function onItemSelected() {
    return null;
  },
  onItemDeselected: function onItemDeselected() {
    return null;
  },
  Item: _ToggleButton2.default,
  Border: _BulkSelectBorder2.default,
  Divider: _BulkSelectDivider2.default,
  ItemContainer: _BulkSelectItemContainer2.default
};


BulkSelect.Small = (0, _recompose.withProps)({ ItemContainer: _BulkSelectItemContainer2.default.Small })(BulkSelect);

BulkSelect.Colorful = (0, _recompose.withProps)({ Item: _ToggleButton2.default.Colorful })(BulkSelect);

exports.default = BulkSelect;