"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 jsx_runtime_1 = require("react/jsx-runtime");
require("@testing-library/jest-dom");
const react_1 = require("@testing-library/react");
const user_event_1 = __importDefault(require("@testing-library/user-event"));
const CodeBlockMessage_1 = __importDefault(require("./CodeBlockMessage"));
// Mock clipboard API
Object.assign(navigator, {
    clipboard: {
        writeText: jest.fn()
    }
});
describe('CodeBlockMessage', () => {
    beforeEach(() => {
        jest.clearAllMocks();
    });
    it('should render inline code for single-line content', () => {
        (0, react_1.render)((0, jsx_runtime_1.jsx)(CodeBlockMessage_1.default, { className: "language-javascript", children: "const x = 5;" }));
        const code = react_1.screen.getByText('const x = 5;');
        expect(code.tagName).toBe('CODE');
        expect(code).toHaveClass('pf-chatbot__message-inline-code');
    });
    it('should render code block for multi-line content', () => {
        const multilineCode = 'const x = 5;\nconst y = 10;';
        const { container } = (0, react_1.render)((0, jsx_runtime_1.jsx)(CodeBlockMessage_1.default, { className: "language-javascript", children: multilineCode }));
        const codeElement = container.querySelector('code');
        expect(codeElement === null || codeElement === void 0 ? void 0 : codeElement.textContent).toBe(multilineCode);
    });
    it('should display language label', () => {
        const code = 'const x = 5;\nconst y = 10;';
        (0, react_1.render)((0, jsx_runtime_1.jsx)(CodeBlockMessage_1.default, { className: "language-javascript", children: code }));
        expect(react_1.screen.getByText('javascript')).toBeInTheDocument();
    });
    it('should render copy button', () => {
        const code = 'const x = 5;\nconst y = 10;';
        (0, react_1.render)((0, jsx_runtime_1.jsx)(CodeBlockMessage_1.default, { children: code }));
        expect(react_1.screen.getByRole('button', { name: 'Copy code' })).toBeInTheDocument();
    });
    it('should copy plain string content to clipboard', () => __awaiter(void 0, void 0, void 0, function* () {
        const code = 'const x = 5;\nconst y = 10;';
        (0, react_1.render)((0, jsx_runtime_1.jsx)(CodeBlockMessage_1.default, { children: code }));
        const copyButton = react_1.screen.getByRole('button', { name: 'Copy code' });
        yield user_event_1.default.click(copyButton);
        expect(navigator.clipboard.writeText).toHaveBeenCalledWith(code);
    }));
    it('should extract text content from React elements when copying', () => __awaiter(void 0, void 0, void 0, function* () {
        // Simulate what happens with syntax highlighting - children become React elements
        const { container } = (0, react_1.render)((0, jsx_runtime_1.jsxs)(CodeBlockMessage_1.default, { className: "language-javascript", children: [(0, jsx_runtime_1.jsx)("span", { className: "hljs-keyword", children: "const" }), " x = 5;", '\n', (0, jsx_runtime_1.jsx)("span", { className: "hljs-keyword", children: "const" }), " y = 10;"] }));
        const copyButton = react_1.screen.getByRole('button', { name: 'Copy code' });
        yield user_event_1.default.click(copyButton);
        // Should extract actual text content from DOM, not "[object Object]"
        const codeElement = container.querySelector('code');
        const expectedText = (codeElement === null || codeElement === void 0 ? void 0 : codeElement.textContent) || '';
        expect(navigator.clipboard.writeText).toHaveBeenCalledWith(expectedText);
        expect(expectedText).not.toContain('[object Object]');
    }));
    it('should show check icon after copying', () => __awaiter(void 0, void 0, void 0, function* () {
        const code = 'const x = 5;\nconst y = 10;';
        (0, react_1.render)((0, jsx_runtime_1.jsx)(CodeBlockMessage_1.default, { children: code }));
        const copyButton = react_1.screen.getByRole('button', { name: 'Copy code' });
        yield user_event_1.default.click(copyButton);
        // Check icon should be visible (we can verify by checking if CopyIcon is not present)
        const svgElement = copyButton.querySelector('svg');
        expect(svgElement).toBeInTheDocument();
    }));
    it('should render expandable section when isExpandable is true', () => {
        const code = 'const x = 5;\nconst y = 10;';
        (0, react_1.render)((0, jsx_runtime_1.jsx)(CodeBlockMessage_1.default, { isExpandable: true, children: code }));
        expect(react_1.screen.getByRole('button', { name: 'Show more' })).toBeInTheDocument();
    });
    it('should toggle expandable section', () => __awaiter(void 0, void 0, void 0, function* () {
        const code = 'const x = 5;\nconst y = 10;';
        (0, react_1.render)((0, jsx_runtime_1.jsx)(CodeBlockMessage_1.default, { isExpandable: true, children: code }));
        const toggleButton = react_1.screen.getByRole('button', { name: 'Show more' });
        yield user_event_1.default.click(toggleButton);
        expect(react_1.screen.getByRole('button', { name: 'Show less' })).toBeInTheDocument();
    }));
    it('should use custom expanded/collapsed text', () => {
        const code = 'const x = 5;\nconst y = 10;';
        (0, react_1.render)((0, jsx_runtime_1.jsx)(CodeBlockMessage_1.default, { isExpandable: true, expandedText: "Hide", collapsedText: "Reveal", children: code }));
        expect(react_1.screen.getByRole('button', { name: 'Reveal' })).toBeInTheDocument();
    });
    it('should pass through expandableSectionProps', () => {
        const code = 'const x = 5;\nconst y = 10;';
        const { container } = (0, react_1.render)((0, jsx_runtime_1.jsx)(CodeBlockMessage_1.default, { isExpandable: true, expandableSectionProps: { className: 'custom-expandable-class' }, children: code }));
        const expandableSection = container.querySelector('.pf-v6-c-expandable-section.custom-expandable-class');
        expect(expandableSection).toBeInTheDocument();
    });
    it('should render custom actions', () => {
        const code = 'const x = 5;\nconst y = 10;';
        const customAction = (0, jsx_runtime_1.jsx)("button", { "aria-label": "Custom action", children: "Custom" });
        (0, react_1.render)((0, jsx_runtime_1.jsx)(CodeBlockMessage_1.default, { customActions: customAction, children: code }));
        expect(react_1.screen.getByRole('button', { name: 'Custom action' })).toBeInTheDocument();
    });
    it('should apply isPrimary class to inline code', () => {
        (0, react_1.render)((0, jsx_runtime_1.jsx)(CodeBlockMessage_1.default, { isPrimary: true, children: "const x = 5;" }));
        const code = react_1.screen.getByText('const x = 5;');
        expect(code).toHaveClass('pf-m-primary');
    });
    it('should apply shouldRetainStyles class to code block', () => {
        const code = 'const x = 5;\nconst y = 10;';
        const { container } = (0, react_1.render)((0, jsx_runtime_1.jsx)(CodeBlockMessage_1.default, { shouldRetainStyles: true, children: code }));
        const codeBlockDiv = container.querySelector('.pf-chatbot__message-code-block');
        expect(codeBlockDiv).toHaveClass('pf-m-markdown');
    });
    it('should use custom aria-label for copy button', () => {
        const code = 'const x = 5;\nconst y = 10;';
        (0, react_1.render)((0, jsx_runtime_1.jsx)(CodeBlockMessage_1.default, { "aria-label": "Copy this code", children: code }));
        expect(react_1.screen.getByRole('button', { name: 'Copy this code' })).toBeInTheDocument();
    });
    it('should prioritize data-expanded-text over expandedText prop', () => {
        const code = 'const x = 5;\nconst y = 10;';
        const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(jest.fn());
        (0, react_1.render)((0, jsx_runtime_1.jsx)(CodeBlockMessage_1.default, { isExpandable: true, expandedText: "Custom Expanded", "data-expanded-text": "Data Expanded", children: code }));
        expect(consoleErrorSpy).toHaveBeenCalledWith('Message:', expect.stringContaining('data-expanded-text or data-collapsed-text will override'));
        consoleErrorSpy.mockRestore();
    });
});
