"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("@testing-library/react");
require("@testing-library/jest-dom");
const ImagePreview_1 = __importDefault(require("./ImagePreview"));
const Chatbot_1 = require("../Chatbot");
const mockImages = [
    {
        fileName: 'image1.jpg',
        fileSize: '2.5 MB',
        image: (0, jsx_runtime_1.jsx)("img", { src: "", alt: "Test image 1" })
    },
    {
        fileName: 'image2.png',
        fileSize: '1.8 MB',
        image: (0, jsx_runtime_1.jsx)("img", { src: "", alt: "Test image 2" })
    },
    {
        fileName: 'image3.gif',
        image: (0, jsx_runtime_1.jsx)("img", { src: "", alt: "Test image 3" })
    }
];
const defaultProps = {
    isModalOpen: true,
    handleModalToggle: jest.fn(),
    images: mockImages
};
describe('ImagePreview', () => {
    beforeEach(() => {
        jest.clearAllMocks();
    });
    it('renders modal when isModalOpen is true', () => {
        (0, react_1.render)((0, jsx_runtime_1.jsx)(ImagePreview_1.default, Object.assign({}, defaultProps)));
        expect(react_1.screen.getByRole('dialog')).toBeInTheDocument();
    });
    it('does not render modal when isModalOpen is false', () => {
        (0, react_1.render)((0, jsx_runtime_1.jsx)(ImagePreview_1.default, Object.assign({}, defaultProps, { isModalOpen: false })));
        expect(react_1.screen.queryByRole('dialog')).not.toBeInTheDocument();
    });
    it('displays custom title when provided', () => {
        const customTitle = 'Custom image preview';
        (0, react_1.render)((0, jsx_runtime_1.jsx)(ImagePreview_1.default, Object.assign({}, defaultProps, { title: customTitle })));
        expect(react_1.screen.getByRole('heading', { name: customTitle })).toBeInTheDocument();
    });
    it('displays default title when no title provided', () => {
        (0, react_1.render)((0, jsx_runtime_1.jsx)(ImagePreview_1.default, Object.assign({}, defaultProps)));
        expect(react_1.screen.getByRole('heading', { name: /Preview images/i })).toBeInTheDocument();
    });
    it('calls handleModalToggle when modal is closed', () => {
        const mockHandleToggle = jest.fn();
        (0, react_1.render)((0, jsx_runtime_1.jsx)(ImagePreview_1.default, Object.assign({}, defaultProps, { handleModalToggle: mockHandleToggle })));
        const closeButton = react_1.screen.getByRole('button', { name: /close/i });
        react_1.fireEvent.click(closeButton);
        expect(mockHandleToggle).toHaveBeenCalledTimes(1);
    });
    it('displays first image by default', () => {
        (0, react_1.render)((0, jsx_runtime_1.jsx)(ImagePreview_1.default, Object.assign({}, defaultProps)));
        expect(react_1.screen.getByText('image1.jpg')).toBeInTheDocument();
        expect(react_1.screen.getByText('2.5 MB')).toBeInTheDocument();
        expect(react_1.screen.getByAltText('Test image 1')).toBeInTheDocument();
    });
    it('displays page counter correctly', () => {
        (0, react_1.render)((0, jsx_runtime_1.jsx)(ImagePreview_1.default, Object.assign({}, defaultProps)));
        expect(react_1.screen.getByText('1/3')).toBeInTheDocument();
    });
    it('navigates to next image when next button is clicked', () => {
        const mockOnNextClick = jest.fn();
        (0, react_1.render)((0, jsx_runtime_1.jsx)(ImagePreview_1.default, Object.assign({}, defaultProps, { onNextClick: mockOnNextClick })));
        const nextButton = react_1.screen.getByRole('button', { name: /Go to next image/i });
        react_1.fireEvent.click(nextButton);
        expect(mockOnNextClick).toHaveBeenCalled();
        expect(react_1.screen.getByText('2/3')).toBeInTheDocument();
        expect(react_1.screen.getByText('image2.png')).toBeInTheDocument();
    });
    it('navigates to previous image when previous button is clicked', () => {
        const mockOnPreviousClick = jest.fn();
        (0, react_1.render)((0, jsx_runtime_1.jsx)(ImagePreview_1.default, Object.assign({}, defaultProps, { onPreviousClick: mockOnPreviousClick })));
        // First go to page 2
        const nextButton = react_1.screen.getByRole('button', { name: /Go to next image/i });
        react_1.fireEvent.click(nextButton);
        // Then go back to page 1
        const previousButton = react_1.screen.getByRole('button', { name: /Go to previous image/i });
        react_1.fireEvent.click(previousButton);
        expect(mockOnPreviousClick).toHaveBeenCalled();
        expect(react_1.screen.getByText('1/3')).toBeInTheDocument();
    });
    it('calls onSetPage when page changes', () => {
        const mockOnSetPage = jest.fn();
        (0, react_1.render)((0, jsx_runtime_1.jsx)(ImagePreview_1.default, Object.assign({}, defaultProps, { onSetPage: mockOnSetPage })));
        const nextButton = react_1.screen.getByRole('button', { name: /Go to next image/i });
        react_1.fireEvent.click(nextButton);
        expect(mockOnSetPage).toHaveBeenCalledWith(expect.any(Object), 2);
    });
    it('disables previous button on first page', () => {
        (0, react_1.render)((0, jsx_runtime_1.jsx)(ImagePreview_1.default, Object.assign({}, defaultProps)));
        const previousButton = react_1.screen.getByRole('button', { name: /Go to previous image/i });
        expect(previousButton).toBeDisabled();
    });
    it('disables next button on last page', () => {
        (0, react_1.render)((0, jsx_runtime_1.jsx)(ImagePreview_1.default, Object.assign({}, defaultProps)));
        // Navigate to last page
        const nextButton = react_1.screen.getByRole('button', { name: /Go to next image/i });
        react_1.fireEvent.click(nextButton); // page 2
        react_1.fireEvent.click(nextButton); // page 3
        expect(nextButton).toBeDisabled();
    });
    it('disables both navigation buttons when isDisabled is true', () => {
        (0, react_1.render)((0, jsx_runtime_1.jsx)(ImagePreview_1.default, Object.assign({}, defaultProps, { isDisabled: true })));
        const previousButton = react_1.screen.getByRole('button', { name: /Go to previous image/i });
        const nextButton = react_1.screen.getByRole('button', { name: /Go to next image/i });
        expect(previousButton).toBeDisabled();
        expect(nextButton).toBeDisabled();
    });
    it('uses custom aria labels for pagination', () => {
        const customLabels = {
            paginationAriaLabel: 'Custom pagination',
            toPreviousPageAriaLabel: 'Go to previous image',
            toNextPageAriaLabel: 'Go to next image'
        };
        (0, react_1.render)((0, jsx_runtime_1.jsx)(ImagePreview_1.default, Object.assign({}, defaultProps, customLabels)));
        expect(react_1.screen.getByRole('navigation', { name: 'Custom pagination' })).toBeInTheDocument();
        expect(react_1.screen.getByRole('button', { name: 'Go to previous image' })).toBeInTheDocument();
        expect(react_1.screen.getByRole('button', { name: 'Go to next image' })).toBeInTheDocument();
    });
    it('renders with compact mode when isCompact is true', () => {
        (0, react_1.render)((0, jsx_runtime_1.jsx)(ImagePreview_1.default, Object.assign({}, defaultProps, { isCompact: true })));
        const modal = react_1.screen.getByRole('dialog');
        expect(modal).toHaveClass('pf-m-compact');
    });
    it('applies custom className when provided', () => {
        const customClassName = 'custom-image-preview';
        (0, react_1.render)((0, jsx_runtime_1.jsx)(ImagePreview_1.default, Object.assign({}, defaultProps, { className: customClassName })));
        const modal = react_1.screen.getByRole('dialog');
        expect(modal).toHaveClass(customClassName);
    });
    it('applies display mode class correctly', () => {
        (0, react_1.render)((0, jsx_runtime_1.jsx)(ImagePreview_1.default, Object.assign({}, defaultProps, { displayMode: Chatbot_1.ChatbotDisplayMode.embedded })));
        const modal = react_1.screen.getByRole('dialog');
        expect(modal).toHaveClass('pf-chatbot__image-preview-modal--embedded');
    });
    it('passes additional props to ChatbotModal', () => {
        const modalClass = 'custom-modal-class';
        const additionalProps = {
            'data-testid': 'modal',
            className: modalClass
        };
        (0, react_1.render)((0, jsx_runtime_1.jsx)(ImagePreview_1.default, Object.assign({}, defaultProps, additionalProps)));
        const modal = react_1.screen.getByTestId('modal');
        expect(modal).toBeInTheDocument();
        expect(modal).toBeInTheDocument();
        expect(modal).toHaveClass(modalClass);
    });
    it('passes modalHeaderProps correctly', () => {
        const headerClass = 'custom-modal-header-class';
        const headerProps = {
            'data-testid': 'header',
            className: headerClass
        };
        (0, react_1.render)((0, jsx_runtime_1.jsx)(ImagePreview_1.default, Object.assign({}, defaultProps, { modalHeaderProps: headerProps })));
        expect(react_1.screen.getByTestId('header')).toBeInTheDocument();
        expect(react_1.screen.getByTestId('header')).toHaveClass(headerClass);
    });
    it('passes modalBodyProps correctly', () => {
        const bodyClass = 'custom-modal-body-class';
        const bodyProps = {
            'data-testid': 'body',
            className: bodyClass
        };
        (0, react_1.render)((0, jsx_runtime_1.jsx)(ImagePreview_1.default, Object.assign({}, defaultProps, { modalBodyProps: bodyProps })));
        expect(react_1.screen.getByTestId('body')).toBeInTheDocument();
        expect(react_1.screen.getByTestId('body')).toHaveClass(bodyClass);
    });
    it('handles single image without pagination', () => {
        const singleImage = [mockImages[0]];
        (0, react_1.render)((0, jsx_runtime_1.jsx)(ImagePreview_1.default, Object.assign({}, defaultProps, { images: singleImage })));
        expect(react_1.screen.queryByText('1/1')).not.toBeInTheDocument();
        expect(react_1.screen.queryByRole('button', { name: /Go to previous image/i })).not.toBeInTheDocument();
        expect(react_1.screen.queryByRole('button', { name: /Go to next image/i })).not.toBeInTheDocument();
    });
    it('calls onCloseFileDetailsLabel when file details close button is clicked', () => {
        const mockOnClose = jest.fn();
        (0, react_1.render)((0, jsx_runtime_1.jsx)(ImagePreview_1.default, Object.assign({}, defaultProps, { onCloseFileDetailsLabel: mockOnClose })));
        const closeButton = react_1.screen.getByRole('button', { name: /Close image1.jpg/i });
        react_1.fireEvent.click(closeButton);
        expect(mockOnClose).toHaveBeenCalled();
    });
    it('passes fileDetailsLabelProps correctly to FileDetailsLabel', () => {
        const customFileDetailsProps = {
            'data-testid': 'custom-file-details'
        };
        (0, react_1.render)((0, jsx_runtime_1.jsx)(ImagePreview_1.default, Object.assign({}, defaultProps, { fileDetailsLabelProps: customFileDetailsProps })));
        expect(react_1.screen.getByTestId('custom-file-details')).toBeInTheDocument();
    });
    it('displays file details for current page when navigating', () => {
        (0, react_1.render)((0, jsx_runtime_1.jsx)(ImagePreview_1.default, Object.assign({}, defaultProps)));
        // Initially shows first image details
        expect(react_1.screen.getByText('image1.jpg')).toBeInTheDocument();
        expect(react_1.screen.getByText('2.5 MB')).toBeInTheDocument();
        // Navigate to second page
        const nextButton = react_1.screen.getByRole('button', { name: /Go to next image/i });
        react_1.fireEvent.click(nextButton);
        // Should now show second image details
        expect(react_1.screen.getByText('image2.png')).toBeInTheDocument();
        expect(react_1.screen.getByText('1.8 MB')).toBeInTheDocument();
        // Navigate to third page
        react_1.fireEvent.click(nextButton);
        // Should now show third image details (no file size)
        expect(react_1.screen.getByText('image3.gif')).toBeInTheDocument();
        expect(react_1.screen.queryByText(/MB/)).not.toBeInTheDocument();
    });
    it('sets hasTruncation to false on FileDetailsLabel', () => {
        const longFileName = 'very-long-filename-that-would-normally-be-truncated-in-other-contexts.jpg';
        const imageWithLongName = {
            fileName: longFileName,
            fileSize: '1.0 MB',
            image: (0, jsx_runtime_1.jsx)("img", { src: "", alt: "Test image with long name" })
        };
        (0, react_1.render)((0, jsx_runtime_1.jsx)(ImagePreview_1.default, Object.assign({}, defaultProps, { images: [imageWithLongName] })));
        expect(react_1.screen.getByText(longFileName)).toBeInTheDocument();
    });
});
