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 { RelationshipField } = await import('../../views/page-builder/RelationshipField.js');
const formField = {
    type: 'relationship',
    label: 'Form',
    relationTo: 'forms',
};
beforeEach(() => {
    cmsApi.mockReset();
});
describe('RelationshipField (form block picker)', () => {
    it('loads forms and lets the editor pick one by id', async () => {
        cmsApi.mockResolvedValue({
            data: [
                { id: 'f1', name: 'Contact Us', slug: 'contact-us' },
                { id: 'f2', name: 'Event RSVP', slug: 'event-rsvp' },
            ],
            status: 200,
        });
        const onChange = vi.fn();
        render(_jsx(RelationshipField, { definition: formField, value: "", onChange: onChange, label: "Form" }));
        await waitFor(() => expect(screen.getByText('Contact Us')).toBeTruthy());
        expect(cmsApi).toHaveBeenCalledWith('/forms');
        expect(screen.getByText('Event RSVP')).toBeTruthy();
        fireEvent.change(screen.getByLabelText('Form'), { target: { value: 'f2' } });
        expect(onChange).toHaveBeenCalledWith('f2');
    });
    it('shows an empty hint when the collection has no items', async () => {
        cmsApi.mockResolvedValue({ data: [], status: 200 });
        render(_jsx(RelationshipField, { definition: formField, value: "", onChange: vi.fn(), label: "Form" }));
        await waitFor(() => expect(screen.getByText('No forms found.')).toBeTruthy());
    });
    it('degrades to a plain id input when the collection cannot be loaded', async () => {
        cmsApi.mockResolvedValue({ error: 'boom', status: 500 });
        render(_jsx(RelationshipField, { definition: formField, value: "f9", onChange: vi.fn(), label: "Form" }));
        await waitFor(() => expect(screen.getByPlaceholderText('Enter forms id…').value).toBe('f9'));
    });
});
//# sourceMappingURL=page-builder-form-picker.render.test.js.map