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 React from 'react';
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';
const ERROR = {
    bodyText: (React.createElement(React.Fragment, null,
        "To try again, check your connection and reload this page. If the issue persists,",
        ' ',
        React.createElement("a", { href: "" }, "contact the support team"),
        ".")),
    buttonText: 'Reload',
    buttonIcon: React.createElement(Spinner, { size: "sm" }),
    hasButton: true,
    titleText: 'Could not load chat history',
    status: EmptyStateStatus.danger,
    onClick: () => alert('Clicked Reload')
};
const ERROR_WITHOUT_BUTTON = {
    bodyText: (React.createElement(React.Fragment, null,
        "To try again, check your connection and reload this page. If the issue persists,",
        ' ',
        React.createElement("a", { href: "" }, "contact the support team"),
        ".")),
    buttonText: 'Reload',
    buttonIcon: React.createElement(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(React.createElement(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(React.createElement(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(React.createElement(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 not apply the reversed class when reverseButtonOrder is false', () => {
        render(React.createElement(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(React.createElement(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(React.createElement(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(React.createElement(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(React.createElement(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(React.createElement(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(React.createElement(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(React.createElement(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(React.createElement(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(React.createElement(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(React.createElement(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(React.createElement(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(React.createElement(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: /Filter menu items/i })).toBeTruthy();
        expect(screen.getByRole('heading', { name: /Could not load chat history/i })).toBeTruthy();
    });
    it('should accept errorState without button', () => {
        render(React.createElement(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: /Filter menu items/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(React.createElement(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();
    });
});
