import { jsx as _jsx } from "react/jsx-runtime";
// @vitest-environment happy-dom
import { afterEach, describe, expect, it, vi } from 'vitest';
import { render, screen } from '@testing-library/react';
const fetchPageForEditor = vi.fn();
vi.mock('../../lib/page-editor-service.js', async (importOriginal) => {
    const actual = await importOriginal();
    return {
        ...actual,
        fetchPageForEditor: (...args) => fetchPageForEditor(...args),
    };
});
const { PageSectionEditor } = await import('../../views/page-editor/PageSectionEditor.js');
const { AdminSectionRenderersProvider } = await import('../../views/page-editor/sections/index.js');
const validConfig = {
    collections: {
        pages: {
            slug: 'pages',
            fields: {
                title: { type: 'text' },
                slug: { type: 'slug' },
                path: { type: 'text' },
                sections: { type: 'json' },
                metaTitle: { type: 'text' },
                metaDescription: { type: 'text' },
            },
        },
    },
};
afterEach(() => {
    fetchPageForEditor.mockReset();
});
describe('PageSectionEditor schema guard', () => {
    it('refuses to operate when the pages collection is missing the sections field', async () => {
        const badConfig = {
            collections: { pages: { slug: 'pages', fields: { title: { type: 'text' } } } },
        };
        render(_jsx(PageSectionEditor, { documentId: "p1", config: badConfig, onNavigate: () => { }, session: {} }));
        expect(await screen.findByText(/missing required field/i)).toBeTruthy();
        expect(screen.getByText(/sections/)).toBeTruthy();
        // The guard must short-circuit before any network read.
        expect(fetchPageForEditor).not.toHaveBeenCalled();
    });
    it('does not block when no config is provided (embedding/test contexts)', async () => {
        render(_jsx(PageSectionEditor, { config: undefined, onNavigate: () => { }, session: {} }));
        // New page (no documentId) → empty editor shell renders.
        expect(await screen.findByText('Sections')).toBeTruthy();
    });
});
describe('PageSectionEditor new page', () => {
    it('renders the editor shell without fetching for /pages/new', async () => {
        render(_jsx(PageSectionEditor, { config: validConfig, onNavigate: () => { }, session: {} }));
        expect(await screen.findByText('Sections')).toBeTruthy();
        expect(fetchPageForEditor).not.toHaveBeenCalled();
    });
    it('hides the version history button for unsaved pages', async () => {
        render(_jsx(PageSectionEditor, { config: validConfig, onNavigate: () => { }, session: {} }));
        await screen.findByText('Sections');
        expect(screen.queryByRole('button', { name: 'Version history' })).toBeNull();
    });
    it('auto-opens Page settings so a new page starts by naming it', async () => {
        render(_jsx(PageSectionEditor, { config: validConfig, onNavigate: () => { }, session: {} }));
        expect(await screen.findByRole('dialog', { name: 'Page settings' })).toBeTruthy();
        expect(screen.getByLabelText(/Title/)).toBeTruthy();
    });
    it('does not auto-open Page settings for existing pages', async () => {
        fetchPageForEditor.mockResolvedValue(loadedPage([]));
        render(_jsx(PageSectionEditor, { documentId: "p1", config: validConfig, onNavigate: () => { }, session: {} }));
        await screen.findByText('Sections');
        expect(screen.queryByRole('dialog', { name: 'Page settings' })).toBeNull();
    });
});
function loadedPage(sections) {
    return {
        id: 'p1',
        title: 'Home',
        slug: 'home',
        path: '/home',
        status: 'DRAFT',
        sections: sections.map((s) => ({ ...s, visible: true, content: {}, settings: {} })),
        seoTitle: '',
        seoDescription: '',
        publishedAt: null,
        updatedAt: null,
        builderOwned: false,
    };
}
describe('PageSectionEditor structure mode', () => {
    it('stays out of structure mode when every section has a canvas renderer', async () => {
        fetchPageForEditor.mockResolvedValue(loadedPage([{ id: 's1', sectionType: 'hero', name: 'Hero' }]));
        render(_jsx(PageSectionEditor, { documentId: "p1", config: validConfig, onNavigate: () => { }, session: {} }));
        await screen.findByText('Sections');
        expect(screen.queryByText(/Structure view/)).toBeNull();
        // "View on site" stays an icon-only button.
        expect(screen.getByRole('button', { name: 'View draft on site' })).toBeTruthy();
        expect(screen.getByRole('button', { name: 'Preview page' })).toBeTruthy();
    });
    it('shows the structure notice and promotes "View on site" for custom section types', async () => {
        fetchPageForEditor.mockResolvedValue(loadedPage([
            { id: 's1', sectionType: 'hero', name: 'Hero' },
            { id: 's2', sectionType: 'opal-flip-cards', name: 'Flip Cards' },
        ]));
        render(_jsx(PageSectionEditor, { documentId: "p1", config: validConfig, onNavigate: () => { }, session: {} }));
        await screen.findByText('Sections');
        expect(screen.getByText(/Structure view/)).toBeTruthy();
        // Promoted to a labeled button — the primary design preview.
        expect(screen.getByRole('button', { name: /View on site/ })).toBeTruthy();
        // Eye preview is relabeled to set expectations.
        expect(screen.getByRole('button', { name: /Preview structure/ })).toBeTruthy();
    });
    it('stays out of structure mode when a custom renderer is registered for the type', async () => {
        fetchPageForEditor.mockResolvedValue(loadedPage([
            { id: 's1', sectionType: 'hero', name: 'Hero' },
            { id: 's2', sectionType: 'opal-flip-cards', name: 'Flip Cards' },
        ]));
        render(_jsx(AdminSectionRenderersProvider, { value: { 'opal-flip-cards': () => _jsx("div", { "data-testid": "opal-canvas" }) }, children: _jsx(PageSectionEditor, { documentId: "p1", config: validConfig, onNavigate: () => { }, session: {} }) }));
        await screen.findByText('Sections');
        // The registered renderer draws the section, so the canvas is no longer
        // structural for this page.
        expect(screen.getByTestId('opal-canvas')).toBeTruthy();
        expect(screen.queryByText(/Structure view/)).toBeNull();
        expect(screen.getByRole('button', { name: 'View draft on site' })).toBeTruthy();
    });
    it('exposes a version history button for saved pages', async () => {
        fetchPageForEditor.mockResolvedValue(loadedPage([{ id: 's1', sectionType: 'hero', name: 'Hero' }]));
        render(_jsx(PageSectionEditor, { documentId: "p1", config: validConfig, onNavigate: () => { }, session: {} }));
        await screen.findByText('Sections');
        expect(screen.getByRole('button', { name: 'Version history' })).toBeTruthy();
    });
});
//# sourceMappingURL=page-section-editor.test.js.map