"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("@testing-library/react");
const sidebar_provider_1 = require("./sidebar-provider");
const wrapper = ({ children }) => ((0, jsx_runtime_1.jsx)(sidebar_provider_1.SidebarProvider, { children: children }));
describe('SidebarProvider', () => {
    beforeEach(() => {
        localStorage.clear();
        jest.clearAllMocks();
    });
    describe('initial state', () => {
        it('should initialize with collapsed as false (SSR-safe default)', () => {
            const { result } = (0, react_1.renderHook)(() => (0, sidebar_provider_1.useSidebar)(), { wrapper });
            expect(result.current.isCollapsed).toBe(false);
        });
        it('should initialize with empty items (SSR-safe default)', () => {
            const { result } = (0, react_1.renderHook)(() => (0, sidebar_provider_1.useSidebar)(), { wrapper });
            expect(result.current.items).toEqual({});
        });
    });
    describe('hydration from localStorage', () => {
        it('should load collapsed state from localStorage after hydration', async () => {
            localStorage.setItem('sidebar-collapsed', 'true');
            const { result } = (0, react_1.renderHook)(() => (0, sidebar_provider_1.useSidebar)(), { wrapper });
            await (0, react_1.waitFor)(() => {
                expect(result.current.isCollapsed).toBe(true);
            });
        });
        it('should load items from localStorage after hydration', async () => {
            const storedItems = { 'item-1': true, 'item-2': false };
            localStorage.setItem('sidebar-items', JSON.stringify(storedItems));
            const { result } = (0, react_1.renderHook)(() => (0, sidebar_provider_1.useSidebar)(), { wrapper });
            await (0, react_1.waitFor)(() => {
                expect(result.current.items).toEqual(storedItems);
            });
        });
        it('should handle invalid JSON in localStorage gracefully', async () => {
            localStorage.setItem('sidebar-items', 'invalid-json');
            const { result } = (0, react_1.renderHook)(() => (0, sidebar_provider_1.useSidebar)(), { wrapper });
            await (0, react_1.waitFor)(() => {
                expect(result.current.items).toEqual({});
            });
        });
    });
    describe('toggleSidebar', () => {
        it('should toggle collapsed state from false to true', async () => {
            const { result } = (0, react_1.renderHook)(() => (0, sidebar_provider_1.useSidebar)(), { wrapper });
            expect(result.current.isCollapsed).toBe(false);
            (0, react_1.act)(() => {
                result.current.toggleSidebar();
            });
            expect(result.current.isCollapsed).toBe(true);
        });
        it('should toggle collapsed state from true to false', async () => {
            localStorage.setItem('sidebar-collapsed', 'true');
            const { result } = (0, react_1.renderHook)(() => (0, sidebar_provider_1.useSidebar)(), { wrapper });
            await (0, react_1.waitFor)(() => {
                expect(result.current.isCollapsed).toBe(true);
            });
            (0, react_1.act)(() => {
                result.current.toggleSidebar();
            });
            expect(result.current.isCollapsed).toBe(false);
        });
    });
    describe('getItemCollapsed', () => {
        it('should return false for non-existent item', () => {
            const { result } = (0, react_1.renderHook)(() => (0, sidebar_provider_1.useSidebar)(), { wrapper });
            expect(result.current.getItemCollapsed('non-existent')).toBe(false);
        });
        it('should return the correct value for existing item', async () => {
            localStorage.setItem('sidebar-items', JSON.stringify({ 'my-item': true }));
            const { result } = (0, react_1.renderHook)(() => (0, sidebar_provider_1.useSidebar)(), { wrapper });
            await (0, react_1.waitFor)(() => {
                expect(result.current.getItemCollapsed('my-item')).toBe(true);
            });
        });
    });
    describe('setItemCollapsed', () => {
        it('should set item collapsed state', () => {
            const { result } = (0, react_1.renderHook)(() => (0, sidebar_provider_1.useSidebar)(), { wrapper });
            (0, react_1.act)(() => {
                result.current.setItemCollapsed('new-item', true);
            });
            expect(result.current.items['new-item']).toBe(true);
            expect(result.current.getItemCollapsed('new-item')).toBe(true);
        });
        it('should update existing item collapsed state', async () => {
            localStorage.setItem('sidebar-items', JSON.stringify({ 'existing-item': true }));
            const { result } = (0, react_1.renderHook)(() => (0, sidebar_provider_1.useSidebar)(), { wrapper });
            await (0, react_1.waitFor)(() => {
                expect(result.current.getItemCollapsed('existing-item')).toBe(true);
            });
            (0, react_1.act)(() => {
                result.current.setItemCollapsed('existing-item', false);
            });
            expect(result.current.getItemCollapsed('existing-item')).toBe(false);
        });
        it('should preserve other items when setting a new item', async () => {
            localStorage.setItem('sidebar-items', JSON.stringify({ 'item-1': true, 'item-2': false }));
            const { result } = (0, react_1.renderHook)(() => (0, sidebar_provider_1.useSidebar)(), { wrapper });
            await (0, react_1.waitFor)(() => {
                expect(result.current.items).toEqual({
                    'item-1': true,
                    'item-2': false
                });
            });
            (0, react_1.act)(() => {
                result.current.setItemCollapsed('item-3', true);
            });
            expect(result.current.items).toEqual({
                'item-1': true,
                'item-2': false,
                'item-3': true
            });
        });
    });
    describe('localStorage persistence', () => {
        it('should persist collapsed state to localStorage after change', async () => {
            const { result } = (0, react_1.renderHook)(() => (0, sidebar_provider_1.useSidebar)(), { wrapper });
            // Wait for hydration
            await (0, react_1.waitFor)(() => {
                expect(localStorage.getItem('sidebar-collapsed')).toBeDefined();
            });
            (0, react_1.act)(() => {
                result.current.toggleSidebar();
            });
            await (0, react_1.waitFor)(() => {
                expect(localStorage.getItem('sidebar-collapsed')).toBe('true');
            });
        });
        it('should persist items to localStorage after change', async () => {
            const { result } = (0, react_1.renderHook)(() => (0, sidebar_provider_1.useSidebar)(), { wrapper });
            // Wait for hydration
            await (0, react_1.waitFor)(() => {
                expect(localStorage.getItem('sidebar-items')).toBeDefined();
            });
            (0, react_1.act)(() => {
                result.current.setItemCollapsed('persisted-item', true);
            });
            await (0, react_1.waitFor)(() => {
                const stored = JSON.parse(localStorage.getItem('sidebar-items') || '{}');
                expect(stored['persisted-item']).toBe(true);
            });
        });
        it('should not overwrite localStorage before hydration completes', async () => {
            const initialItems = { 'pre-existing': true };
            localStorage.setItem('sidebar-items', JSON.stringify(initialItems));
            (0, react_1.renderHook)(() => (0, sidebar_provider_1.useSidebar)(), { wrapper });
            // The initial localStorage value should be preserved and loaded
            await (0, react_1.waitFor)(() => {
                const stored = JSON.parse(localStorage.getItem('sidebar-items') || '{}');
                expect(stored['pre-existing']).toBe(true);
            });
        });
    });
    describe('useSidebar hook', () => {
        it('should throw error when used outside SidebarProvider', () => {
            // Suppress console.error for this test
            const consoleSpy = jest
                .spyOn(console, 'error')
                .mockImplementation(() => { });
            expect(() => {
                (0, react_1.renderHook)(() => (0, sidebar_provider_1.useSidebar)());
            }).toThrow('useSidebar must be used within a SidebarProvider');
            consoleSpy.mockRestore();
        });
    });
});
