'use strict';

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

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

var _styledComponents2 = _interopRequireDefault(_styledComponents);

var _lodash = require('lodash');

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 Selectable = function (_React$Component) {
  _inherits(Selectable, _React$Component);

  function Selectable(props) {
    _classCallCheck(this, Selectable);

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

    _initialiseProps.call(_this);

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

  _createClass(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) {
      Array.from(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;