"use client";
import { useCallback, useState } from "react";
const VALID_MODES = new Set([
    "table",
    "cards",
    "list",
]);
function isValidMode(value) {
    return VALID_MODES.has(value);
}
/**
 * Persists the user's preferred view mode (table / cards / list) in
 * localStorage, scoped by a caller-provided storage key.
 *
 * Falls back to `defaultMode` when no persisted value exists or when
 * the stored value is invalid (forward-compatible with future modes).
 *
 * Pass `undefined` for `storageKey` to use an ephemeral (non-persisted)
 * view mode that resets on unmount.
 *
 * @param storageKey  localStorage key (e.g. `"stigmer:workbench:agents:viewMode"`).
 *                    Pass `undefined` to disable persistence.
 * @param defaultMode The view mode to use when no persisted value exists.
 *
 * @example
 * ```tsx
 * const { viewMode, setViewMode } = useViewPreference(
 *   "stigmer:workbench:agents:viewMode",
 *   "table",
 * );
 * ```
 */
export function useViewPreference(storageKey, defaultMode) {
    const [viewMode, setViewModeState] = useState(() => {
        if (!storageKey || typeof window === "undefined")
            return defaultMode;
        try {
            const stored = localStorage.getItem(storageKey);
            if (stored && isValidMode(stored))
                return stored;
        }
        catch {
            // localStorage may be unavailable (e.g. sandboxed iframe).
        }
        return defaultMode;
    });
    const setViewMode = useCallback((mode) => {
        setViewModeState(mode);
        if (!storageKey)
            return;
        try {
            localStorage.setItem(storageKey, mode);
        }
        catch {
            // Silently ignore write failures.
        }
    }, [storageKey]);
    return { viewMode, setViewMode };
}
//# sourceMappingURL=useViewPreference.js.map