"use strict";
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());
    });
};
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"));
require("@testing-library/jest-dom");
const react_2 = require("@testing-library/react");
const Chatbot_1 = require("../Chatbot/Chatbot");
const ChatbotConversationHistoryNav_1 = __importDefault(require("./ChatbotConversationHistoryNav"));
const react_core_1 = require("@patternfly/react-core");
const react_icons_1 = require("@patternfly/react-icons");
const ERROR = {
    bodyText: (react_1.default.createElement(react_1.default.Fragment, null,
        "To try again, check your connection and reload this page. If the issue persists,",
        ' ',
        react_1.default.createElement("a", { href: "" }, "contact the support team"),
        ".")),
    buttonText: 'Reload',
    buttonIcon: react_1.default.createElement(react_core_1.Spinner, { size: "sm" }),
    hasButton: true,
    titleText: 'Could not load chat history',
    status: react_core_1.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: react_icons_1.SearchIcon
};
const EMPTY_STATE = {
    bodyText: 'Access timely assistance by starting a conversation with an AI model.',
    titleText: 'Start a new chat',
    icon: react_icons_1.OutlinedCommentsIcon
};
const ERROR_WITHOUT_BUTTON = {
    bodyText: (react_1.default.createElement(react_1.default.Fragment, null,
        "To try again, check your connection and reload this page. If the issue persists,",
        ' ',
        react_1.default.createElement("a", { href: "" }, "contact the support team"),
        ".")),
    buttonText: 'Reload',
    buttonIcon: react_1.default.createElement(react_core_1.Spinner, { size: "sm" }),
    hasButton: false,
    titleText: 'Could not load chat history',
    status: react_core_1.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', () => {
        (0, react_2.render)(react_1.default.createElement(ChatbotConversationHistoryNav_1.default, { onDrawerToggle: onDrawerToggle, isDrawerOpen: true, displayMode: Chatbot_1.ChatbotDisplayMode.fullscreen, setIsDrawerOpen: jest.fn(), conversations: initialConversations }));
        expect(react_2.screen.queryByText('ChatBot documentation')).toBeInTheDocument();
    });
    it('should display the conversations for grouped conversations', () => {
        const groupedConversations = {
            Today: [...initialConversations, { id: '2', text: 'Chatbot extension' }]
        };
        (0, react_2.render)(react_1.default.createElement(ChatbotConversationHistoryNav_1.default, { onDrawerToggle: onDrawerToggle, isDrawerOpen: true, displayMode: Chatbot_1.ChatbotDisplayMode.fullscreen, setIsDrawerOpen: jest.fn(), conversations: groupedConversations }));
        expect(react_2.screen.queryByText('Chatbot extension')).toBeInTheDocument();
    });
    it('should apply the reversed class when reverseButtonOrder is true', () => {
        (0, react_2.render)(react_1.default.createElement(ChatbotConversationHistoryNav_1.default, { onDrawerToggle: onDrawerToggle, isDrawerOpen: true, displayMode: Chatbot_1.ChatbotDisplayMode.fullscreen, setIsDrawerOpen: jest.fn(), reverseButtonOrder: true, conversations: initialConversations }));
        expect(react_2.screen.getByTestId('chatbot-nav-drawer-actions')).toHaveClass('pf-v6-c-drawer__actions--reversed');
    });
    it('should not apply the reversed class when reverseButtonOrder is false', () => {
        (0, react_2.render)(react_1.default.createElement(ChatbotConversationHistoryNav_1.default, { onDrawerToggle: onDrawerToggle, isDrawerOpen: true, displayMode: Chatbot_1.ChatbotDisplayMode.fullscreen, setIsDrawerOpen: jest.fn(), reverseButtonOrder: false, conversations: initialConversations }));
        expect(react_2.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' }]
        };
        (0, react_2.render)(react_1.default.createElement(ChatbotConversationHistoryNav_1.default, { onDrawerToggle: onDrawerToggle, isDrawerOpen: true, displayMode: Chatbot_1.ChatbotDisplayMode.fullscreen, setIsDrawerOpen: jest.fn(), reverseButtonOrder: false, conversations: groupedConversations, handleTextInputChange: handleSearch }));
        const searchInput = react_2.screen.getByPlaceholderText(/Search/i);
        react_2.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* () {
        (0, react_2.render)(react_1.default.createElement(ChatbotConversationHistoryNav_1.default, { onDrawerToggle: onDrawerToggle, isDrawerOpen: true, displayMode: Chatbot_1.ChatbotDisplayMode.fullscreen, setIsDrawerOpen: jest.fn(), reverseButtonOrder: false, handleTextInputChange: jest.fn(), conversations: initialConversations }));
        react_2.fireEvent.keyDown(react_2.screen.getByPlaceholderText(/Search/i), {
            key: 'Escape',
            code: 'Escape',
            keyCode: 27,
            charCode: 27
        });
        (0, react_2.waitFor)(() => {
            expect(react_2.screen.queryByText('ChatBot documentation')).not.toBeInTheDocument();
        });
    }));
    it('should be resizable', () => {
        (0, react_2.render)(react_1.default.createElement(ChatbotConversationHistoryNav_1.default, { onDrawerToggle: onDrawerToggle, isDrawerOpen: true, displayMode: Chatbot_1.ChatbotDisplayMode.fullscreen, setIsDrawerOpen: jest.fn(), conversations: initialConversations, drawerPanelContentProps: { isResizable: true, minSize: '200px' } }));
        expect(react_2.screen.getByRole('dialog', { name: /Resize/i })).toBeTruthy();
        expect(react_2.screen.getByRole('separator', { name: /Resize/i })).toBeTruthy();
        expect(react_2.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 } = (0, react_2.render)(react_1.default.createElement(ChatbotConversationHistoryNav_1.default, { onDrawerToggle: onDrawerToggle, isDrawerOpen: true, displayMode: Chatbot_1.ChatbotDisplayMode.fullscreen, setIsDrawerOpen: jest.fn(), conversations: initialConversations, drawerContentProps: { className: 'test' } }));
        const element = container.querySelector('.test');
        expect(element).toBeInTheDocument();
    });
    it('should accept drawerContentBodyProps', () => {
        const { container } = (0, react_2.render)(react_1.default.createElement(ChatbotConversationHistoryNav_1.default, { onDrawerToggle: onDrawerToggle, isDrawerOpen: true, displayMode: Chatbot_1.ChatbotDisplayMode.fullscreen, setIsDrawerOpen: jest.fn(), conversations: initialConversations, drawerContentBodyProps: { className: 'test' } }));
        const element = container.querySelector('.test');
        expect(element).toBeInTheDocument();
    });
    it('should accept drawerHeadProps', () => {
        const { container } = (0, react_2.render)(react_1.default.createElement(ChatbotConversationHistoryNav_1.default, { onDrawerToggle: onDrawerToggle, isDrawerOpen: true, displayMode: Chatbot_1.ChatbotDisplayMode.fullscreen, setIsDrawerOpen: jest.fn(), conversations: initialConversations, drawerHeadProps: { className: 'test' } }));
        const element = container.querySelector('.test');
        expect(element).toBeInTheDocument();
    });
    it('should accept drawerActionsProps', () => {
        const { container } = (0, react_2.render)(react_1.default.createElement(ChatbotConversationHistoryNav_1.default, { onDrawerToggle: onDrawerToggle, isDrawerOpen: true, displayMode: Chatbot_1.ChatbotDisplayMode.fullscreen, setIsDrawerOpen: jest.fn(), conversations: initialConversations, drawerActionsProps: { className: 'test' } }));
        const element = container.querySelector('.test');
        expect(element).toBeInTheDocument();
    });
    it('should accept drawerCloseButtonProps', () => {
        const { container } = (0, react_2.render)(react_1.default.createElement(ChatbotConversationHistoryNav_1.default, { onDrawerToggle: onDrawerToggle, isDrawerOpen: true, displayMode: Chatbot_1.ChatbotDisplayMode.fullscreen, setIsDrawerOpen: jest.fn(), conversations: initialConversations, drawerCloseButtonProps: { className: 'test' } }));
        const element = container.querySelector('.test');
        expect(element).toBeInTheDocument();
    });
    it('should accept drawerPanelBodyProps', () => {
        const { container } = (0, react_2.render)(react_1.default.createElement(ChatbotConversationHistoryNav_1.default, { onDrawerToggle: onDrawerToggle, isDrawerOpen: true, displayMode: Chatbot_1.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', () => {
        (0, react_2.render)(react_1.default.createElement(ChatbotConversationHistoryNav_1.default, { onDrawerToggle: onDrawerToggle, isDrawerOpen: true, displayMode: Chatbot_1.ChatbotDisplayMode.fullscreen, setIsDrawerOpen: jest.fn(), reverseButtonOrder: false, handleTextInputChange: jest.fn(), conversations: initialConversations, isLoading: true }));
        expect(react_2.screen.getByRole('dialog', { name: /Loading chatbot conversation history/i })).toBeTruthy();
        expect(react_2.screen.getByRole('button', { name: /Close drawer panel/i })).toBeTruthy();
    });
    it('should pass alternative aria label to loading state', () => {
        (0, react_2.render)(react_1.default.createElement(ChatbotConversationHistoryNav_1.default, { onDrawerToggle: onDrawerToggle, isDrawerOpen: true, displayMode: Chatbot_1.ChatbotDisplayMode.fullscreen, setIsDrawerOpen: jest.fn(), reverseButtonOrder: false, handleTextInputChange: jest.fn(), conversations: initialConversations, isLoading: true, loadingState: { screenreaderText: 'I am a test' } }));
        expect(react_2.screen.getByRole('dialog', { name: /I am a test/i })).toBeTruthy();
    });
    it('should accept errorState', () => {
        (0, react_2.render)(react_1.default.createElement(ChatbotConversationHistoryNav_1.default, { onDrawerToggle: onDrawerToggle, isDrawerOpen: true, displayMode: Chatbot_1.ChatbotDisplayMode.fullscreen, setIsDrawerOpen: jest.fn(), reverseButtonOrder: false, handleTextInputChange: jest.fn(), conversations: initialConversations, errorState: ERROR }));
        expect(react_2.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(react_2.screen.getByRole('button', { name: /Close drawer panel/i })).toBeTruthy();
        expect(react_2.screen.getByRole('button', { name: /Loading... Reload/i })).toBeTruthy();
        expect(react_2.screen.getByRole('textbox', { name: /Filter menu items/i })).toBeTruthy();
        expect(react_2.screen.getByRole('heading', { name: /Could not load chat history/i })).toBeTruthy();
    });
    it('should accept errorState without button', () => {
        (0, react_2.render)(react_1.default.createElement(ChatbotConversationHistoryNav_1.default, { onDrawerToggle: onDrawerToggle, isDrawerOpen: true, displayMode: Chatbot_1.ChatbotDisplayMode.fullscreen, setIsDrawerOpen: jest.fn(), reverseButtonOrder: false, handleTextInputChange: jest.fn(), conversations: initialConversations, errorState: ERROR_WITHOUT_BUTTON }));
        expect(react_2.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(react_2.screen.getByRole('button', { name: /Close drawer panel/i })).toBeTruthy();
        expect(react_2.screen.queryByRole('button', { name: /Loading... Reload/i })).toBeFalsy();
        expect(react_2.screen.getByRole('textbox', { name: /Filter menu items/i })).toBeTruthy();
        expect(react_2.screen.getByRole('heading', { name: /Could not load chat history/i })).toBeTruthy();
    });
    it('should show loading state over error state if both are supplied', () => {
        (0, react_2.render)(react_1.default.createElement(ChatbotConversationHistoryNav_1.default, { onDrawerToggle: onDrawerToggle, isDrawerOpen: true, displayMode: Chatbot_1.ChatbotDisplayMode.fullscreen, setIsDrawerOpen: jest.fn(), reverseButtonOrder: false, handleTextInputChange: jest.fn(), conversations: initialConversations, isLoading: true, errorState: ERROR }));
        expect(react_2.screen.getByRole('dialog', { name: /Loading/i })).toBeTruthy();
    });
    it('should accept emptyState', () => {
        (0, react_2.render)(react_1.default.createElement(ChatbotConversationHistoryNav_1.default, { onDrawerToggle: onDrawerToggle, isDrawerOpen: true, displayMode: Chatbot_1.ChatbotDisplayMode.fullscreen, setIsDrawerOpen: jest.fn(), reverseButtonOrder: false, handleTextInputChange: jest.fn(), conversations: initialConversations, emptyState: EMPTY_STATE }));
        expect(react_2.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', () => {
        (0, react_2.render)(react_1.default.createElement(ChatbotConversationHistoryNav_1.default, { onDrawerToggle: onDrawerToggle, isDrawerOpen: true, displayMode: Chatbot_1.ChatbotDisplayMode.fullscreen, setIsDrawerOpen: jest.fn(), reverseButtonOrder: false, handleTextInputChange: jest.fn(), conversations: initialConversations, noResultsState: NO_RESULTS }));
        expect(react_2.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', () => {
        (0, react_2.render)(react_1.default.createElement(ChatbotConversationHistoryNav_1.default, { onDrawerToggle: onDrawerToggle, isDrawerOpen: true, displayMode: Chatbot_1.ChatbotDisplayMode.fullscreen, setIsDrawerOpen: jest.fn(), reverseButtonOrder: false, handleTextInputChange: jest.fn(), conversations: initialConversations, noResultsState: NO_RESULTS, isCompact: true, "data-testid": "drawer" }));
        expect(react_2.screen.getByTestId('drawer')).toHaveClass('pf-m-compact');
    });
});
