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';
// Drive the global All Entries inbox through a mocked forms-service so the test
// owns the data and can assert the cross-form table (Form column + form
// filter), status-pill refetches, and opening the detail drawer.
function makeEntry(over = {}) {
    return {
        id: 'e1',
        entryNumber: 1,
        formId: 'f1',
        formName: 'Contact Us',
        formSlug: 'contact-us',
        data: { name: 'Jordan Avery', email: 'jordan@example.com', message: 'Hello there' },
        senderName: 'Jordan Avery',
        senderEmail: 'jordan@example.com',
        preview: 'Hello there',
        unread: true,
        starred: false,
        archived: false,
        spamStatus: 'clean',
        submittedAt: new Date().toISOString(),
        createdAt: new Date().toISOString(),
        updatedAt: new Date().toISOString(),
        ...over,
    };
}
const ENTRIES = [
    makeEntry(),
    makeEntry({
        id: 'e2',
        entryNumber: 2,
        formId: 'f2',
        formName: 'Project Inquiry',
        formSlug: 'project-inquiry',
        senderName: 'Priya Nair',
        senderEmail: 'priya@example.com',
        preview: 'A question about pricing',
        unread: false,
    }),
];
const fetchForms = vi.fn(async () => [
    { id: 'f1', name: 'Contact Us', slug: 'contact-us', status: 'active', fields: [] },
    { id: 'f2', name: 'Project Inquiry', slug: 'project-inquiry', status: 'active', fields: [] },
]);
const fetchEntries = vi.fn(async (_p) => ({
    entries: ENTRIES,
    total: ENTRIES.length,
    page: 1,
    pageSize: 25,
}));
const fetchEntryCounts = vi.fn(async (_p) => ({
    total: 2,
    unread: 1,
    starred: 0,
    thisWeek: 2,
    byForm: { f1: 1 },
}));
const fetchEntry = vi.fn(async (_id) => ({ entry: ENTRIES[0], fields: [] }));
const updateEntry = vi.fn(async (_id, _patch) => ({ data: ENTRIES[0] }));
vi.mock('../../lib/forms-service.js', () => ({
    fetchForms: () => fetchForms(),
    fetchEntries: (p) => fetchEntries(p),
    fetchEntryCounts: (p) => fetchEntryCounts(p),
    fetchEntry: (id) => fetchEntry(id),
    updateEntry: (id, patch) => updateEntry(id, patch),
    deleteEntry: vi.fn(async () => ({})),
    bulkUpdateEntries: vi.fn(async () => ({ data: { updated: 1 } })),
}));
vi.mock('../../lib/forms-events.js', () => ({
    onFormsChanged: () => () => { },
    emitFormsChanged: vi.fn(),
}));
vi.mock('sonner', () => ({ toast: { success: vi.fn(), error: vi.fn(), loading: vi.fn() } }));
const { FormEntries } = await import('../../views/FormEntries.js');
beforeEach(() => {
    fetchForms.mockClear();
    fetchEntries.mockClear();
    fetchEntryCounts.mockClear();
    fetchEntry.mockClear();
});
describe('FormEntries (All Entries)', () => {
    it('renders cross-form entries with a Form column and filter', async () => {
        render(_jsx(FormEntries, { onNavigate: () => { } }));
        expect(await screen.findByText('Jordan Avery')).toBeTruthy();
        expect(screen.getByText('Priya Nair')).toBeTruthy();
        expect(screen.getByRole('table', { name: 'All entries' })).toBeTruthy();
        // Form names appear (column values) and the filter is populated from fetchForms.
        expect(screen.getByLabelText('Filter by form')).toBeTruthy();
        await waitFor(() => expect(fetchForms).toHaveBeenCalled());
    });
    it('refetches scoped to a form when the form filter changes', async () => {
        render(_jsx(FormEntries, { onNavigate: () => { } }));
        await screen.findByText('Jordan Avery');
        fireEvent.change(screen.getByLabelText('Filter by form'), { target: { value: 'f2' } });
        await waitFor(() => expect(fetchEntries).toHaveBeenCalledWith(expect.objectContaining({ formId: 'f2' })));
    });
    it('refetches with the unread filter when the pill is clicked', async () => {
        render(_jsx(FormEntries, { onNavigate: () => { } }));
        await screen.findByText('Jordan Avery');
        fireEvent.click(screen.getByRole('button', { name: 'Unread', pressed: false }));
        await waitFor(() => expect(fetchEntries).toHaveBeenCalledWith(expect.objectContaining({ unread: true })));
    });
    it('opens the detail drawer when a row is clicked', async () => {
        render(_jsx(FormEntries, { onNavigate: () => { } }));
        const row = await screen.findByText('Jordan Avery');
        fireEvent.click(row);
        await waitFor(() => expect(fetchEntry).toHaveBeenCalledWith('e1'));
    });
});
//# sourceMappingURL=form-entries.render.test.js.map