'use strict';

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

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

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

  function DragLayer() {
    var _ref;

    var _temp, _this, _ret;

    (0, _classCallCheck3.default)(this, DragLayer);

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

    return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, (_ref = DragLayer.__proto__ || (0, _getPrototypeOf2.default)(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), (0, _possibleConstructorReturn3.default)(_this, _ret);
  }

  (0, _createClass3.default)(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);