import { jsx as _jsx } from "react/jsx-runtime";
// @vitest-environment happy-dom
import { beforeEach, describe, expect, it, vi, afterEach } from 'vitest';
import { cleanup, render, screen, waitFor } from '@testing-library/react';
const cmsApi = vi.fn();
vi.mock('../../lib/api.js', () => ({ cmsApi: (...args) => cmsApi(...args) }));
vi.mock('sonner', () => ({
    toast: { success: vi.fn(), error: vi.fn(), loading: vi.fn() },
    Toaster: () => null,
}));
const { PageBuilder } = await import('../../views/page-builder/PageBuilder.js');
const TEMPLATE_TREE = {
    id: 'tpl-page',
    type: 'page',
    children: [
        {
            id: 'sec1',
            type: 'section',
            settings: { label: 'Hero From Template' },
            children: [],
        },
    ],
};
beforeEach(() => {
    cmsApi.mockReset();
    cmsApi.mockImplementation(async (endpoint) => {
        if (endpoint === '/page-templates/tpl-1') {
            return { data: { id: 'tpl-1', name: 'Landing', tree: TEMPLATE_TREE }, status: 200 };
        }
        return { data: {}, status: 200 };
    });
});
afterEach(cleanup);
describe('PageBuilder template seeding (/page-builder/new?template=)', () => {
    it('fetches the template and seeds the canvas tree for a new page', async () => {
        render(_jsx(PageBuilder, { templateId: "tpl-1", collectionSlug: "pages", config: {}, onNavigate: () => { } }));
        await waitFor(() => expect(cmsApi).toHaveBeenCalledWith('/page-templates/tpl-1'));
        // replaceTree marks the builder dirty — the unsaved-changes dot is the
        // observable proof the template tree was seeded into the canvas state.
        expect(await screen.findByTitle('Unsaved changes')).toBeTruthy();
    });
    it('starts from a blank, clean tree when no template id is given', async () => {
        render(_jsx(PageBuilder, { collectionSlug: "pages", config: {}, onNavigate: () => { } }));
        await waitFor(() => expect(cmsApi).not.toHaveBeenCalledWith(expect.stringContaining('/page-templates/')));
        expect(screen.queryByTitle('Unsaved changes')).toBeNull();
    });
});
//# sourceMappingURL=page-builder-template-seed.render.test.js.map