"use strict";

var _react = _interopRequireDefault(require("react"));

var _react2 = require("@storybook/react");

var _addonKnobs = require("@storybook/addon-knobs");

var _addonActions = require("@storybook/addon-actions");

var _index = _interopRequireDefault(require("./index"));

var _Token = _interopRequireDefault(require("../Token"));

var _Tile = _interopRequireDefault(require("../Tile"));

var _Col = _interopRequireDefault(require("../Layout/Col"));

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

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var itemsWithoutGroups = [{
  id: 1,
  type: "item",
  text: "Item A",
  description: "ID: 0123"
}, {
  id: 2,
  type: "item",
  text: "Item B",
  description: "ID: 4567"
}, {
  id: 3,
  type: "item",
  text: "Item C",
  description: "ID: 8910"
}, {
  id: 4,
  type: "item",
  text: "Item D",
  description: "ID: 1112"
}, {
  id: 5,
  type: "item",
  text: "Item E",
  description: "ID: 1314"
}];

var renderTokenItem = function renderTokenItem(_ref) {
  var item = _ref.item,
      index = _ref.index,
      snapshot = _ref.snapshot;
  return _react["default"].createElement("div", null, _react["default"].createElement(_Token["default"], {
    isDraggable: true,
    name: item["text"] + "- isDragging? " + snapshot.isDragging,
    order: index + 1,
    showWell: false
  }));
};

var renderTokenItemWithAdjustedDragging = function renderTokenItemWithAdjustedDragging(_ref2) {
  var item = _ref2.item,
      index = _ref2.index,
      snapshot = _ref2.snapshot,
      dragHandleProps = _ref2.dragHandleProps;
  return _react["default"].createElement("div", null, _react["default"].createElement(_Token["default"], {
    isDraggable: true,
    name: item["text"] + "- isDragging? " + snapshot.isDragging,
    showWell: false,
    usesDragHandle: true,
    dragHandleProps: dragHandleProps
  }));
};

var renderTileItem = function renderTileItem(_ref3) {
  var item = _ref3.item,
      index = _ref3.index,
      snapshot = _ref3.snapshot,
      dragHandleProps = _ref3.dragHandleProps;
  return _react["default"].createElement(_Col["default"], {
    small: "6"
  }, _react["default"].createElement(_Tile["default"], {
    name: item["text"],
    description: item["description"],
    onPillClick: (0, _addonActions.action)("onPillClick"),
    isDraggable: true,
    testSection: "with-warning",
    dragHandleProps: dragHandleProps,
    onDismiss: (0, _addonActions.action)("on Dismiss Click")
  }));
}; // Helper wrapper class to store the state so the stories are usable/interactive


var DragAndDrop =
/*#__PURE__*/
function (_React$Component) {
  _inherits(DragAndDrop, _React$Component);

  function DragAndDrop() {
    var _getPrototypeOf2;

    var _this;

    _classCallCheck(this, DragAndDrop);

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

    _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(DragAndDrop)).call.apply(_getPrototypeOf2, [this].concat(args)));

    _defineProperty(_assertThisInitialized(_this), "state", {
      items: _this.props.items
    });

    _defineProperty(_assertThisInitialized(_this), "updateItemOrder", function (items) {
      _this.setState({
        items: items
      });

      _this.props.onDragEnd(items);
    });

    return _this;
  }

  _createClass(DragAndDrop, [{
    key: "render",
    value: function render() {
      var _this$props = this.props,
          renderItem = _this$props.renderItem,
          idForDroppableRegion = _this$props.idForDroppableRegion,
          onBeforeCapture = _this$props.onBeforeCapture,
          useCustomDragHandle = _this$props.useCustomDragHandle;
      return _react["default"].createElement(_index["default"], {
        idForDroppableRegion: idForDroppableRegion,
        items: this.state.items,
        onBeforeCapture: onBeforeCapture,
        onDragEnd: this.updateItemOrder,
        renderItem: renderItem,
        useCustomDragHandle: useCustomDragHandle
      });
    }
  }]);

  return DragAndDrop;
}(_react["default"].Component);

DragAndDrop.propTypes = _index["default"].propTypes;
DragAndDrop.defaultProps = _index["default"].defaultProps;
var stories = (0, _react2.storiesOf)("Experimental|DragAndDrop", module);
stories.addDecorator(_addonKnobs.withKnobs).addDecorator(function (story) {
  return _react["default"].createElement("div", {
    id: "root-preview"
  }, story());
});
stories.add("Reordering flat list", function () {
  return _react["default"].createElement(DragAndDrop, {
    idForDroppableRegion: "droppable-story-demo",
    items: itemsWithoutGroups,
    onBeforeCapture: (0, _addonActions.action)("do something before dragging begins"),
    onDragEnd: (0, _addonActions.action)("get new order of items"),
    renderItem: renderTokenItem
  });
}).add("With custom drag handle", function () {
  return _react["default"].createElement(DragAndDrop, {
    idForDroppableRegion: "droppable-story-demo",
    items: itemsWithoutGroups,
    onBeforeCapture: (0, _addonActions.action)("do something before dragging begins"),
    onDragEnd: (0, _addonActions.action)("get new order of items"),
    useCustomDragHandle: true,
    renderItem: renderTokenItemWithAdjustedDragging
  });
}).add("Using Tile", function () {
  return _react["default"].createElement(DragAndDrop, {
    idForDroppableRegion: "droppable-story-demo",
    items: itemsWithoutGroups,
    onBeforeCapture: (0, _addonActions.action)("do something before dragging begins"),
    onDragEnd: (0, _addonActions.action)("get new order of items"),
    renderItem: renderTileItem
  });
});