"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
exports.__esModule = true;
exports["default"] = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _reactDnd = require("react-dnd");
var _itemTypes = _interopRequireDefault(require("./itemTypes"));
var _ReactPortal = _interopRequireDefault(require("../utils/ReactPortal/ReactPortal"));
var _constants = require("../DragAndDrop/Draggable/constants");
var _jsxFileName = "/home/builduser/work/57e038ea7326c1ec/packages/wix-design-system/dist/cjs/NestableListBase/DragLayer.jsx";
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2["default"])(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _callSuper(t, o, e) { return o = (0, _getPrototypeOf2["default"])(o), (0, _possibleConstructorReturn2["default"])(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], (0, _getPrototypeOf2["default"])(t).constructor) : o.apply(t, e)); }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
var defaultLayerStyles = {
  position: 'fixed',
  pointerEvents: 'none',
  left: 0,
  top: 0
};
function getItemStyles(props, clientRect, handleOffset) {
  var initialOffset = props.initialOffset,
    currentOffset = props.currentOffset;
  if (!initialOffset || !currentOffset) {
    return {
      display: 'none'
    };
  }
  var x = currentOffset.x - handleOffset.x;
  var y = currentOffset.y - handleOffset.y;
  var width = clientRect.width,
    height = clientRect.height;
  var transform = "translate(".concat(x, "px, ").concat(y, "px)");
  return {
    transform: transform,
    WebkitTransform: transform,
    width: width,
    height: height
  };
}
var defaultConnectDragSource = function defaultConnectDragSource(el) {
  return el;
};
var CustomDragLayer = /*#__PURE__*/function (_Component) {
  function CustomDragLayer() {
    var _this;
    (0, _classCallCheck2["default"])(this, CustomDragLayer);
    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }
    _this = _callSuper(this, CustomDragLayer, [].concat(args));
    _this.renderChildren = function (items, depth) {
      var _this$props = _this.props,
        renderItem = _this$props.renderItem,
        childrenProperty = _this$props.childrenProperty,
        childrenStyle = _this$props.childrenStyle,
        theme = _this$props.theme;
      if (!items || !items.length) {
        return null;
      }
      var classes = (0, _classnames["default"])('nestable-item', 'dragging-nestable-item', theme && theme.item);
      return /*#__PURE__*/_react["default"].createElement("div", {
        style: childrenStyle,
        __self: _this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 51,
          columnNumber: 7
        }
      }, items.map(function (item, i) {
        return /*#__PURE__*/_react["default"].createElement("div", {
          className: classes,
          "data-hook": "dragging-nestable-item",
          key: i,
          __self: _this,
          __source: {
            fileName: _jsxFileName,
            lineNumber: 53,
            columnNumber: 11
          }
        }, /*#__PURE__*/_react["default"].cloneElement(renderItem({
          item: item,
          isPlaceholder: false,
          isPreview: true,
          depth: depth,
          siblings: items,
          connectDragSource: defaultConnectDragSource
        }), (0, _defineProperty2["default"])((0, _defineProperty2["default"])((0, _defineProperty2["default"])({}, _constants.dataAttributes.draggableSource, true), _constants.dataAttributes.depth, depth - 1), _constants.dataAttributes.id, item.id)), !item.isCollapsed && _this.renderChildren(item[childrenProperty], depth + 1, item.isCollapsed));
      }));
    };
    return _this;
  }
  (0, _inherits2["default"])(CustomDragLayer, _Component);
  return (0, _createClass2["default"])(CustomDragLayer, [{
    key: "render",
    value: function render() {
      var _this$props2 = this.props,
        item = _this$props2.item,
        itemType = _this$props2.itemType,
        siblings = _this$props2.siblings,
        renderItem = _this$props2.renderItem,
        isPlaceholder = _this$props2.isPlaceholder,
        childrenProperty = _this$props2.childrenProperty,
        isRenderDraggingChildren = _this$props2.isRenderDraggingChildren,
        theme = _this$props2.theme,
        dragLayerZIndex = _this$props2.dragLayerZIndex;
      if (!isPlaceholder || itemType !== _itemTypes["default"].nestedItem) {
        return null;
      }
      var classes = (0, _classnames["default"])('nestable-item', 'dragging-nestable-item', theme && theme.item);
      var renderedItem = /*#__PURE__*/_react["default"].cloneElement(renderItem({
        item: item.data,
        isPlaceholder: false,
        isPreview: true,
        depth: 1,
        siblings: siblings,
        connectDragSource: defaultConnectDragSource
      }), (0, _defineProperty2["default"])((0, _defineProperty2["default"])((0, _defineProperty2["default"])({}, _constants.dataAttributes.draggableSource, true), _constants.dataAttributes.depth, 0), _constants.dataAttributes.id, item.id));

      // portal is used because of position fixed and transform issue
      return /*#__PURE__*/_react["default"].createElement(_ReactPortal["default"], {
        __self: this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 122,
          columnNumber: 7
        }
      }, /*#__PURE__*/_react["default"].createElement("div", {
        style: _objectSpread(_objectSpread({}, defaultLayerStyles), {}, {
          zIndex: dragLayerZIndex
        }),
        __self: this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 123,
          columnNumber: 9
        }
      }, /*#__PURE__*/_react["default"].createElement("div", {
        className: classes,
        style: getItemStyles(this.props, item.clientRect, item.handleOffset),
        __self: this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 124,
          columnNumber: 11
        }
      }, renderedItem, isRenderDraggingChildren && !item.data.isCollapsed && this.renderChildren(item.data[childrenProperty], 2))));
    }
  }]);
}(_react.Component);
var PureDragLayer = /*#__PURE__*/function (_React$PureComponent) {
  function PureDragLayer() {
    (0, _classCallCheck2["default"])(this, PureDragLayer);
    return _callSuper(this, PureDragLayer, arguments);
  }
  (0, _inherits2["default"])(PureDragLayer, _React$PureComponent);
  return (0, _createClass2["default"])(PureDragLayer, [{
    key: "render",
    value: function render() {
      return /*#__PURE__*/_react["default"].createElement(CustomDragLayer, (0, _extends2["default"])({}, this.props, {
        __self: this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 145,
          columnNumber: 12
        }
      }));
    }
  }]);
}(_react["default"].PureComponent);
var _default = exports["default"] = (0, _reactDnd.DragLayer)(function (monitor) {
  return {
    item: monitor.getItem(),
    itemType: monitor.getItemType(),
    initialOffset: monitor.getInitialSourceClientOffset(),
    currentOffset: monitor.getSourceClientOffset(),
    isPlaceholder: monitor.isDragging()
  };
})(PureDragLayer);