"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.TableDragAndDropContext = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _react = _interopRequireWildcard(require("react"));
var _mobxReactLite = require("mobx-react-lite");
var _dragAndDrop = require("@wix/wix-style-react-incubator/drag-and-drop");
var _DragAndDropAnnouncements = require("../DragAndDrop/DragAndDropAnnouncements");
var _dndTestEventHandlers = require("../GridDragAndDropDndKit/dndTestEventHandlers");
var _providers = require("../../providers");
var _util = require("@wix/bex-core/util");
var _createUseIsDropDisabled = require("./createUseIsDropDisabled");
var _react2 = require("@wix/bex-core/react");
var _jsxFileName = "/home/builduser/work/843658b9f44192f0/packages/cairo/dist/cjs/components/TableDragAndDropContext/TableDragAndDropContext.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 _TableDragAndDropContext(props) {
  const {
    onSubmit,
    onBulkSubmit,
    children
  } = props;
  const {
    type: Table,
    props: tableProps
  } = children;
  const tableState = (0, _providers.useTableContext)();
  const {
    modalsContainerRef
  } = (0, _react2.usePageContext)();
  const state = tableState.tableDragAndDropState;
  const dragAndDropState = state.dragAndDrop.dnd;
  const useIsDropDisabled = (0, _createUseIsDropDisabled.createUseIsDropDisabled)(dragAndDropState);
  (0, _react.useEffect)(() => state.init(), []);
  dragAndDropState.onSubmit = onSubmit;
  dragAndDropState.onBulkSubmit = onBulkSubmit;
  const [dragAndDropContext] = (0, _react.useState)(() => ({
    useIsDropDisabled,
    onDragAndDropDisabledRowPointerDown: (id, e) => {
      dragAndDropState.attachDisabledDragAttempt(id, e, 'y');
    },
    cancelDrop: ev => dragAndDropState.beforeDrop(ev),
    dropAnimation: {
      duration: dragAndDropState.dropAnimation.duration,
      easing: dragAndDropState.dropAnimation.easing
    },
    announcements: dragAndDropState.nullAnnouncements,
    a11yContainer: modalsContainerRef.current,
    onDragOver: ({
      over
    }) => dragAndDropState.onDragOver({
      over
    }),
    onDragMove: event => dragAndDropState.onDragMove(event),
    restoreFocus: false,
    onDropAnimationEnd: (id, dragHandleRef) => {
      return (0, _util.addEventListener)(dragAndDropState.events, 'dropAnimationEnd', () => {
        dragAndDropState.focusDragHandleIfDropped(id, dragHandleRef.current);
      });
    },
    eventHandlers: {
      row: ({
        id,
        disabled
      }) => {
        const testEvents = process.env.NODE_ENV === 'test' ? (0, _dndTestEventHandlers.sortableItemTestEvents)({
          state: dragAndDropState,
          id,
          disabled
        }) : undefined;
        return {
          ...testEvents,
          onPointerDown: e => {
            dragAndDropState.containerEvents.onPointerDown == null || dragAndDropState.containerEvents.onPointerDown(e);
            testEvents == null || testEvents.onPointerDown == null || testEvents.onPointerDown(e);
          }
        };
      },
      handle: ({
        id
      }) => ({
        onPointerDown: e => {
          dragAndDropState.handleEvents.onPointerDown == null || dragAndDropState.handleEvents.onPointerDown(e);
        },
        onKeyDown: e => {
          dragAndDropState.handleEvents.onKeyDown == null || dragAndDropState.handleEvents.onKeyDown(e);
        },
        ...(process.env.NODE_ENV === 'test' && (0, _dndTestEventHandlers.dragHandleTestEvents)({
          state: dragAndDropState,
          id
        }))
      }),
      keyboardCodes: dragAndDropState.keyboardCodes
    }
  }));
  dragAndDropContext.a11yContainer = modalsContainerRef.current;
  return /*#__PURE__*/_react.default.createElement(_dragAndDrop.DragAndDropContext.Provider, {
    value: dragAndDropContext,
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 115,
      columnNumber: 5
    }
  }, /*#__PURE__*/_react.default.createElement(Table, (0, _extends2.default)({}, tableProps, {
    isDragAndDropDisabled: dragAndDropState.isDisabled,
    onDragStart: event => dragAndDropState.onDragStart(event),
    onDragEnd: event => dragAndDropState.onDragEnd(event),
    onDragCancel: () => dragAndDropState.onDragCancel(),
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 116,
      columnNumber: 7
    }
  })), /*#__PURE__*/_react.default.createElement(_DragAndDropAnnouncements.DragAndDropAnnouncements, {
    state: dragAndDropState,
    dataHook: "collection-table-dnd-live-region",
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 124,
      columnNumber: 7
    }
  }));
}
const TableDragAndDropContext = exports.TableDragAndDropContext = (0, _mobxReactLite.observer)(_TableDragAndDropContext);
//# sourceMappingURL=TableDragAndDropContext.js.map