"use strict";
/**
 * useLayoutPersistence hook
 *
 * Custom hook to persist layout settings in localStorage
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.useLayoutPersistence = useLayoutPersistence;
const react_1 = require("react");
/**
 * Custom hook to persist layout settings in localStorage
 * @param {string} key - The key to use for storage
 * @param {any} initialValue - The initial value to use if no value exists in storage
 * @returns {[any, function]} The current value and a setter function
 *
 * @example
 * const [sidebarCollapsed, setSidebarCollapsed] = useLayoutPersistence('sidebarCollapsed', false);
 *
 * // Toggle sidebar
 * const toggleSidebar = () => setSidebarCollapsed(!sidebarCollapsed);
 */
function useLayoutPersistence(key, initialValue) {
    // Get stored value from localStorage
    const getStoredValue = () => {
        if (typeof window === 'undefined') {
            return initialValue;
        }
        try {
            const item = window.localStorage.getItem(`bf-layout-${key}`);
            return item ? JSON.parse(item) : initialValue;
        }
        catch (error) {
            console.error(`Error retrieving layout preference for ${key}:`, error);
            return initialValue;
        }
    };
    // State to store the current value
    const [value, setValue] = (0, react_1.useState)(getStoredValue);
    // Update localStorage when the value changes
    (0, react_1.useEffect)(() => {
        if (typeof window !== 'undefined') {
            try {
                window.localStorage.setItem(`bf-layout-${key}`, JSON.stringify(value));
            }
            catch (error) {
                console.error(`Error storing layout preference for ${key}:`, error);
            }
        }
    }, [key, value]);
    return [value, setValue];
}
exports.default = useLayoutPersistence;
//# sourceMappingURL=useLayoutPersistence.js.map