import { closestCorners, getFirstCollision, KeyboardCode } from '@dnd-kit/core';
const directions = [KeyboardCode.Down, KeyboardCode.Right, KeyboardCode.Up, KeyboardCode.Left];
export const coordinateGetter = (event, { context: { active, droppableRects, droppableContainers, collisionRect } }) => {
    var _a;
    if (directions.includes(event.code)) {
        event.preventDefault();
        if (!active || !collisionRect) {
            return;
        }
        const filteredContainers = [];
        droppableContainers.getEnabled().forEach(entry => {
            var _a;
            if (!entry || (entry === null || entry === void 0 ? void 0 : entry.disabled)) {
                return;
            }
            const rect = droppableRects.get(entry.id);
            if (!rect) {
                return;
            }
            const data = entry.data.current;
            if (data) {
                const { type, children } = data;
                if (type === 'container' && Array.isArray(children) && children.length > 0) {
                    if (((_a = active.data.current) === null || _a === void 0 ? void 0 : _a.type) !== 'container') {
                        return;
                    }
                }
            }
            switch (event.code) {
                case KeyboardCode.Down:
                    if (collisionRect.top < rect.top) {
                        filteredContainers.push(entry);
                    }
                    break;
                case KeyboardCode.Up:
                    if (collisionRect.top > rect.top) {
                        filteredContainers.push(entry);
                    }
                    break;
                case KeyboardCode.Left:
                    if (collisionRect.left >= rect.left + rect.width) {
                        return;
                    }
                    break;
                case KeyboardCode.Right:
                    if (collisionRect.left + collisionRect.width <= rect.left) {
                        filteredContainers.push(entry);
                    }
                    break;
            }
        });
        const collisions = closestCorners({
            active,
            collisionRect: collisionRect,
            droppableRects,
            droppableContainers: filteredContainers,
            pointerCoordinates: null
        });
        const closestId = getFirstCollision(collisions, 'id');
        if (closestId != null) {
            const newDroppable = droppableContainers.get(closestId);
            const newNode = newDroppable === null || newDroppable === void 0 ? void 0 : newDroppable.node.current;
            const newRect = newDroppable === null || newDroppable === void 0 ? void 0 : newDroppable.rect.current;
            if (newNode && newRect) {
                if (newDroppable.id === 'placeholder') {
                    return {
                        x: newRect.left + (newRect.width - collisionRect.width) / 2,
                        y: newRect.top + (newRect.height - collisionRect.height) / 2
                    };
                }
                if (((_a = newDroppable.data.current) === null || _a === void 0 ? void 0 : _a.type) === 'container') {
                    return {
                        x: newRect.left + 20,
                        y: newRect.top + 74
                    };
                }
                return {
                    x: newRect.left,
                    y: newRect.top
                };
            }
        }
    }
    return undefined;
};
//# sourceMappingURL=multipleContainersKeyboardCoordinates.js.map