"use strict";

exports.__esModule = true;
exports.sortableGridKeyboardCoordinates = void 0;
var _core = require("@wix/wix-style-react-incubator/dnd-kit/core");
var _utilities = require("@wix/wix-style-react-incubator/dnd-kit/utilities");
var _sortable = require("@wix/wix-style-react-incubator/dnd-kit/sortable");
const directions = [_core.KeyboardCode.Down, _core.KeyboardCode.Right, _core.KeyboardCode.Up, _core.KeyboardCode.Left];
const sortableGridKeyboardCoordinates = ({
  containerDistanceThreshold = 0
} = {}) => (event, {
  context: {
    active,
    collisionRect,
    droppableRects,
    droppableContainers,
    over,
    scrollableAncestors
  }
}) => {
  if (directions.includes(event.code)) {
    event.preventDefault();
    if (!active || !collisionRect) {
      return;
    }
    const filteredContainers = [];
    droppableContainers.getEnabled().forEach(entry => {
      if (!entry || entry != null && entry.disabled) {
        return;
      }
      const rect = droppableRects.get(entry.id);
      if (!rect) {
        return;
      }
      switch (event.code) {
        case _core.KeyboardCode.Down:
          if (rect.top - collisionRect.top > containerDistanceThreshold) {
            filteredContainers.push(entry);
          }
          break;
        case _core.KeyboardCode.Up:
          if (collisionRect.top - rect.top > containerDistanceThreshold) {
            filteredContainers.push(entry);
          }
          break;
        case _core.KeyboardCode.Left:
          if (collisionRect.left - rect.left > containerDistanceThreshold) {
            filteredContainers.push(entry);
          }
          break;
        case _core.KeyboardCode.Right:
          if (rect.left - collisionRect.left > containerDistanceThreshold) {
            filteredContainers.push(entry);
          }
          break;
      }
    });
    const collisions = (0, _core.closestCorners)({
      active,
      collisionRect,
      droppableRects,
      droppableContainers: filteredContainers,
      pointerCoordinates: null
    });
    let closestId = (0, _core.getFirstCollision)(collisions, 'id');
    if (closestId === (over == null ? void 0 : over.id) && collisions.length > 1) {
      closestId = collisions[1].id;
    }
    if (closestId != null) {
      const activeDroppable = droppableContainers.get(active.id);
      const newDroppable = droppableContainers.get(closestId);
      const newRect = newDroppable ? droppableRects.get(newDroppable.id) : null;
      const newNode = newDroppable == null ? void 0 : newDroppable.node.current;
      if (newNode && newRect && activeDroppable && newDroppable) {
        const newScrollAncestors = (0, _core.getScrollableAncestors)(newNode);
        const hasDifferentScrollAncestors = newScrollAncestors.some((element, index) => scrollableAncestors[index] !== element);
        const hasSameContainer = isSameContainer(activeDroppable, newDroppable);
        const isAfterActive = isAfter(activeDroppable, newDroppable);
        const offset = hasDifferentScrollAncestors || !hasSameContainer ? {
          x: 0,
          y: 0
        } : {
          x: isAfterActive ? collisionRect.width - newRect.width : 0,
          y: isAfterActive ? collisionRect.height - newRect.height : 0
        };
        const rectCoordinates = {
          x: newRect.left,
          y: newRect.top
        };
        const newCoordinates = offset.x && offset.y ? rectCoordinates : (0, _utilities.subtract)(rectCoordinates, offset);
        return newCoordinates;
      }
    }
  }
  return undefined;
};
exports.sortableGridKeyboardCoordinates = sortableGridKeyboardCoordinates;
function isSameContainer(a, b) {
  if (!(0, _sortable.hasSortableData)(a) || !(0, _sortable.hasSortableData)(b)) {
    return false;
  }
  return a.data.current.sortable.containerId === b.data.current.sortable.containerId;
}
function isAfter(a, b) {
  if (!(0, _sortable.hasSortableData)(a) || !(0, _sortable.hasSortableData)(b)) {
    return false;
  }
  if (!isSameContainer(a, b)) {
    return false;
  }
  return a.data.current.sortable.index < b.data.current.sortable.index;
}
//# sourceMappingURL=sortableGridKeyboardCoordinates.js.map