'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 _DragItemPreviewContainer = require('./styles/DragItemPreviewContainer');

var _DragItemPreviewContainer2 = _interopRequireDefault(_DragItemPreviewContainer);

var _DragLayerOverlay = require('./styles/DragLayerOverlay');

var _DragLayerOverlay2 = _interopRequireDefault(_DragLayerOverlay);

var _DragContainer = require('../DragContainer');

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

/**
 * Renders a generic layer for drawing dragged items from draggable
 * components.
 */
var DragLayer = function (_React$Component) {
  _inherits(DragLayer, _React$Component);

  function DragLayer() {
    var _ref;

    var _temp, _this, _ret;

    _classCallCheck(this, DragLayer);

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

    return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = DragLayer.__proto__ || Object.getPrototypeOf(DragLayer)).call.apply(_ref, [this].concat(args))), _this), _this.renderPreviewContent = function () {
      var item = _this.props.item;


      return item.children && _react2.default.Children.map(item.children, function (child) {
        return child ? _react2.default.cloneElement(child, { wrapDragHandle: _lodash.identity }) : null;
      });
    }, _temp), _possibleConstructorReturn(_this, _ret);
  }

  _createClass(DragLayer, [{
    key: 'render',
    value: function render() {
      var _props = this.props,
          item = _props.item,
          clientOffset = _props.clientOffset,
          Overlay = _props.Overlay,
          PreviewContainer = _props.PreviewContainer,
          Cutout = _props.Cutout;


      if (!item || !clientOffset) {
        return _react2.default.createElement(Overlay, null);
      }

      var previewPositionStyles = {
        transform: 'translate(' + clientOffset.x + 'px, ' + clientOffset.y + 'px)',
        width: item.dimensions ? item.dimensions.width + 'px' : '0px',
        height: item.dimensions ? item.dimensions.height + 'px' : '0px'
      };

      return _react2.default.createElement(
        _DragContainer.DragContext.Provider,
        { value: { wrapDragHandle: _lodash.identity } },
        _react2.default.createElement(
          Overlay,
          null,
          _react2.default.createElement(
            'div',
            { style: previewPositionStyles },
            _react2.default.createElement(
              PreviewContainer,
              null,
              this.renderPreviewContent()
            )
          )
        )
      );
    }
  }]);

  return DragLayer;
}(_react2.default.Component);

DragLayer.propTypes = {
  /**
   * Provided by DragGroupItem via drag context.
   * Contains details needed to render an item preview
   */
  item: _propTypes2.default.shape({
    children: _propTypes2.default.node,
    dimensions: _propTypes2.default.shape({
      width: _propTypes2.default.number,
      height: _propTypes2.default.number
    })
  }),
  /**
   * Provided by
   * Offset values for the position of the dragged preview
   * item
   */
  clientOffset: _propTypes2.default.object,

  /**
   * Override the presentational component which renders the full-screen
   * overlay
   */
  Overlay: _propTypes2.default.func,
  /**
   * Override the presentational component which positions and sizes
   * the preview element
   */
  PreviewContainer: _propTypes2.default.func
};
DragLayer.defaultProps = {
  item: null,
  clientOffset: null,
  Overlay: _DragLayerOverlay2.default,
  PreviewContainer: _DragItemPreviewContainer2.default
};


var collect = function collect(monitor) {
  return {
    item: monitor.getItem(),
    clientOffset: monitor.getClientOffset()
  };
};

exports.default = (0, _reactDnd.DragLayer)(collect)(DragLayer);