import { useRef } from "react";
const DEV = process.env.NODE_ENV !== "production";
/**
 * Dev-only hook that detects key instability in a list of keyed items.
 *
 * Compares the current render's key set against the previous render's
 * and warns when:
 *
 * - Duplicate keys exist in the current render (collision bug).
 * - A key disappears and a new key appears at the same list index
 *   (suggests a remount caused by key change, not a logical add/remove).
 * - The total number of key replacements in a single render exceeds a
 *   threshold, indicating widespread instability.
 *
 * In production builds the entire function body is dead-code-eliminated.
 */
export function useKeyStability(items) {
    const prevRef = useRef(null);
    if (!DEV)
        return;
    // Detect duplicate keys within the current render.
    const seen = new Map();
    for (let i = 0; i < items.length; i++) {
        const prevIdx = seen.get(items[i].key);
        if (prevIdx !== undefined) {
            console.warn(`[stgm:perf:keys] Duplicate key "${items[i].key}" at indices ${prevIdx} and ${i} ` +
                `(${items[prevIdx].kind}, ${items[i].kind}). React will silently drop one.`);
        }
        else {
            seen.set(items[i].key, i);
        }
    }
    const prev = prevRef.current;
    prevRef.current = items;
    if (!prev || prev.length === 0)
        return;
    const prevKeys = new Map();
    for (let i = 0; i < prev.length; i++) {
        prevKeys.set(prev[i].key, i);
    }
    const curKeys = new Set(seen.keys());
    const removed = [];
    for (const key of prevKeys.keys()) {
        if (!curKeys.has(key))
            removed.push(key);
    }
    if (removed.length === 0)
        return;
    let swapCount = 0;
    for (const removedKey of removed) {
        const idx = prevKeys.get(removedKey);
        if (idx < items.length) {
            const replacement = items[idx];
            if (!prevKeys.has(replacement.key)) {
                swapCount++;
                console.warn(`[stgm:perf:keys] Key swap at index ${idx}: ` +
                    `"${removedKey}" (${prev[idx].kind}) → "${replacement.key}" (${replacement.kind}). ` +
                    "This causes React to unmount/remount the row.");
            }
        }
    }
    if (swapCount > 3) {
        console.warn(`[stgm:perf:keys] ${swapCount} key swaps detected in a single render. ` +
            "Thread items may be using unstable keys.");
    }
}
//# sourceMappingURL=use-key-stability.js.map