import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
/**
 * @license
 * Copyright 2025 Vybestack LLC
 * SPDX-License-Identifier: Apache-2.0
 */
import { render, act } from '@testing-library/react';
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { LayoutManager, useLayout } from '../LayoutManager.js';
// Mock the useTerminalSize hook
vi.mock('../../hooks/useTerminalSize.js', () => ({
    useTerminalSize: vi.fn(() => ({ rows: 24, columns: 80 })),
}));
// Component to test the context values
const TestComponent = () => {
    const layout = useLayout();
    return (_jsxs("div", { children: [_jsx("div", { "data-testid": "terminal-height", children: layout.terminalHeight }), _jsx("div", { "data-testid": "terminal-width", children: layout.terminalWidth }), _jsx("div", { "data-testid": "footer-height", children: layout.footerHeight }), _jsx("div", { "data-testid": "constrain-height", children: String(layout.constrainHeight) }), _jsx("div", { "data-testid": "available-terminal-height", children: layout.availableTerminalHeight })] }));
};
describe('LayoutManager', () => {
    beforeEach(() => {
        vi.clearAllMocks();
    });
    afterEach(() => {
        vi.restoreAllMocks();
    });
    it('provides initial layout values', () => {
        const { getByTestId } = render(_jsx(LayoutManager, { children: _jsx(TestComponent, {}) }));
        expect(getByTestId('terminal-height').textContent).toBe('24');
        expect(getByTestId('terminal-width').textContent).toBe('80');
        expect(getByTestId('footer-height').textContent).toBe('0');
        expect(getByTestId('constrain-height').textContent).toBe('true');
        // availableTerminalHeight = terminalHeight (24) - footerHeight (0) - staticExtraHeight (3) = 21
        expect(getByTestId('available-terminal-height').textContent).toBe('21');
    });
    it('updates available terminal height when footer height changes', () => {
        let setFooterHeight = null;
        const TestWithSetter = () => {
            const layout = useLayout();
            setFooterHeight = layout.setFooterHeight;
            return _jsx(TestComponent, {});
        };
        const { getByTestId } = render(_jsx(LayoutManager, { children: _jsx(TestWithSetter, {}) }));
        // Initial state
        expect(getByTestId('available-terminal-height').textContent).toBe('21');
        // Update footer height
        act(() => {
            setFooterHeight?.(5);
        });
        expect(getByTestId('footer-height').textContent).toBe('5');
        // availableTerminalHeight = terminalHeight (24) - footerHeight (5) - staticExtraHeight (3) = 16
        expect(getByTestId('available-terminal-height').textContent).toBe('16');
    });
    it('updates constrain height when setter is called', () => {
        let setConstrainHeight = null;
        const TestWithSetter = () => {
            const layout = useLayout();
            setConstrainHeight = layout.setConstrainHeight;
            return _jsx(TestComponent, {});
        };
        const { getByTestId } = render(_jsx(LayoutManager, { children: _jsx(TestWithSetter, {}) }));
        // Initial state
        expect(getByTestId('constrain-height').textContent).toBe('true');
        // Update constrain height
        act(() => {
            setConstrainHeight?.(false);
        });
        expect(getByTestId('constrain-height').textContent).toBe('false');
    });
    it('responds to terminal size changes', async () => {
        const { useTerminalSize } = await import('../../hooks/useTerminalSize.js');
        const mockUseTerminalSize = useTerminalSize;
        // Start with initial size
        mockUseTerminalSize.mockReturnValue({ rows: 24, columns: 80 });
        const { getByTestId, rerender } = render(_jsx(LayoutManager, { children: _jsx(TestComponent, {}) }));
        expect(getByTestId('terminal-height').textContent).toBe('24');
        expect(getByTestId('terminal-width').textContent).toBe('80');
        // Simulate terminal resize
        mockUseTerminalSize.mockReturnValue({ rows: 30, columns: 100 });
        rerender(_jsx(LayoutManager, { children: _jsx(TestComponent, {}) }));
        expect(getByTestId('terminal-height').textContent).toBe('30');
        expect(getByTestId('terminal-width').textContent).toBe('100');
        // availableTerminalHeight should update: 30 - 0 - 3 = 27
        expect(getByTestId('available-terminal-height').textContent).toBe('27');
    });
    it('throws error when useLayout is called outside of LayoutManager', () => {
        // Suppress console.error for this test
        const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => { });
        expect(() => {
            render(_jsx(TestComponent, {}));
        }).toThrow('useLayout must be used within LayoutManager');
        consoleSpy.mockRestore();
    });
});
//# sourceMappingURL=LayoutManager.test.js.map