"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.GridDraggableCardItem = 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 _designSystem = require("@wix/design-system");
var _mobxReactLite = require("mobx-react-lite");
var _dndTestEventHandlers = require("./dndTestEventHandlers");
var _util = require("@wix/bex-core/util");
var _jsxFileName = "/home/builduser/work/843658b9f44192f0/packages/cairo/dist/cjs/components/GridDragAndDropDndKit/GridDraggableCardItem.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 _GridDraggableCardItem(props) {
  var _dnd$nullAnnouncement, _state$_dnd2;
  const {
    children,
    id,
    isOverlay,
    state
  } = props;
  const {
    type: CardItem,
    props: forwardCardItemProps
  } = children;
  const {
    _dnd
  } = state;
  const isDropping = (_dnd == null ? void 0 : _dnd.droppingId) === id;
  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: {
      draggable: false,
      droppable: _dnd == null ? void 0 : _dnd.isActiveRectOutsideContainer
    }
  });
  const showMask = (isDragging || isDropping) && !isOverlay;
  const ariaDescribedBy = `draggable-card-item-description-${id}`;
  const dragHandleProps = isOverlay ? {} : {
    tabIndex: 0,
    domRef: e => {
      setActivatorNodeRef(e);
      dragHandleRef.current = e;
      _dnd == null || _dnd.focusDragHandleIfDropped(id, dragHandleRef.current);
    },
    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
    }))
  };
  const cardItemProps = {
    ...forwardCardItemProps,
    ...(_dnd != null && _dnd.isDisabled ? {} : {
      draggable: true,
      droppable: _dnd == null ? void 0 : _dnd.isActive,
      dragging: isOverlay,
      dragHandleProps
    })
  };
  const style = {
    position: 'relative',
    height: '100%',
    width: '100%',
    transform: _utilities.CSS.Transform.toString(transform),
    transition,
    ...((_dnd == null ? void 0 : _dnd.isDropping) && {
      transition: undefined
    })
  };
  const testEvents = process.env.NODE_ENV === 'test' && state._dnd ? (0, _dndTestEventHandlers.sortableItemTestEvents)({
    id,
    state: state._dnd
  }) : undefined;
  const dndAttrs = _dnd != null && _dnd.isDisabled ? {
    onPointerDown: e => {
      _dnd == null || _dnd.attachDisabledDragAttempt(id, e.nativeEvent, 'all');
    }
  } : {
    ...attributes,
    ...listeners,
    ...testEvents,
    onPointerDown: e => {
      var _state$_dnd;
      (_state$_dnd = state._dnd) == null || _state$_dnd.containerEvents.onPointerDown == null || _state$_dnd.containerEvents.onPointerDown(e);
      listeners == null || listeners.onPointerDown == null || listeners.onPointerDown(e);
      testEvents == null || testEvents.onPointerDown == null || testEvents.onPointerDown(e);
    }
  };
  return /*#__PURE__*/_react.default.createElement("div", (0, _extends2.default)({
    ref: setNodeRef,
    style: style
  }, dndAttrs, {
    tabIndex: undefined,
    "data-hook": "grid-card-item-events",
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 119,
      columnNumber: 5
    }
  }), /*#__PURE__*/_react.default.createElement("div", {
    id: ariaDescribedBy,
    style: {
      display: 'none'
    },
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 126,
      columnNumber: 7
    }
  }, (_state$_dnd2 = state._dnd) == null || (_state$_dnd2 = _state$_dnd2.nullAnnouncements) == null || _state$_dnd2.itemDescription == null ? void 0 : _state$_dnd2.itemDescription(id)), !showMask && /*#__PURE__*/_react.default.createElement(CardItem, (0, _extends2.default)({}, cardItemProps, {
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 129,
      columnNumber: 21
    }
  })), showMask && /*#__PURE__*/_react.default.createElement(_designSystem.Box, {
    borderRadius: 8,
    height: "100%",
    width: "100%",
    backgroundColor: "D60",
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 131,
      columnNumber: 9
    }
  }));
}
const GridDraggableCardItem = exports.GridDraggableCardItem = (0, _mobxReactLite.observer)(_GridDraggableCardItem);
//# sourceMappingURL=GridDraggableCardItem.js.map