'use client';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { createContext, useContext, useState, useCallback, useEffect, useRef, } from 'react';
import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from '../components/ui/alert-dialog.js';
const DrawerCloseGuardContext = createContext(null);
/* ---------------------------------------------------------------------------
 * Provider — renders the shared confirmation dialog
 * ------------------------------------------------------------------------- */
export function DrawerCloseGuardProvider({ children }) {
    // Use a ref so guard updates (on every keystroke) don't re-render the provider tree.
    const guardRef = useRef(null);
    const pendingNavigateRef = useRef(null);
    const [showConfirmation, setShowConfirmation] = useState(false);
    const setGuard = useCallback((guard) => {
        guardRef.current = guard;
    }, []);
    const guardedNavigate = useCallback((navigate) => {
        if (guardRef.current?.isDirty) {
            pendingNavigateRef.current = navigate;
            setShowConfirmation(true);
            return;
        }
        navigate();
    }, []);
    const confirmDiscard = useCallback(() => {
        setShowConfirmation(false);
        guardRef.current?.onReset();
        guardRef.current = null;
        pendingNavigateRef.current?.();
        pendingNavigateRef.current = null;
    }, []);
    const cancelDiscard = useCallback(() => {
        setShowConfirmation(false);
        pendingNavigateRef.current = null;
    }, []);
    const getIsDirty = useCallback(() => guardRef.current?.isDirty ?? false, []);
    return (_jsxs(DrawerCloseGuardContext, { value: { setGuard, guardedNavigate, getIsDirty }, children: [children, _jsx(AlertDialog, { open: showConfirmation, children: _jsxs(AlertDialogContent, { children: [_jsxs(AlertDialogHeader, { children: [_jsx(AlertDialogTitle, { children: "Discard unsaved changes?" }), _jsx(AlertDialogDescription, { children: "You have unsaved changes. Are you sure you want to discard them?" })] }), _jsxs(AlertDialogFooter, { children: [_jsx(AlertDialogAction, { variant: "destructive", onClick: confirmDiscard, children: "Discard" }), _jsx(AlertDialogCancel, { onClick: cancelDiscard, children: "Keep editing" })] })] }) })] }));
}
/* ---------------------------------------------------------------------------
 * Hook — access the guard from any child component
 * ------------------------------------------------------------------------- */
export function useDrawerCloseGuard() {
    const ctx = useContext(DrawerCloseGuardContext);
    if (!ctx)
        throw new Error('useDrawerCloseGuard must be used within DrawerCloseGuardProvider');
    return ctx;
}
/* ---------------------------------------------------------------------------
 * Convenience hook for drawer components
 *
 * Registers dirty state + reset callback with the guard, and returns an
 * `attemptClose` function that goes through the guard before closing.
 * ------------------------------------------------------------------------- */
export function useGuardedDrawerClose({ open, isDirty, onClose, onReset, }) {
    const { setGuard, guardedNavigate } = useDrawerCloseGuard();
    // Keep the guard in sync with dirty state. Because setGuard writes to a ref,
    // this doesn't trigger re-renders in the provider.
    useEffect(() => {
        if (open) {
            setGuard({ isDirty, onReset });
        }
        else {
            setGuard(null);
        }
        return () => setGuard(null);
    }, [open, isDirty, onReset, setGuard]);
    const attemptClose = useCallback(() => {
        guardedNavigate(() => {
            onReset();
            onClose();
        });
    }, [guardedNavigate, onClose, onReset]);
    return { attemptClose };
}
