'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 _reactDnd = require('react-dnd');

var _reactDndHtml5Backend = require('react-dnd-html5-backend');

var _reactDndHtml5Backend2 = _interopRequireDefault(_reactDndHtml5Backend);

var _update = require('react/lib/update');

var _update2 = _interopRequireDefault(_update);

var _SortableItem = require('./SortableItem');

var _SortableItem2 = _interopRequireDefault(_SortableItem);

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

/*
 * Based on
 * https://github.com/react-dnd/react-dnd/tree/master/examples/04%20Sortable/Simple
 */
var propTypes = {
  /** @ignore */
  className: _propTypes2.default.string,
  list: _propTypes2.default.array,
  keyProp: _propTypes2.default.string,
  children: _propTypes2.default.func,
  onChange: _propTypes2.default.func.isRequired,
  listName: _propTypes2.default.string.isRequired
};

var defaultProps = {
  list: [],
  keyProp: null,
  className: undefined,
  children: null
};

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

  function SortableList() {
    var _ref;

    var _temp, _this, _ret;

    _classCallCheck(this, SortableList);

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

    return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = SortableList.__proto__ || Object.getPrototypeOf(SortableList)).call.apply(_ref, [this].concat(args))), _this), _this.state = {
      list: _this.props.list
    }, _this.moveItem = function (dragIndex, hoverIndex) {
      var list = _this.state.list;

      var dragItem = list[dragIndex];

      var newList = (0, _update2.default)(list, {
        $splice: [[dragIndex, 1], [hoverIndex, 0, dragItem]]
      });
      _this.setState({ list: newList });
    }, _this.endDrag = function () {
      var list = _this.state.list;
      var onChange = _this.props.onChange;

      onChange(list);
    }, _temp), _possibleConstructorReturn(_this, _ret);
  }

  _createClass(SortableList, [{
    key: 'componentWillReceiveProps',
    value: function componentWillReceiveProps(nextProps) {
      this.setState({ list: nextProps.list });
    }
  }, {
    key: 'render',
    value: function render() {
      var _this2 = this;

      var list = this.state.list;
      var _props = this.props,
          children = _props.children,
          keyProp = _props.keyProp,
          className = _props.className,
          listName = _props.listName;


      return _react2.default.createElement(
        'div',
        { className: className },
        list.map(function (item, i) {
          return _react2.default.createElement(
            _SortableItem2.default,
            {
              className: 'flive_sortable_sortable--item',
              key: item[keyProp],
              listName: listName,
              index: i,
              moveItem: _this2.moveItem,
              endDrag: _this2.endDrag
            },
            children(item, i)
          );
        })
      );
    }
  }]);

  return SortableList;
}(_react.Component);

SortableList.propTypes = propTypes;
SortableList.defaultProps = defaultProps;

exports.default = (0, _reactDnd.DragDropContext)(_reactDndHtml5Backend2.default)(SortableList);
module.exports = exports['default'];