import { jsx as _jsx } from "react/jsx-runtime";
// @vitest-environment happy-dom
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
const cmsApi = vi.fn();
vi.mock('../../lib/api.js', () => ({ cmsApi: (...args) => cmsApi(...args) }));
const { BlockRenderer } = await import('../../views/page-builder/canvas/BlockRenderer.js');
function formNode(over = {}) {
    return {
        id: 'node-1',
        type: 'block',
        settings: { blockType: 'form', variant: 'contact' },
        data: { formId: 'f1' },
        ...over,
    };
}
beforeEach(() => {
    cmsApi.mockReset();
    cmsApi.mockResolvedValue({
        data: {
            name: 'Contact Us',
            fields: [
                { key: 'name', label: 'Full Name', type: 'text' },
                { key: 'message', label: 'Message', type: 'textarea' },
            ],
        },
        status: 200,
    });
});
describe('canvas BlockRenderer', () => {
    it('renders the live form preview with the linked form name and fields', async () => {
        render(_jsx(BlockRenderer, { node: formNode(), selectedNodeId: null, onSelectNode: vi.fn() }));
        await waitFor(() => expect(screen.getByText('Contact Us')).toBeTruthy());
        expect(cmsApi).toHaveBeenCalledWith('/forms/f1');
        expect(screen.getByText('Full Name')).toBeTruthy();
    });
    it('selects the node on click', () => {
        const onSelectNode = vi.fn();
        const { container } = render(_jsx(BlockRenderer, { node: formNode({ data: {} }), selectedNodeId: null, onSelectNode: onSelectNode }));
        fireEvent.click(container.querySelector('[data-node-id="node-1"]'));
        expect(onSelectNode).toHaveBeenCalledWith('node-1');
    });
    it('falls back to a placeholder for unknown block types', () => {
        render(_jsx(BlockRenderer, { node: formNode({ settings: { blockType: 'widget' }, data: {} }), selectedNodeId: null, onSelectNode: vi.fn() }));
        expect(screen.getByText('No preview available')).toBeTruthy();
    });
});
//# sourceMappingURL=page-builder-block-renderer.render.test.js.map