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 list purely through a mocked posts-service so the test can
// control what each page returns. fetchPosts echoes the requested page so
// we can assert page-scoped selection behaviour without a real backend.
function makePost(id, title) {
    return {
        id,
        postTypeId: 'articles',
        postTypeName: 'Article',
        postTypeAccentColor: 'blue',
        title,
        slug: id,
        authorName: 'Author',
        authorInitials: 'AU',
        status: 'PUBLISHED',
        updatedDate: '2026-01-01T00:00:00.000Z',
        seoScore: 80,
    };
}
const PAGE_ONE = [makePost('a1', 'Alpha'), makePost('a2', 'Alpha Two')];
const PAGE_TWO = [makePost('b1', 'Beta'), makePost('b2', 'Beta Two')];
const fetchPosts = vi.fn(async ({ page }) => ({
    posts: page === 1 ? PAGE_ONE : PAGE_TWO,
    total: 20,
    types: [{ slug: 'articles', name: 'Article', postCount: 20 }],
    authors: [],
}));
const bulkUpdatePosts = vi.fn(async () => ({ ok: 0, failed: 0 }));
const bulkDeletePosts = vi.fn(async () => ({ ok: 0, failed: 0 }));
vi.mock('../../lib/posts-service.js', () => ({
    fetchPosts: (args) => fetchPosts(args),
    bulkUpdatePosts: (...args) => bulkUpdatePosts(...args),
    bulkDeletePosts: (...args) => bulkDeletePosts(...args),
}));
const { PostsListView } = await import('../../views/Posts/PostsListView.js');
beforeEach(() => {
    fetchPosts.mockClear();
    bulkUpdatePosts.mockClear();
    bulkDeletePosts.mockClear();
});
describe('PostsListView — page-scoped selection', () => {
    it('clears the selection when navigating to another page so the bulk count stays honest', async () => {
        render(_jsx(PostsListView, { onNavigate: () => { } }));
        // Wait for page 1 to load, then select a row.
        const alpha = await screen.findByLabelText('Select "Alpha"');
        fireEvent.click(alpha);
        expect(screen.getByText('1 selected')).toBeTruthy();
        // Navigate to page 2.
        fireEvent.click(screen.getByLabelText('Next page'));
        // Page 2 rows render and the stale selection is gone — no bulk bar,
        // so a bulk publish/delete can never silently no-op against page 1.
        await screen.findByLabelText('Select "Beta"');
        expect(screen.queryByText('1 selected')).toBeNull();
        expect(screen.queryByText(/selected$/)).toBeNull();
    });
    it('keeps the bulk count equal to the rows a bulk action will touch', async () => {
        render(_jsx(PostsListView, { onNavigate: () => { } }));
        const alpha = await screen.findByLabelText('Select "Alpha"');
        const alphaTwo = await screen.findByLabelText('Select "Alpha Two"');
        fireEvent.click(alpha);
        fireEvent.click(alphaTwo);
        expect(screen.getByText('2 selected')).toBeTruthy();
        fireEvent.click(screen.getByText('Publish'));
        await waitFor(() => expect(bulkUpdatePosts).toHaveBeenCalledTimes(1));
        // The handler received exactly the two rows the bar advertised.
        const ids = bulkUpdatePosts.mock.calls[0][0];
        expect(ids).toHaveLength(2);
    });
});
//# sourceMappingURL=posts-list-view.render.test.js.map