"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.DraggableListItem = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _sortable = require("@wix/wix-style-react-incubator/dnd-kit/sortable");
var _react = _interopRequireWildcard(require("react"));
var _utilities = require("@wix/wix-style-react-incubator/dnd-kit/utilities");
var _mobxReactLite = require("mobx-react-lite");
var _dndTestEventHandlers = require("../GridDragAndDropDndKit/dndTestEventHandlers");
var _util = require("@wix/bex-core/util");
var _jsxFileName = "/home/builduser/work/843658b9f44192f0/packages/cairo/dist/cjs/components/ListDragAndDrop/DraggableListItem.tsx";
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (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 (const 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 _DraggableListItem(props) {
  var _state$_dnd, _dnd$nullAnnouncement, _state$_dnd3;
  const {
    children,
    id,
    isOverlay,
    dndDisabled,
    state
  } = props;
  const {
    type: CardItem,
    props: cardItemProps
  } = children;
  const {
    _dnd
  } = state;
  const isDropping = (_dnd == null ? void 0 : _dnd.droppingId) === id;
  const disabled = {
    draggable: dndDisabled,
    droppable: dndDisabled || ((_state$_dnd = state._dnd) == null ? void 0 : _state$_dnd.isActiveRectOutsideContainer)
  };
  const dragHandleRef = (0, _react.useRef)(null);
  (0, _react.useEffect)(() => {
    if (_dnd) {
      return (0, _util.addEventListener)(_dnd.events, 'dropAnimationEnd', () => {
        _dnd.focusDragHandleIfDropped(id, dragHandleRef.current);
      });
    }
    return;
  }, [id, _dnd]);
  const {
    attributes,
    listeners,
    setNodeRef,
    transform,
    transition,
    isDragging,
    setActivatorNodeRef
  } = (0, _sortable.useSortable)({
    id,
    transition: _dnd == null ? void 0 : _dnd.sortableAnimation,
    disabled
  });
  const style = {
    position: 'relative',
    width: '100%',
    transform: _utilities.CSS.Transform.toString(transform),
    transition,
    visibility: (isDragging || isDropping) && !isOverlay ? 'hidden' : undefined
  };
  const testEvents = process.env.NODE_ENV === 'test' && state._dnd ? (0, _dndTestEventHandlers.sortableItemTestEvents)({
    id,
    disabled,
    state: state._dnd
  }) : undefined;
  const dndAttrs = {
    ...attributes,
    ...listeners,
    ...testEvents,
    onPointerDown: e => {
      var _state$_dnd2;
      (_state$_dnd2 = state._dnd) == null || _state$_dnd2.containerEvents.onPointerDown == null || _state$_dnd2.containerEvents.onPointerDown(e);
      listeners == null || listeners.onPointerDown == null || listeners.onPointerDown(e);
      testEvents == null || testEvents.onPointerDown == null || testEvents.onPointerDown(e);
    }
  };
  const ariaDescribedBy = `draggable-list-item-description-${id}`;
  const dragHandleProps = isOverlay ? {} : {
    tabIndex: 0,
    domRef: e => {
      setActivatorNodeRef(e);
    },
    role: 'button',
    'aria-label': _dnd == null || (_dnd$nullAnnouncement = _dnd.nullAnnouncements) == null || _dnd$nullAnnouncement.dragHandleLabel == null ? void 0 : _dnd$nullAnnouncement.dragHandleLabel(id),
    'aria-describedby': ariaDescribedBy,
    onClick: e => e.stopPropagation(),
    ...(_dnd == null ? void 0 : _dnd.handleEvents),
    ...(process.env.NODE_ENV === 'test' && state._dnd && (0, _dndTestEventHandlers.dragHandleTestEvents)({
      id,
      state: state._dnd
    }))
  };
  return /*#__PURE__*/_react.default.createElement("div", (0, _extends2.default)({
    ref: setNodeRef
  }, dndAttrs, {
    tabIndex: undefined,
    style: style,
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 100,
      columnNumber: 5
    }
  }), /*#__PURE__*/_react.default.createElement("div", {
    id: ariaDescribedBy,
    style: {
      display: 'none'
    },
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 101,
      columnNumber: 7
    }
  }, (_state$_dnd3 = state._dnd) == null || (_state$_dnd3 = _state$_dnd3.nullAnnouncements) == null || _state$_dnd3.itemDescription == null ? void 0 : _state$_dnd3.itemDescription(id)), /*#__PURE__*/_react.default.createElement(CardItem, (0, _extends2.default)({}, cardItemProps, {
    draggable: true,
    droppable: _dnd == null ? void 0 : _dnd.isActive,
    dragging: isOverlay,
    dragHandleProps: dragHandleProps,
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 104,
      columnNumber: 7
    }
  })));
}
const DraggableListItem = exports.DraggableListItem = (0, _mobxReactLite.observer)(_DraggableListItem);
//# sourceMappingURL=DraggableListItem.js.map