"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");
const react_1 = require("@testing-library/react");
require("@testing-library/jest-dom");
const MarkdownContent_1 = __importDefault(require("./MarkdownContent"));
const rehype_external_links_1 = __importDefault(require("../__mocks__/rehype-external-links"));
const BOLD_TEXT = '**Bold text**';
const ITALIC_TEXT = '*Italic text*';
const INLINE_CODE = 'Here is inline code: `const x = 5`';
const CODE_BLOCK = `\`\`\`javascript
function hello() {
  console.log('Hello, world!');
}
\`\`\``;
const HEADING = '# Heading 1';
const LINK = '[PatternFly](https://www.patternfly.org/)';
const UNORDERED_LIST = `
* Item 1
* Item 2
* Item 3
`;
const ORDERED_LIST = `
1. First item
2. Second item
3. Third item
`;
const TABLE = `
| Column 1 | Column 2 |
|----------|----------|
| Cell 1   | Cell 2   |
| Cell 3   | Cell 4   |
`;
const BLOCKQUOTE = '> This is a blockquote';
const IMAGE = '![Alt text](https://example.com/image.png)';
describe('MarkdownContent', () => {
    beforeEach(() => {
        jest.clearAllMocks();
    });
    it('should render bold text correctly', () => {
        const { container } = (0, react_1.render)((0, jsx_runtime_1.jsx)(MarkdownContent_1.default, { content: BOLD_TEXT }));
        expect(container.querySelector('strong')).toBeTruthy();
        expect(react_1.screen.getByText('Bold text')).toBeTruthy();
    });
    it('should render italic text correctly', () => {
        const { container } = (0, react_1.render)((0, jsx_runtime_1.jsx)(MarkdownContent_1.default, { content: ITALIC_TEXT }));
        expect(container.querySelector('em')).toBeTruthy();
        expect(react_1.screen.getByText('Italic text')).toBeTruthy();
    });
    it('should render inline code correctly', () => {
        (0, react_1.render)((0, jsx_runtime_1.jsx)(MarkdownContent_1.default, { content: INLINE_CODE }));
        expect(react_1.screen.getByText(/const x = 5/)).toBeTruthy();
    });
    it('should render code blocks correctly', () => {
        (0, react_1.render)((0, jsx_runtime_1.jsx)(MarkdownContent_1.default, { content: CODE_BLOCK }));
        expect(react_1.screen.getByText(/function hello/)).toBeVisible();
        expect(react_1.screen.getByText(/console.log/)).toBeVisible();
        expect(react_1.screen.getByRole('button', { name: 'Copy code' })).toBeVisible();
    });
    it('should render headings correctly', () => {
        (0, react_1.render)((0, jsx_runtime_1.jsx)(MarkdownContent_1.default, { content: HEADING }));
        expect(react_1.screen.getByRole('heading', { name: /Heading 1/i })).toBeTruthy();
    });
    it('should render links correctly', () => {
        (0, react_1.render)((0, jsx_runtime_1.jsx)(MarkdownContent_1.default, { content: LINK }));
        expect(react_1.screen.getByRole('link', { name: /PatternFly/i })).toBeTruthy();
    });
    it('should render unordered lists correctly', () => {
        (0, react_1.render)((0, jsx_runtime_1.jsx)(MarkdownContent_1.default, { content: UNORDERED_LIST }));
        expect(react_1.screen.getByText('Item 1')).toBeTruthy();
        expect(react_1.screen.getByText('Item 2')).toBeTruthy();
        expect(react_1.screen.getByText('Item 3')).toBeTruthy();
        expect(react_1.screen.getAllByRole('listitem')).toHaveLength(3);
    });
    it('should render ordered lists correctly', () => {
        (0, react_1.render)((0, jsx_runtime_1.jsx)(MarkdownContent_1.default, { content: ORDERED_LIST }));
        expect(react_1.screen.getByText('First item')).toBeTruthy();
        expect(react_1.screen.getByText('Second item')).toBeTruthy();
        expect(react_1.screen.getByText('Third item')).toBeTruthy();
        expect(react_1.screen.getAllByRole('listitem')).toHaveLength(3);
    });
    it('should render tables correctly', () => {
        (0, react_1.render)((0, jsx_runtime_1.jsx)(MarkdownContent_1.default, { content: TABLE, tableProps: { 'aria-label': 'Test table' } }));
        expect(react_1.screen.getByRole('grid', { name: /Test table/i })).toBeTruthy();
        expect(react_1.screen.getByRole('columnheader', { name: /Column 1/i })).toBeTruthy();
        expect(react_1.screen.getByRole('columnheader', { name: /Column 2/i })).toBeTruthy();
        expect(react_1.screen.getByRole('cell', { name: /Cell 1/i })).toBeTruthy();
        expect(react_1.screen.getByRole('cell', { name: /Cell 2/i })).toBeTruthy();
    });
    it('should render blockquotes correctly', () => {
        var _a;
        (0, react_1.render)((0, jsx_runtime_1.jsx)(MarkdownContent_1.default, { content: BLOCKQUOTE }));
        const quote = react_1.screen.getByText(/This is a blockquote/);
        expect(quote).toBeVisible();
        expect((_a = quote.closest('.pf-v6-c-content--blockquote')) === null || _a === void 0 ? void 0 : _a.tagName).toBe('BLOCKQUOTE');
    });
    it('should render images when hasNoImages is false', () => {
        (0, react_1.render)((0, jsx_runtime_1.jsx)(MarkdownContent_1.default, { content: IMAGE, hasNoImages: false }));
        expect(react_1.screen.getByRole('img', { name: /Alt text/i })).toBeTruthy();
    });
    it('should not render images when hasNoImages is true', () => {
        (0, react_1.render)((0, jsx_runtime_1.jsx)(MarkdownContent_1.default, { content: IMAGE, hasNoImages: true }));
        expect(react_1.screen.queryByRole('img', { name: /Alt text/i })).toBeFalsy();
    });
    it('should disable markdown rendering when isMarkdownDisabled is true', () => {
        (0, react_1.render)((0, jsx_runtime_1.jsx)(MarkdownContent_1.default, { content: BOLD_TEXT, isMarkdownDisabled: true }));
        expect(react_1.screen.getByText('**Bold text**')).toBeTruthy();
    });
    it('should render text component when isMarkdownDisabled is true and textComponent is provided', () => {
        const textComponent = (0, jsx_runtime_1.jsx)("div", { "data-testid": "custom-text", children: "Custom text component" });
        (0, react_1.render)((0, jsx_runtime_1.jsx)(MarkdownContent_1.default, { content: BOLD_TEXT, isMarkdownDisabled: true, textComponent: textComponent }));
        expect(react_1.screen.getByTestId('custom-text')).toBeTruthy();
        expect(react_1.screen.getByText('Custom text component')).toBeTruthy();
    });
    it('should apply isPrimary prop to elements', () => {
        const { container } = (0, react_1.render)((0, jsx_runtime_1.jsx)(MarkdownContent_1.default, { content: INLINE_CODE, isPrimary: true }));
        expect(container.querySelector('.pf-m-primary')).toBeTruthy();
    });
    it('should apply shouldRetainStyles prop to elements', () => {
        const { container } = (0, react_1.render)((0, jsx_runtime_1.jsx)(MarkdownContent_1.default, { content: BOLD_TEXT, shouldRetainStyles: true }));
        expect(container.querySelector('.pf-m-markdown')).toBeTruthy();
    });
    it('should pass codeBlockProps to code blocks', () => {
        (0, react_1.render)((0, jsx_runtime_1.jsx)(MarkdownContent_1.default, { content: CODE_BLOCK, codeBlockProps: { 'aria-label': 'Custom code block' } }));
        expect(react_1.screen.getByRole('button', { name: /Custom code block/i })).toBeTruthy();
    });
    it('should pass tableProps to tables', () => {
        (0, react_1.render)((0, jsx_runtime_1.jsx)(MarkdownContent_1.default, { content: TABLE, tableProps: { 'aria-label': 'Custom table label' } }));
        expect(react_1.screen.getByRole('grid', { name: /Custom table label/i })).toBeTruthy();
    });
    it('should open links in new tab when openLinkInNewTab is true', () => {
        (0, react_1.render)((0, jsx_runtime_1.jsx)(MarkdownContent_1.default, { content: LINK, openLinkInNewTab: true }));
        expect(rehype_external_links_1.default).toHaveBeenCalledTimes(1);
    });
    it('should not open links in new tab when openLinkInNewTab is false', () => {
        (0, react_1.render)((0, jsx_runtime_1.jsx)(MarkdownContent_1.default, { content: LINK, openLinkInNewTab: false }));
        expect(rehype_external_links_1.default).not.toHaveBeenCalled();
    });
    it('should pass linkProps to links', () => __awaiter(void 0, void 0, void 0, function* () {
        const onClick = jest.fn();
        (0, react_1.render)((0, jsx_runtime_1.jsx)(MarkdownContent_1.default, { content: LINK, linkProps: { onClick } }));
        const link = react_1.screen.getByRole('link', { name: /PatternFly/i });
        link.click();
        expect(onClick).toHaveBeenCalledTimes(1);
    }));
    it('should handle reactMarkdownProps.disallowedElements', () => {
        (0, react_1.render)((0, jsx_runtime_1.jsx)(MarkdownContent_1.default, { content: CODE_BLOCK, reactMarkdownProps: { disallowedElements: ['code'] } }));
        // Code block should not render when disallowed
        expect(react_1.screen.queryByRole('button', { name: /Copy code/i })).toBeFalsy();
    });
    it('should render plain text when no markdown is present', () => {
        (0, react_1.render)((0, jsx_runtime_1.jsx)(MarkdownContent_1.default, { content: "Plain text without markdown" }));
        expect(react_1.screen.getByText('Plain text without markdown')).toBeTruthy();
    });
    it('should handle empty content', () => {
        const { container } = (0, react_1.render)((0, jsx_runtime_1.jsx)(MarkdownContent_1.default, { content: "" }));
        expect(container.textContent).toBe('');
    });
    it('should handle undefined content', () => {
        const { container } = (0, react_1.render)((0, jsx_runtime_1.jsx)(MarkdownContent_1.default, {}));
        expect(container.textContent).toBe('');
    });
    it('should render multiple markdown elements together', () => {
        const content = `# Heading

**Bold text** and *italic text*

\`\`\`javascript
const x = 5;
\`\`\`

[Link](https://example.com)`;
        (0, react_1.render)((0, jsx_runtime_1.jsx)(MarkdownContent_1.default, { content: content }));
        expect(react_1.screen.getByRole('heading', { name: /Heading/i })).toBeTruthy();
        expect(react_1.screen.getByText('Bold text')).toBeTruthy();
        expect(react_1.screen.getByText('italic text')).toBeTruthy();
        expect(react_1.screen.getByText(/const x = 5/)).toBeTruthy();
        expect(react_1.screen.getByRole('link', { name: /Link/i })).toBeTruthy();
    });
});
