import { jsx as _jsx } from "react/jsx-runtime";
// @vitest-environment happy-dom
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { render, screen } from '@testing-library/react';
import { SectionRenderer, AdminSectionRenderersProvider, } from '../../views/page-editor/sections/index.js';
function customSection(overrides = {}) {
    return {
        id: 's1',
        sectionType: 'opal-flip-cards',
        name: 'Flip Cards',
        visible: true,
        content: { heading: 'Why us' },
        settings: {},
        ...overrides,
    };
}
describe('pluggable admin section renderers', () => {
    it('renders the structural placeholder when no renderer is registered', () => {
        render(_jsx(SectionRenderer, { section: customSection() }));
        expect(screen.getByText(/Custom section "opal-flip-cards"/)).toBeTruthy();
    });
    it('renders a registered custom renderer instead of the placeholder', () => {
        const renderers = {
            'opal-flip-cards': ({ section }) => (_jsx("div", { "data-testid": "opal-renderer", children: String(section.content?.heading) })),
        };
        render(_jsx(AdminSectionRenderersProvider, { value: renderers, children: _jsx(SectionRenderer, { section: customSection() }) }));
        expect(screen.getByTestId('opal-renderer').textContent).toBe('Why us');
        expect(screen.queryByText(/Custom section "opal-flip-cards"/)).toBeNull();
    });
    it('passes the resolved render options to the custom renderer', () => {
        const seen = [];
        const renderers = {
            'opal-flip-cards': ({ options }) => {
                seen.push(options);
                return _jsx("div", { children: options.renderImage({ src: '/x.png', alt: 'x' }) });
            },
        };
        render(_jsx(AdminSectionRenderersProvider, { value: renderers, children: _jsx(SectionRenderer, { section: customSection() }) }));
        const options = seen[0];
        expect(options.sanitizeHtml('<b>x</b>')).toBe('<b>x</b>');
        expect(options.baseUrl).toBe('');
        expect(screen.getByAltText('x')).toBeTruthy();
    });
    describe('throwing renderers', () => {
        beforeEach(() => {
            // React logs boundary-caught errors; keep test output clean.
            vi.spyOn(console, 'error').mockImplementation(() => { });
        });
        afterEach(() => {
            vi.restoreAllMocks();
        });
        it('falls back to the structural placeholder instead of crashing the canvas', () => {
            const renderers = {
                'opal-flip-cards': () => {
                    throw new Error('consumer renderer exploded');
                },
            };
            render(_jsx(AdminSectionRenderersProvider, { value: renderers, children: _jsx(SectionRenderer, { section: customSection() }) }));
            expect(screen.getByText(/Custom section "opal-flip-cards"/)).toBeTruthy();
        });
    });
    it('does not affect built-in section types', () => {
        const renderers = {
            hero: () => _jsx("div", { "data-testid": "hijacked-hero" }),
        };
        render(_jsx(AdminSectionRenderersProvider, { value: renderers, children: _jsx(SectionRenderer, { section: customSection({ sectionType: 'hero', name: 'Hero', content: {} }) }) }));
        // Built-in types always use the built-in canvas component.
        expect(screen.queryByTestId('hijacked-hero')).toBeNull();
    });
});
//# sourceMappingURL=admin-section-renderers.test.js.map