"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = __importDefault(require("react"));
const react_2 = require("@testing-library/react");
require("@testing-library/jest-dom");
const components_1 = require("../components");
// Mock user data
const mockUsers = [
    {
        id: 'user1',
        name: 'Alice Johnson',
        email: 'alice@example.com',
        color: '#FF6B6B',
        cursor: { field: 'title', position: 5 }
    },
    {
        id: 'user2',
        name: 'Bob Smith',
        email: 'bob@example.com',
        color: '#4ECDC4'
    },
    {
        id: 'user3',
        name: 'Carol Davis',
        email: 'carol@example.com',
        color: '#45B7D1'
    }
];
const mockAwareness = [
    {
        userId: 'user1',
        userName: 'Alice Johnson',
        color: '#FF6B6B',
        section: 'test-section',
        activity: 'editing',
        lastActivity: new Date()
    }
];
const mockChanges = [
    {
        id: 'change-1',
        userId: 'user1',
        userName: 'Alice Johnson',
        timestamp: new Date(Date.now() - 5 * 60 * 1000),
        field: 'title',
        oldValue: 'Old Title',
        newValue: 'New Title',
        type: 'update',
        metadata: { source: 'ui' }
    },
    {
        id: 'change-2',
        userId: 'user2',
        userName: 'Bob Smith',
        timestamp: new Date(Date.now() - 15 * 60 * 1000),
        field: 'status',
        oldValue: 'pending',
        newValue: 'in-progress',
        type: 'update',
        metadata: { source: 'api' }
    }
];
describe('Collaboration Components', () => {
    describe('UserPresence', () => {
        test('renders user avatars correctly', () => {
            (0, react_2.render)(<components_1.UserPresence users={mockUsers}/>);
            expect(react_2.screen.getByText('AJ')).toBeInTheDocument(); // Alice's initials
            expect(react_2.screen.getByText('BS')).toBeInTheDocument(); // Bob's initials
            expect(react_2.screen.getByText('CD')).toBeInTheDocument(); // Carol's initials
        });
        test('shows typing indicator for users with cursors', () => {
            (0, react_2.render)(<components_1.UserPresence users={mockUsers} showCursors={true}/>);
            // Should show typing indicator for user1 who has cursor
            const typingIndicators = document.querySelectorAll('.user-presence__typing-indicator');
            expect(typingIndicators).toHaveLength(1);
        });
        test('limits displayed users when maxDisplay is set', () => {
            (0, react_2.render)(<components_1.UserPresence users={mockUsers} maxDisplay={2}/>);
            // Should show 2 users + "more" indicator
            const moreIndicator = react_2.screen.getByText('+1');
            expect(moreIndicator).toBeInTheDocument();
        });
        test('displays user names on hover', () => {
            (0, react_2.render)(<components_1.UserPresence users={mockUsers}/>);
            const userAvatar = react_2.screen.getByText('AJ').closest('.user-presence__item');
            expect(userAvatar).toHaveAttribute('title', 'Alice Johnson (alice@example.com)');
        });
    });
    describe('CollaborativeField', () => {
        const defaultProps = {
            fieldName: 'title',
            value: 'Test Value',
            onChange: jest.fn(),
            onCursorChange: jest.fn(),
            cursors: []
        };
        beforeEach(() => {
            jest.clearAllMocks();
        });
        test('renders input field correctly', () => {
            (0, react_2.render)(<components_1.CollaborativeField {...defaultProps}/>);
            const input = react_2.screen.getByDisplayValue('Test Value');
            expect(input).toBeInTheDocument();
        });
        test('renders textarea when multiline is true', () => {
            (0, react_2.render)(<components_1.CollaborativeField {...defaultProps} multiline={true}/>);
            const textarea = react_2.screen.getByDisplayValue('Test Value');
            expect(textarea.tagName).toBe('TEXTAREA');
        });
        test('calls onChange when value changes', () => {
            (0, react_2.render)(<components_1.CollaborativeField {...defaultProps}/>);
            const input = react_2.screen.getByDisplayValue('Test Value');
            react_2.fireEvent.change(input, { target: { value: 'New Value' } });
            expect(defaultProps.onChange).toHaveBeenCalledWith('New Value');
        });
        test('shows placeholder when no value', () => {
            (0, react_2.render)(<components_1.CollaborativeField {...defaultProps} value="" placeholder="Enter text..."/>);
            const input = react_2.screen.getByPlaceholderText('Enter text...');
            expect(input).toBeInTheDocument();
        });
        test('shows cursor indicators', () => {
            const cursors = [
                {
                    userId: 'user1',
                    field: 'title',
                    position: 5,
                    color: '#FF6B6B',
                    userName: 'Alice'
                }
            ];
            (0, react_2.render)(<components_1.CollaborativeField {...defaultProps} cursors={cursors}/>);
            // Cursor indicator should be present (implementation-specific)
            const field = react_2.screen.getByDisplayValue('Test Value').parentElement;
            expect(field).toBeInTheDocument();
        });
        test('is disabled when disabled prop is true', () => {
            (0, react_2.render)(<components_1.CollaborativeField {...defaultProps} disabled={true}/>);
            const input = react_2.screen.getByDisplayValue('Test Value');
            expect(input).toBeDisabled();
        });
    });
    describe('AwarenessIndicator', () => {
        test('renders awareness indicator with users', () => {
            (0, react_2.render)(<components_1.AwarenessIndicator section="test-section" awareness={mockAwareness} currentUserId="current-user"/>);
            // Should show user avatar
            const avatar = react_2.screen.getByText('A'); // Alice's initial
            expect(avatar).toBeInTheDocument();
        });
        test('does not render when no awareness data', () => {
            const { container } = (0, react_2.render)(<components_1.AwarenessIndicator section="test-section" awareness={[]} currentUserId="current-user"/>);
            expect(container.firstChild).toBeNull();
        });
        test('shows tooltip on hover', async () => {
            (0, react_2.render)(<components_1.AwarenessIndicator section="test-section" awareness={mockAwareness} currentUserId="current-user"/>);
            const indicator = document.querySelector('.awareness-indicator');
            if (indicator) {
                react_2.fireEvent.mouseEnter(indicator);
                await (0, react_2.waitFor)(() => {
                    expect(react_2.screen.getByText('Active in this section')).toBeInTheDocument();
                });
            }
        });
    });
    describe('ChangeHistory', () => {
        test('renders change history list', () => {
            (0, react_2.render)(<components_1.ChangeHistory changes={mockChanges}/>);
            expect(react_2.screen.getByText('Alice Johnson')).toBeInTheDocument();
            expect(react_2.screen.getByText('Bob Smith')).toBeInTheDocument();
            expect(react_2.screen.getByText('Title')).toBeInTheDocument();
            expect(react_2.screen.getByText('Status')).toBeInTheDocument();
        });
        test('shows filter inputs', () => {
            (0, react_2.render)(<components_1.ChangeHistory changes={mockChanges}/>);
            expect(react_2.screen.getByPlaceholderText('Filter by field...')).toBeInTheDocument();
            expect(react_2.screen.getByPlaceholderText('Filter by user...')).toBeInTheDocument();
        });
        test('calls onRevert when revert button is clicked', () => {
            const onRevert = jest.fn();
            (0, react_2.render)(<components_1.ChangeHistory changes={mockChanges} onRevert={onRevert}/>);
            const revertButtons = react_2.screen.getAllByTitle(/Revert this change/i);
            react_2.fireEvent.click(revertButtons[0]);
            expect(onRevert).toHaveBeenCalledWith('change-1');
        });
        test('allows change selection for comparison', () => {
            const onCompare = jest.fn();
            (0, react_2.render)(<components_1.ChangeHistory changes={mockChanges} onCompare={onCompare}/>);
            const checkboxes = react_2.screen.getAllByRole('checkbox');
            react_2.fireEvent.click(checkboxes[0]);
            react_2.fireEvent.click(checkboxes[1]);
            expect(onCompare).toHaveBeenCalledWith('change-1', 'change-2');
        });
        test('expands change details when clicked', () => {
            (0, react_2.render)(<components_1.ChangeHistory changes={mockChanges} showDiff={true}/>);
            const changeItem = react_2.screen.getByText('Alice Johnson').closest('.change-history__item-header');
            if (changeItem) {
                react_2.fireEvent.click(changeItem);
                // Should show diff content
                expect(react_2.screen.getByText('Old:')).toBeInTheDocument();
                expect(react_2.screen.getByText('New:')).toBeInTheDocument();
            }
        });
        test('filters changes by field', () => {
            (0, react_2.render)(<components_1.ChangeHistory changes={mockChanges}/>);
            const fieldFilter = react_2.screen.getByPlaceholderText('Filter by field...');
            react_2.fireEvent.change(fieldFilter, { target: { value: 'title' } });
            // Should still show Alice's change (title change)
            expect(react_2.screen.getByText('Alice Johnson')).toBeInTheDocument();
        });
        test('filters changes by user', () => {
            (0, react_2.render)(<components_1.ChangeHistory changes={mockChanges}/>);
            const userFilter = react_2.screen.getByPlaceholderText('Filter by user...');
            react_2.fireEvent.change(userFilter, { target: { value: 'Alice' } });
            // Should show Alice's change
            expect(react_2.screen.getByText('Alice Johnson')).toBeInTheDocument();
        });
        test('shows empty state when no changes', () => {
            (0, react_2.render)(<components_1.ChangeHistory changes={[]}/>);
            expect(react_2.screen.getByText('No changes found')).toBeInTheDocument();
        });
        test('limits displayed changes', () => {
            const manyChanges = Array(100).fill(null).map((_, i) => ({
                ...mockChanges[0],
                id: `change-${i}`,
                timestamp: new Date(Date.now() - i * 60 * 1000)
            }));
            (0, react_2.render)(<components_1.ChangeHistory changes={manyChanges} maxDisplay={2}/>);
            // Should only render 2 changes
            const changeItems = document.querySelectorAll('.change-history__item');
            expect(changeItems).toHaveLength(2);
        });
    });
});
//# sourceMappingURL=components.test.js.map