var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import '@testing-library/jest-dom';
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
import { ChatbotDisplayMode } from '../Chatbot/Chatbot';
import ChatbotConversationHistoryNav from './ChatbotConversationHistoryNav';
import { EmptyStateStatus, Spinner } from '@patternfly/react-core';
import { BellIcon, OutlinedCommentsIcon, SearchIcon } from '@patternfly/react-icons';
const ERROR = {
    bodyText: (_jsxs(_Fragment, { children: ["To try again, check your connection and reload this page. If the issue persists,", ' ', _jsx("a", { href: "", children: "contact the support team" }), "."] })),
    buttonText: 'Reload',
    buttonIcon: _jsx(Spinner, { size: "sm" }),
    hasButton: true,
    titleText: 'Could not load chat history',
    status: EmptyStateStatus.danger,
    onClick: () => alert('Clicked Reload')
};
const NO_RESULTS = {
    bodyText: 'Adjust your search query and try again. Check your spelling or try a more general term.',
    titleText: 'No results found',
    icon: SearchIcon
};
const EMPTY_STATE = {
    bodyText: 'Access timely assistance by starting a conversation with an AI model.',
    titleText: 'Start a new chat',
    icon: OutlinedCommentsIcon
};
const ERROR_WITHOUT_BUTTON = {
    bodyText: (_jsxs(_Fragment, { children: ["To try again, check your connection and reload this page. If the issue persists,", ' ', _jsx("a", { href: "", children: "contact the support team" }), "."] })),
    buttonText: 'Reload',
    buttonIcon: _jsx(Spinner, { size: "sm" }),
    hasButton: false,
    titleText: 'Could not load chat history',
    status: EmptyStateStatus.danger,
    onClick: () => alert('Clicked Reload')
};
describe('ChatbotConversationHistoryNav', () => {
    const onDrawerToggle = jest.fn();
    const initialConversations = [
        {
            id: '1',
            text: 'ChatBot documentation'
        }
    ];
    it('should open the conversation history navigation drawer', () => {
        render(_jsx(ChatbotConversationHistoryNav, { onDrawerToggle: onDrawerToggle, isDrawerOpen: true, displayMode: ChatbotDisplayMode.fullscreen, setIsDrawerOpen: jest.fn(), conversations: initialConversations }));
        expect(screen.queryByText('ChatBot documentation')).toBeInTheDocument();
    });
    it('should display the conversations for grouped conversations', () => {
        const groupedConversations = {
            Today: [...initialConversations, { id: '2', text: 'Chatbot extension' }]
        };
        render(_jsx(ChatbotConversationHistoryNav, { onDrawerToggle: onDrawerToggle, isDrawerOpen: true, displayMode: ChatbotDisplayMode.fullscreen, setIsDrawerOpen: jest.fn(), conversations: groupedConversations }));
        expect(screen.queryByText('Chatbot extension')).toBeInTheDocument();
    });
    it('should apply the reversed class when reverseButtonOrder is true', () => {
        render(_jsx(ChatbotConversationHistoryNav, { onDrawerToggle: onDrawerToggle, isDrawerOpen: true, displayMode: ChatbotDisplayMode.fullscreen, setIsDrawerOpen: jest.fn(), reverseButtonOrder: true, conversations: initialConversations }));
        expect(screen.getByTestId('chatbot-nav-drawer-actions')).toHaveClass('pf-v6-c-drawer__actions--reversed');
    });
    it('should disable new chat button', () => {
        render(_jsx(ChatbotConversationHistoryNav, { onDrawerToggle: onDrawerToggle, isDrawerOpen: true, displayMode: ChatbotDisplayMode.fullscreen, setIsDrawerOpen: jest.fn(), reverseButtonOrder: true, conversations: initialConversations, newChatButtonProps: { isDisabled: true }, onNewChat: jest.fn() }));
        expect(screen.getByRole('button', { name: 'New chat' })).toBeDisabled();
    });
    it('should not apply the reversed class when reverseButtonOrder is false', () => {
        render(_jsx(ChatbotConversationHistoryNav, { onDrawerToggle: onDrawerToggle, isDrawerOpen: true, displayMode: ChatbotDisplayMode.fullscreen, setIsDrawerOpen: jest.fn(), reverseButtonOrder: false, conversations: initialConversations }));
        expect(screen.getByTestId('chatbot-nav-drawer-actions')).not.toHaveClass('pf-v6-c-drawer__actions--reversed');
    });
    it('should invoke handleTextInputChange callback when user searches for conversations', () => {
        const handleSearch = jest.fn();
        const groupedConversations = {
            Today: [...initialConversations, { id: '2', text: 'Chatbot extension' }]
        };
        render(_jsx(ChatbotConversationHistoryNav, { onDrawerToggle: onDrawerToggle, isDrawerOpen: true, displayMode: ChatbotDisplayMode.fullscreen, setIsDrawerOpen: jest.fn(), reverseButtonOrder: false, conversations: groupedConversations, handleTextInputChange: handleSearch }));
        const searchInput = screen.getByPlaceholderText(/Search/i);
        fireEvent.change(searchInput, { target: { value: 'Chatbot' } });
        expect(handleSearch).toHaveBeenCalledWith('Chatbot');
    });
    it('should close the drawer when escape key is pressed', () => __awaiter(void 0, void 0, void 0, function* () {
        render(_jsx(ChatbotConversationHistoryNav, { onDrawerToggle: onDrawerToggle, isDrawerOpen: true, displayMode: ChatbotDisplayMode.fullscreen, setIsDrawerOpen: jest.fn(), reverseButtonOrder: false, handleTextInputChange: jest.fn(), conversations: initialConversations }));
        fireEvent.keyDown(screen.getByPlaceholderText(/Search/i), {
            key: 'Escape',
            code: 'Escape',
            keyCode: 27,
            charCode: 27
        });
        waitFor(() => {
            expect(screen.queryByText('ChatBot documentation')).not.toBeInTheDocument();
        });
    }));
    it('should be resizable', () => {
        render(_jsx(ChatbotConversationHistoryNav, { onDrawerToggle: onDrawerToggle, isDrawerOpen: true, displayMode: ChatbotDisplayMode.fullscreen, setIsDrawerOpen: jest.fn(), conversations: initialConversations, drawerPanelContentProps: { isResizable: true, minSize: '200px' } }));
        expect(screen.getByRole('dialog', { name: /Resize/i })).toBeTruthy();
        expect(screen.getByRole('separator', { name: /Resize/i })).toBeTruthy();
        expect(screen.getByRole('dialog', { name: /Resize/i })).toHaveAttribute('style', '--pf-v6-c-drawer__panel--md--FlexBasis: 384px; --pf-v6-c-drawer__panel--md--FlexBasis--min: 200px;');
    });
    it('should accept drawerContentProps', () => {
        const { container } = render(_jsx(ChatbotConversationHistoryNav, { onDrawerToggle: onDrawerToggle, isDrawerOpen: true, displayMode: ChatbotDisplayMode.fullscreen, setIsDrawerOpen: jest.fn(), conversations: initialConversations, drawerContentProps: { className: 'test' } }));
        const element = container.querySelector('.test');
        expect(element).toBeInTheDocument();
    });
    it('should accept drawerContentBodyProps', () => {
        const { container } = render(_jsx(ChatbotConversationHistoryNav, { onDrawerToggle: onDrawerToggle, isDrawerOpen: true, displayMode: ChatbotDisplayMode.fullscreen, setIsDrawerOpen: jest.fn(), conversations: initialConversations, drawerContentBodyProps: { className: 'test' } }));
        const element = container.querySelector('.test');
        expect(element).toBeInTheDocument();
    });
    it('should accept drawerHeadProps', () => {
        const { container } = render(_jsx(ChatbotConversationHistoryNav, { onDrawerToggle: onDrawerToggle, isDrawerOpen: true, displayMode: ChatbotDisplayMode.fullscreen, setIsDrawerOpen: jest.fn(), conversations: initialConversations, drawerHeadProps: { className: 'test' } }));
        const element = container.querySelector('.test');
        expect(element).toBeInTheDocument();
    });
    it('should accept drawerActionsProps', () => {
        const { container } = render(_jsx(ChatbotConversationHistoryNav, { onDrawerToggle: onDrawerToggle, isDrawerOpen: true, displayMode: ChatbotDisplayMode.fullscreen, setIsDrawerOpen: jest.fn(), conversations: initialConversations, drawerActionsProps: { className: 'test' } }));
        const element = container.querySelector('.test');
        expect(element).toBeInTheDocument();
    });
    it('should accept drawerCloseButtonProps', () => {
        const { container } = render(_jsx(ChatbotConversationHistoryNav, { onDrawerToggle: onDrawerToggle, isDrawerOpen: true, displayMode: ChatbotDisplayMode.fullscreen, setIsDrawerOpen: jest.fn(), conversations: initialConversations, drawerCloseButtonProps: { className: 'test' } }));
        const element = container.querySelector('.test');
        expect(element).toBeInTheDocument();
    });
    it('should accept drawerPanelBodyProps', () => {
        const { container } = render(_jsx(ChatbotConversationHistoryNav, { onDrawerToggle: onDrawerToggle, isDrawerOpen: true, displayMode: ChatbotDisplayMode.fullscreen, setIsDrawerOpen: jest.fn(), conversations: initialConversations, drawerPanelBodyProps: { className: 'test' } }));
        const element = container.querySelector('.test');
        expect(element).toBeInTheDocument();
    });
    it('should show loading state if triggered', () => {
        render(_jsx(ChatbotConversationHistoryNav, { onDrawerToggle: onDrawerToggle, isDrawerOpen: true, displayMode: ChatbotDisplayMode.fullscreen, setIsDrawerOpen: jest.fn(), reverseButtonOrder: false, handleTextInputChange: jest.fn(), conversations: initialConversations, isLoading: true }));
        expect(screen.getByRole('dialog', { name: /Loading chatbot conversation history/i })).toBeTruthy();
        expect(screen.getByRole('button', { name: /Close drawer panel/i })).toBeTruthy();
    });
    it('should pass alternative aria label to loading state', () => {
        render(_jsx(ChatbotConversationHistoryNav, { onDrawerToggle: onDrawerToggle, isDrawerOpen: true, displayMode: ChatbotDisplayMode.fullscreen, setIsDrawerOpen: jest.fn(), reverseButtonOrder: false, handleTextInputChange: jest.fn(), conversations: initialConversations, isLoading: true, loadingState: { screenreaderText: 'I am a test' } }));
        expect(screen.getByRole('dialog', { name: /I am a test/i })).toBeTruthy();
    });
    it('should accept errorState', () => {
        render(_jsx(ChatbotConversationHistoryNav, { onDrawerToggle: onDrawerToggle, isDrawerOpen: true, displayMode: ChatbotDisplayMode.fullscreen, setIsDrawerOpen: jest.fn(), reverseButtonOrder: false, handleTextInputChange: jest.fn(), conversations: initialConversations, errorState: ERROR }));
        expect(screen.getByRole('dialog', {
            name: /Could not load chat history To try again, check your connection and reload this page. If the issue persists, contact the support team . Loading... Reload/i
        })).toBeTruthy();
        expect(screen.getByRole('button', { name: /Close drawer panel/i })).toBeTruthy();
        expect(screen.getByRole('button', { name: /Loading... Reload/i })).toBeTruthy();
        expect(screen.getByRole('textbox', { name: /Search previous conversations/i })).toBeTruthy();
        expect(screen.getByRole('heading', { name: /Could not load chat history/i })).toBeTruthy();
    });
    it('should accept errorState without button', () => {
        render(_jsx(ChatbotConversationHistoryNav, { onDrawerToggle: onDrawerToggle, isDrawerOpen: true, displayMode: ChatbotDisplayMode.fullscreen, setIsDrawerOpen: jest.fn(), reverseButtonOrder: false, handleTextInputChange: jest.fn(), conversations: initialConversations, errorState: ERROR_WITHOUT_BUTTON }));
        expect(screen.getByRole('dialog', {
            name: /Could not load chat history To try again, check your connection and reload this page. If the issue persists, contact the support team ./i
        })).toBeTruthy();
        expect(screen.getByRole('button', { name: /Close drawer panel/i })).toBeTruthy();
        expect(screen.queryByRole('button', { name: /Loading... Reload/i })).toBeFalsy();
        expect(screen.getByRole('textbox', { name: /Search previous conversations/i })).toBeTruthy();
        expect(screen.getByRole('heading', { name: /Could not load chat history/i })).toBeTruthy();
    });
    it('should show loading state over error state if both are supplied', () => {
        render(_jsx(ChatbotConversationHistoryNav, { onDrawerToggle: onDrawerToggle, isDrawerOpen: true, displayMode: ChatbotDisplayMode.fullscreen, setIsDrawerOpen: jest.fn(), reverseButtonOrder: false, handleTextInputChange: jest.fn(), conversations: initialConversations, isLoading: true, errorState: ERROR }));
        expect(screen.getByRole('dialog', { name: /Loading/i })).toBeTruthy();
    });
    it('should accept emptyState', () => {
        render(_jsx(ChatbotConversationHistoryNav, { onDrawerToggle: onDrawerToggle, isDrawerOpen: true, displayMode: ChatbotDisplayMode.fullscreen, setIsDrawerOpen: jest.fn(), reverseButtonOrder: false, handleTextInputChange: jest.fn(), conversations: initialConversations, emptyState: EMPTY_STATE }));
        expect(screen.getByRole('dialog', {
            name: /Start a new chat Access timely assistance by starting a conversation with an AI model./i
        })).toBeTruthy();
    });
    it('should accept no results state', () => {
        render(_jsx(ChatbotConversationHistoryNav, { onDrawerToggle: onDrawerToggle, isDrawerOpen: true, displayMode: ChatbotDisplayMode.fullscreen, setIsDrawerOpen: jest.fn(), reverseButtonOrder: false, handleTextInputChange: jest.fn(), conversations: initialConversations, noResultsState: NO_RESULTS }));
        expect(screen.getByRole('dialog', {
            name: /No results found Adjust your search query and try again. Check your spelling or try a more general term./i
        })).toBeTruthy();
    });
    it('should handle isCompact', () => {
        render(_jsx(ChatbotConversationHistoryNav, { onDrawerToggle: onDrawerToggle, isDrawerOpen: true, displayMode: ChatbotDisplayMode.fullscreen, setIsDrawerOpen: jest.fn(), reverseButtonOrder: false, handleTextInputChange: jest.fn(), conversations: initialConversations, noResultsState: NO_RESULTS, isCompact: true, "data-testid": "drawer" }));
        expect(screen.getByTestId('drawer')).toHaveClass('pf-m-compact');
    });
    it('should display the default title', () => {
        render(_jsx(ChatbotConversationHistoryNav, { onDrawerToggle: onDrawerToggle, isDrawerOpen: true, displayMode: ChatbotDisplayMode.fullscreen, setIsDrawerOpen: jest.fn(), conversations: initialConversations }));
        expect(screen.getByText('Chat history')).toBeInTheDocument();
    });
    it('should display the custom title', () => {
        render(_jsx(ChatbotConversationHistoryNav, { title: "PatternFly history", onDrawerToggle: onDrawerToggle, isDrawerOpen: true, displayMode: ChatbotDisplayMode.fullscreen, setIsDrawerOpen: jest.fn(), conversations: initialConversations }));
        expect(screen.getByText('PatternFly history')).toBeInTheDocument();
    });
    it('should display the clock icon', () => {
        const { container } = render(_jsx(ChatbotConversationHistoryNav, { onDrawerToggle: onDrawerToggle, isDrawerOpen: true, displayMode: ChatbotDisplayMode.fullscreen, setIsDrawerOpen: jest.fn(), conversations: initialConversations }));
        const iconElement = container.querySelector('.pf-chatbot__title-icon');
        expect(iconElement).toBeInTheDocument();
    });
    it('Passes menuProps to Menu', () => {
        var _a;
        render(_jsx(ChatbotConversationHistoryNav, { onDrawerToggle: onDrawerToggle, isDrawerOpen: true, displayMode: ChatbotDisplayMode.fullscreen, setIsDrawerOpen: jest.fn(), conversations: initialConversations, menuProps: { className: 'test' } }));
        expect((_a = screen.getByRole('menu').parentElement) === null || _a === void 0 ? void 0 : _a.parentElement).toHaveClass('test');
    });
    it('Passes menuContentProps to MenuContent', () => {
        render(_jsx(ChatbotConversationHistoryNav, { onDrawerToggle: onDrawerToggle, isDrawerOpen: true, displayMode: ChatbotDisplayMode.fullscreen, setIsDrawerOpen: jest.fn(), conversations: initialConversations, menuContentProps: { className: 'test' } }));
        expect(screen.getByRole('menu').parentElement).toHaveClass('test');
    });
    it('Passes menuListProps to MenuList when conversations is an array', () => {
        render(_jsx(ChatbotConversationHistoryNav, { onDrawerToggle: onDrawerToggle, isDrawerOpen: true, displayMode: ChatbotDisplayMode.fullscreen, setIsDrawerOpen: jest.fn(), conversations: initialConversations, menuListProps: { className: 'test' } }));
        expect(screen.getByRole('menu')).toHaveClass('test');
    });
    it('Passes menuListProps to MenuList when conversations is an object', () => {
        render(_jsx(ChatbotConversationHistoryNav, { onDrawerToggle: onDrawerToggle, isDrawerOpen: true, displayMode: ChatbotDisplayMode.fullscreen, setIsDrawerOpen: jest.fn(), conversations: { Today: initialConversations }, menuListProps: { Today: { className: 'test' } } }));
        expect(screen.getByRole('menu')).toHaveClass('test');
    });
    it('Passes menuGroupProps to MenuGroup when conversations is an object', () => {
        render(_jsx(ChatbotConversationHistoryNav, { onDrawerToggle: onDrawerToggle, isDrawerOpen: true, displayMode: ChatbotDisplayMode.fullscreen, setIsDrawerOpen: jest.fn(), conversations: { Today: initialConversations }, menuGroupProps: { Today: { className: 'test' } } }));
        expect(screen.getByRole('menu').parentElement).toHaveClass('test');
    });
    it('Passes additionalProps to MenuItem', () => {
        render(_jsx(ChatbotConversationHistoryNav, { onDrawerToggle: onDrawerToggle, isDrawerOpen: true, displayMode: ChatbotDisplayMode.fullscreen, setIsDrawerOpen: jest.fn(), conversations: [{ id: '1', text: 'ChatBot documentation', additionalProps: { className: 'test' } }] }));
        expect(screen.getByRole('menuitem')).toHaveClass('test');
    });
    it('should be able to spread search input props when searchInputProps is passed', () => {
        render(_jsx(ChatbotConversationHistoryNav, { onDrawerToggle: onDrawerToggle, isDrawerOpen: true, displayMode: ChatbotDisplayMode.fullscreen, setIsDrawerOpen: jest.fn(), conversations: initialConversations, handleTextInputChange: jest.fn(), searchInputProps: { value: 'I am a sample search' } }));
        expect(screen.getByRole('dialog', { name: /Chat history I am a sample search/i })).toBeInTheDocument();
    });
    it('Does not render search actions by default', () => {
        var _a, _b;
        const handleSearch = jest.fn();
        const groupedConversations = {
            Today: [...initialConversations, { id: '2', text: 'Chatbot extension' }]
        };
        render(_jsx(ChatbotConversationHistoryNav, { onDrawerToggle: onDrawerToggle, isDrawerOpen: true, displayMode: ChatbotDisplayMode.fullscreen, setIsDrawerOpen: jest.fn(), reverseButtonOrder: false, conversations: groupedConversations, handleTextInputChange: handleSearch }));
        const searchInput = screen.getByPlaceholderText(/Search/i);
        expect((_a = searchInput.parentElement) === null || _a === void 0 ? void 0 : _a.previousElementSibling).toBeNull();
        expect((_b = searchInput.parentElement) === null || _b === void 0 ? void 0 : _b.nextElementSibling).toBeNull();
    });
    it('Renders with action at start when searchActionStart is passed', () => {
        const handleSearch = jest.fn();
        const groupedConversations = {
            Today: [...initialConversations, { id: '2', text: 'Chatbot extension' }]
        };
        render(_jsx(ChatbotConversationHistoryNav, { onDrawerToggle: onDrawerToggle, isDrawerOpen: true, displayMode: ChatbotDisplayMode.fullscreen, setIsDrawerOpen: jest.fn(), reverseButtonOrder: false, conversations: groupedConversations, handleTextInputChange: handleSearch, searchActionStart: _jsx("div", { children: "Search action start test" }) }));
        expect(screen.getByText('Search action start test')).toBeVisible();
    });
    it('Renders with action at end when searchActionEnd is passed', () => {
        const handleSearch = jest.fn();
        const groupedConversations = {
            Today: [...initialConversations, { id: '2', text: 'Chatbot extension' }]
        };
        render(_jsx(ChatbotConversationHistoryNav, { onDrawerToggle: onDrawerToggle, isDrawerOpen: true, displayMode: ChatbotDisplayMode.fullscreen, setIsDrawerOpen: jest.fn(), reverseButtonOrder: false, handleTextInputChange: handleSearch, conversations: groupedConversations, searchActionEnd: _jsx("div", { children: "Search action end test" }) }));
        expect(screen.getByText('Search action end test')).toBeVisible();
    });
    it('Overrides default search input and actions when searchToolbar is passed', () => {
        const handleSearch = jest.fn();
        const groupedConversations = {
            Today: [...initialConversations, { id: '2', text: 'Chatbot extension' }]
        };
        render(_jsx(ChatbotConversationHistoryNav, { onDrawerToggle: onDrawerToggle, isDrawerOpen: true, displayMode: ChatbotDisplayMode.fullscreen, setIsDrawerOpen: jest.fn(), reverseButtonOrder: false, conversations: groupedConversations, handleTextInputChange: handleSearch, searchActionStart: _jsx("div", { children: "Search action start test" }), searchActionEnd: _jsx("div", { children: "Search action end test" }), searchToolbar: _jsx("div", { children: "Custom toolbar" }) }));
        expect(screen.queryByPlaceholderText(/Search/i)).not.toBeInTheDocument();
        expect(screen.queryByText('Search action start test')).not.toBeInTheDocument();
        expect(screen.queryByText('Search action end test')).not.toBeInTheDocument();
        expect(screen.getByText('Custom toolbar')).toBeInTheDocument();
    });
    it('overrides nav title heading level when navTitleProps.headingLevel is passed', () => {
        render(_jsx(ChatbotConversationHistoryNav, { onDrawerToggle: onDrawerToggle, isDrawerOpen: true, displayMode: ChatbotDisplayMode.fullscreen, setIsDrawerOpen: jest.fn(), conversations: { Today: initialConversations }, navTitleProps: { headingLevel: 'h1' } }));
        expect(screen.queryByRole('heading', { name: /Chat history/i, level: 2 })).not.toBeInTheDocument();
        expect(screen.getByRole('heading', { name: /Chat history/i, level: 1 })).toBeInTheDocument();
    });
    it('overrides nav title icon when navTitleIcon is passed in', () => {
        render(_jsx(ChatbotConversationHistoryNav, { onDrawerToggle: onDrawerToggle, isDrawerOpen: true, displayMode: ChatbotDisplayMode.fullscreen, setIsDrawerOpen: jest.fn(), conversations: initialConversations, navTitleIcon: _jsx(BellIcon, { "data-testid": "bell" }) }));
        expect(screen.getByTestId('bell')).toBeInTheDocument();
    });
});
