import { jsx as _jsx } from "react/jsx-runtime";
// @vitest-environment happy-dom
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { fireEvent, render, screen } from '@testing-library/react';
// Drive the dashboard purely through `useApiData`. Each endpoint's result
// is looked up in `responses`, so tests can set just the data they care
// about and everything else falls back to a benign empty state.
const responses = {};
const emptyResult = { data: null, loading: false, error: null, exhausted: false, refetch: () => { } };
vi.mock('../../lib/useApiData.js', () => ({
    useApiData: (endpoint) => responses[endpoint ?? ''] ?? emptyResult,
}));
const { Dashboard } = await import('../../views/Dashboard.js');
const emptyStats = {
    totalDocuments: 0,
    totalMedia: 0,
    totalUsers: 0,
    formCount: 0,
    avgSeoScore: 0,
    webhookCount: 0,
    webhookActiveCount: 0,
    collectionCounts: {},
    statusCounts: {},
    recentDocuments: [],
};
function statsResult(over = {}) {
    return {
        data: { ...emptyStats, ...over },
        loading: false,
        error: null,
        exhausted: false,
        refetch: () => { },
    };
}
beforeEach(() => {
    for (const k of Object.keys(responses))
        delete responses[k];
});
describe('Dashboard', () => {
    it('renders a spinner while the primary stats request is still loading', () => {
        responses['/stats'] = {
            data: null,
            loading: true,
            error: null,
            exhausted: false,
            refetch: () => { },
        };
        const { container } = render(_jsx(Dashboard, { config: { collections: [] }, session: {}, onNavigate: () => { } }));
        expect(container.querySelector('.animate-spin')).toBeTruthy();
        expect(screen.queryByText('Recent Activity')).toBeNull();
    });
    it('shows the onboarding block instead of the activity grid when there is no content', () => {
        responses['/stats'] = statsResult();
        render(_jsx(Dashboard, { config: { collections: [] }, session: {}, onNavigate: () => { } }));
        // An effectively-empty site (zero posts+pages, zero media) swaps the
        // Recent Activity / Publishing Queue / Content Health grid for the
        // getting-started onboarding block.
        expect(screen.getByText(/Welcome to ActuateCMS/)).toBeTruthy();
        expect(screen.queryByText('No activity yet')).toBeNull();
        expect(screen.queryByText('No scheduled content')).toBeNull();
    });
    it('surfaces a retryable banner when the stats request is exhausted', () => {
        const refetch = vi.fn();
        responses['/stats'] = { data: null, loading: false, error: 'down', exhausted: true, refetch };
        render(_jsx(Dashboard, { config: { collections: [] }, session: {}, onNavigate: () => { } }));
        fireEvent.click(screen.getByText('Retry'));
        expect(refetch).toHaveBeenCalledTimes(1);
    });
    it('renders Delivery API metrics when api-delivery stats are available', () => {
        responses['/stats'] = statsResult();
        responses['/stats/api-delivery'] = {
            data: {
                hasData: true,
                requests24h: 1200,
                deltaPercent: 8,
                errorRate: 0.01,
                avgLatencyMs: 42,
                p95LatencyMs: 110,
                maxLatencyMs: 500,
                webhookCount: 2,
                webhookActiveCount: 1,
            },
            loading: false,
            error: null,
            exhausted: false,
            refetch: () => { },
        };
        render(_jsx(Dashboard, { config: { collections: [] }, session: {}, onNavigate: () => { } }));
        expect(screen.getByText('Delivery API')).toBeTruthy();
        expect(screen.getByText('1.2K')).toBeTruthy();
    });
    // ── EI-1 regression: recent-activity rows must open the *correct* editor ──
    it('routes a recent post to the section-based post editor (not the legacy editor)', () => {
        const onNavigate = vi.fn();
        responses['/stats'] = statsResult({
            recentDocuments: [
                {
                    id: 'p1',
                    title: 'My Post',
                    status: 'DRAFT',
                    collection: 'blog',
                    updatedAt: new Date().toISOString(),
                    author: 'Sam',
                },
            ],
        });
        render(_jsx(Dashboard, { config: {
                collections: [
                    { slug: 'blog', type: 'post', labels: { singular: 'Blog', plural: 'Blogs' } },
                ],
            }, session: {}, onNavigate: onNavigate }));
        // EI-2: the row is an accessible button, so it is reachable by role.
        fireEvent.click(screen.getByRole('button', { name: /My Post/ }));
        expect(onNavigate).toHaveBeenCalledWith('/posts/blog/p1/edit');
    });
    it('routes a recent page to the full page editor', () => {
        const onNavigate = vi.fn();
        responses['/stats'] = statsResult({
            recentDocuments: [
                {
                    id: 'pg1',
                    title: 'Home',
                    status: 'PUBLISHED',
                    collection: 'pages',
                    updatedAt: new Date().toISOString(),
                    author: 'Sam',
                },
            ],
        });
        render(_jsx(Dashboard, { config: {
                collections: [
                    { slug: 'pages', type: 'page', labels: { singular: 'Page', plural: 'Pages' } },
                ],
            }, session: {}, onNavigate: onNavigate }));
        fireEvent.click(screen.getByRole('button', { name: /Home/ }));
        expect(onNavigate).toHaveBeenCalledWith('/pages/pg1/edit');
    });
    it('routes a scheduled queue item through the same editor resolver', () => {
        const onNavigate = vi.fn();
        responses['/stats'] = statsResult({
            recentDocuments: [
                {
                    id: 'p9',
                    title: 'Scheduled Post',
                    status: 'SCHEDULED',
                    collection: 'blog',
                    updatedAt: new Date().toISOString(),
                    author: 'Sam',
                },
            ],
        });
        render(_jsx(Dashboard, { config: {
                collections: [
                    { slug: 'blog', type: 'post', labels: { singular: 'Blog', plural: 'Blogs' } },
                ],
            }, session: {}, onNavigate: onNavigate }));
        // The publishing-queue row (right column) — there are two buttons named
        // for the post (activity + queue); the queue "Manage" header button also
        // targets the first scheduled item. Click the queue row directly.
        const queueButtons = screen.getAllByRole('button', { name: /Scheduled Post/ });
        fireEvent.click(queueButtons[queueButtons.length - 1]);
        expect(onNavigate).toHaveBeenCalledWith('/posts/blog/p9/edit');
    });
});
//# sourceMappingURL=dashboard.test.js.map