'use strict';

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

var _from = require('babel-runtime/core-js/array/from');

var _from2 = _interopRequireDefault(_from);

var _set = require('babel-runtime/core-js/set');

var _set2 = _interopRequireDefault(_set);

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 _styledComponents = require('styled-components');

var _styledComponents2 = _interopRequireDefault(_styledComponents);

var _lodash = require('lodash');

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

var Selectable = function (_React$Component) {
  (0, _inherits3.default)(Selectable, _React$Component);

  function Selectable(props) {
    (0, _classCallCheck3.default)(this, Selectable);

    var _this = (0, _possibleConstructorReturn3.default)(this, (Selectable.__proto__ || (0, _getPrototypeOf2.default)(Selectable)).call(this, props));

    _initialiseProps.call(_this);

    var selected = new _set2.default();
    if (props.initial) selected.add(props.initial);
    _this.state = { selected: selected };
    return _this;
  }

  (0, _createClass3.default)(Selectable, [{
    key: 'render',
    value: function render() {
      var render = this.props.render,
          selected = this.state.selected,
          selectItem = this.selectItem,
          deselectItem = this.deselectItem,
          toggleItem = this.toggleItem;

      return render({
        selectItem: selectItem,
        deselectItem: deselectItem,
        toggleItem: toggleItem,
        selected: selected
      });
    }
  }]);
  return Selectable;
}(_react2.default.Component);

Selectable.propTypes = {
  /**
   * Render prop to render the selectable items. Supports the following props:
   *
   * *selectItem*: A function that should be invoked with a unique key as its argument whenever an item should be selected.
   *
   * *deselectItem*: A function that should be invoked with a unique key as its argument whenever an item should be deselected.
   *
   * *toggleItem*: A function that should be invoked with a unique key as its argument whenever an item should be toggled.
   *
   * *selected*: A **Set** of selected items represented by their key values.
   */
  render: _propTypes2.default.func,
  /**
   * Callback for when an item is selected. Passed one argument, which is the key of the item that was selected.
   */
  onItemSelected: _propTypes2.default.func,
  /**
   * Callback for when an item is deselected. Passed one argument, which is the key of the item that was deselected.
   */
  onItemDeselected: _propTypes2.default.func,
  /**
   * Limits selection to only one item at a time.
   */
  exclusive: _propTypes2.default.bool,
  /**
   * Control the inital selection.
   */
  initial: _propTypes2.default.object
};
Selectable.defaultProps = {
  render: _lodash.noop,
  onItemSelected: _lodash.noop,
  onItemDeselected: _lodash.noop
};

var _initialiseProps = function _initialiseProps() {
  var _this2 = this;

  this.selectItem = function (key) {
    var _props = _this2.props,
        onItemSelected = _props.onItemSelected,
        exclusive = _props.exclusive,
        selected = _this2.state.selected,
        deselectItem = _this2.deselectItem;

    if (selected.has(key)) {
      return;
    }
    if (exclusive) {
      (0, _from2.default)(selected).map(deselectItem);
    }
    onItemSelected(key);
    _this2.setState(function (state) {
      return {
        selected: state.selected.add(key)
      };
    });
  };

  this.deselectItem = function (key) {
    var onItemDeselected = _this2.props.onItemDeselected,
        selected = _this2.state.selected;

    if (!selected.has(key)) {
      return;
    }
    onItemDeselected(key);
    _this2.setState(function (state) {
      state.selected.delete(key);
      return { selected: state.selected };
    });
  };

  this.toggleItem = function (key) {
    var selected = _this2.state.selected,
        selectItem = _this2.selectItem,
        deselectItem = _this2.deselectItem;

    selected.has(key) ? deselectItem(key) : selectItem(key);
  };
};

exports.default = Selectable;