"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.NestedTableDragAndDropContext = 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 _util = require("@wix/bex-core/util");
var _minimalRequiredWdsRuntimeCheck = require("../../minimalRequiredWdsRuntimeCheck");
var _validateNestedTableDragAndDropProps = require("./validateNestedTableDragAndDropProps");
var _DragOverlayTestComponent = require("../DragAndDrop/DragOverlayTestComponent");
var _NestedTableDragAndDropOverlay = require("./NestedTableDragAndDropOverlay");
var _createUseIsDropDisabled = require("./createUseIsDropDisabled");
var _react2 = require("@wix/bex-core/react");
var _jsxFileName = "/home/builduser/work/843658b9f44192f0/packages/cairo/dist/cjs/components/NestedTableDragAndDrop/NestedTableDragAndDropContext.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 _NestedTableDragAndDropContext(props) {
  const {
    state: nestedState,
    onSubmit,
    onBulkSubmit,
    children
  } = props;
  const {
    type: Table,
    props: tableProps
  } = children;
  if (process.env.NODE_ENV !== 'production') {
    (0, _minimalRequiredWdsRuntimeCheck.minimalRequiredWdsRuntimeCheck)({
      required: '^1.109.2',
      requiredBy: 'WixPatterns/NestedTable'
    });
    (0, _validateNestedTableDragAndDropProps.validateNestedTableDragAndDropProps)(nestedState);
  }
  const state = nestedState.nestedTableDragAndDrop;
  const useIsDropDisabled = (0, _createUseIsDropDisabled.createUseIsDropDisabled)(state);
  const dragAndDropState = state.dnd;
  const {
    modalsContainerRef
  } = (0, _react2.usePageContext)();
  (0, _react.useEffect)(() => state.init(), []);
  dragAndDropState.onSubmit = onSubmit;
  dragAndDropState.onBulkSubmit = onBulkSubmit;
  const [dragAndDropContext] = (0, _react.useState)(() => ({
    useIsDropDisabled,
    closestCenterOnViewPort: true,
    onDragAndDropDisabledRowPointerDown: (id, e) => {
      dragAndDropState.attachDisabledDragAttempt(id, e, 'y');
    },
    cancelDrop: async ev => {
      return 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);
      });
    },
    overlayWrapperElement: /*#__PURE__*/(0, _react.forwardRef)((props, ref) => /*#__PURE__*/_react.default.createElement(_NestedTableDragAndDropOverlay.NestedTableDragAndDropOverlay, (0, _extends2.default)({
      innerRef: ref,
      state: state
    }, props, {
      __self: this,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 104,
        columnNumber: 9
      }
    }))),
    ...(process.env.NODE_ENV === 'test' && {
      DragOverlayComponentType: props => /*#__PURE__*/_react.default.createElement(_DragOverlayTestComponent.DragOverlayTestComponent, (0, _extends2.default)({}, props, {
        state: dragAndDropState,
        __self: this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 112,
          columnNumber: 11
        }
      }))
    }),
    keyboardCodes: dragAndDropState.keyboardCodes,
    autoScrollOptions: {
      acceleration: 6
    },
    hideOverlayBottomShadow: true,
    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
        }))
      })
    }
  }));
  dragAndDropContext.a11yContainer = modalsContainerRef.current;
  return /*#__PURE__*/_react.default.createElement(_dragAndDrop.DragAndDropContext.Provider, {
    value: dragAndDropContext,
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 159,
      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: 160,
      columnNumber: 7
    }
  })), /*#__PURE__*/_react.default.createElement(_DragAndDropAnnouncements.DragAndDropAnnouncements, {
    state: dragAndDropState,
    dataHook: "collection-table-dnd-live-region",
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 168,
      columnNumber: 7
    }
  }));
}
const NestedTableDragAndDropContext = exports.NestedTableDragAndDropContext = (0, _mobxReactLite.observer)(_NestedTableDragAndDropContext);
//# sourceMappingURL=NestedTableDragAndDropContext.js.map