import { layoutAwareIndex } from "../page-editor-chrome/overlay/placeholderZones";
/**
 * Compute where a component lands when nudged one real sibling up or down
 * within its placeholder. Returns null when the move is not possible (component
 * not found, is a layout component, or already at the boundary). Layout
 * components are skipped so keyboard reordering agrees with drag-and-drop.
 */
export function computeReorderTarget(components, componentId, direction) {
    const rawIndex = components.findIndex((c) => c.id === componentId);
    if (rawIndex < 0)
        return null;
    const realSiblings = components.filter((c) => !c.layoutId);
    const realPos = realSiblings.findIndex((c) => c.id === componentId);
    if (realPos < 0)
        return null; // the component itself is a layout component
    const totalReal = realSiblings.length;
    if (direction === "up") {
        const previousReal = realSiblings[realPos - 1];
        if (!previousReal)
            return null;
        // Drop into the gap before the previous real sibling.
        const targetRawGap = components.findIndex((c) => c.id === previousReal.id);
        return {
            targetAdjustedIndex: layoutAwareIndex(components, targetRawGap),
            newPosition: realPos, // 0-based realPos - 1, expressed 1-based
            totalReal,
        };
    }
    const nextReal = realSiblings[realPos + 1];
    if (!nextReal)
        return null;
    // Drop into the gap after the next real sibling.
    const nextRawIndex = components.findIndex((c) => c.id === nextReal.id);
    return {
        targetAdjustedIndex: layoutAwareIndex(components, nextRawIndex + 1),
        newPosition: realPos + 2, // 0-based realPos + 1, expressed 1-based
        totalReal,
    };
}
//# sourceMappingURL=reorderComponent.js.map