import { useCallback, useRef, useState } from "react";
function useNavigationStack(initialItem) {
    const [stack, setStack] = useState(initialItem ? [
        initialItem
    ] : []);
    const stackRef = useRef(stack);
    stackRef.current = stack;
    const push = useCallback((item)=>{
        setStack((prev)=>[
                ...prev,
                item
            ]);
    }, []);
    const pop = useCallback(()=>{
        const currentStack = stackRef.current;
        if (0 === currentStack.length) return null;
        const poppedItem = currentStack.at(-1);
        setStack((prev)=>prev.slice(0, -1));
        return poppedItem;
    }, []);
    const clear = useCallback(()=>{
        setStack([]);
    }, []);
    const canGoBack = stack.length > 0;
    const current = canGoBack ? stack.at(-1) : null;
    const isEmpty = !canGoBack;
    return {
        stack,
        current,
        canGoBack,
        isEmpty,
        push,
        pop,
        clear
    };
}
export { useNavigationStack };
