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, within } from '@testing-library/react';
/**
 * Content SEO tab with the inline editor pane (design split-pane pattern —
 * previously the `SeoEditorDrawer` overlay). Pins: row click toggles the
 * 380px pane, the pane loads the record through `fetchSeoContentRecord`, and
 * Save flows through the same `updateSeoFields` service call.
 */
function makeRecord(over = {}) {
    return {
        id: 'seo1',
        entityType: 'page',
        entityId: 'p1',
        title: 'Home',
        url: 'https://example.com/',
        canonicalUrl: null,
        metaTitle: 'Home — Example',
        metaTitlePage: 'Home',
        siteName: 'Example',
        titleTemplate: '%page% — %site%',
        metaDescription: 'Welcome to Example.',
        focusKeyword: null,
        keyTakeaway: null,
        ogTitle: null,
        ogDescription: null,
        ogImageId: null,
        ogImage: null,
        twitterTitle: null,
        twitterDescription: null,
        twitterImageId: null,
        noindex: false,
        nofollow: false,
        structuredDataType: 'WebPage',
        structuredData: null,
        llmsInclude: 'AUTO',
        llmsDescription: null,
        llmsSection: null,
        llmsOptional: false,
        seoScore: 82,
        seoGrade: 'good',
        updatedAt: '2026-06-01T00:00:00Z',
        ...over,
    };
}
const RECORDS = [
    makeRecord(),
    makeRecord({ id: 'seo2', entityType: 'post', entityId: 'p2', title: 'First Post' }),
];
const fetchSeoContentRecords = vi.fn(async () => RECORDS);
const fetchSeoContentRecord = vi.fn(async (entityType, entityId) => {
    return RECORDS.find((r) => r.entityType === entityType && r.entityId === entityId) ?? null;
});
const updateSeoFields = vi.fn(async (_t, _i, _p) => ({}));
vi.mock('@actuate-media/cms-core/client', async (importOriginal) => {
    const actual = await importOriginal();
    return { ...actual };
});
vi.mock('../../lib/seo-service.js', () => ({
    fetchSeoContentRecords: () => fetchSeoContentRecords(),
    fetchSeoContentRecord: (t, i) => fetchSeoContentRecord(t, i),
    fetchDocumentSeoAnalysis: vi.fn(async () => ({ score: 72, checks: [] })),
    updateSeoFields: (t, i, p) => updateSeoFields(t, i, p),
    generateSeoField: vi.fn(async () => ({ unavailable: true })),
    fetchLinkSuggestions: vi.fn(async () => ({ data: { suggestions: [] } })),
    SEO_SCHEMA_TYPES: ['Article', 'WebPage'],
    applyContentSeoQuery: (records, query) => records.filter((r) => {
        if (query.filter === 'pages' && r.entityType !== 'page')
            return false;
        if (query.filter === 'posts' && r.entityType !== 'post')
            return false;
        if (query.search && !r.title.toLowerCase().includes(query.search.toLowerCase()))
            return false;
        return true;
    }),
}));
vi.mock('../../lib/brand-voice-client.js', () => ({
    fixBrandVoiceContent: vi.fn(async () => ({ ok: false })),
    brandAlignmentTone: () => 'text-success',
    brandAlignmentLabel: () => 'On brand',
}));
vi.mock('sonner', () => ({ toast: { success: vi.fn(), error: vi.fn(), loading: vi.fn() } }));
const { ContentTab } = await import('../../views/seo/ContentTab.js');
beforeEach(() => {
    fetchSeoContentRecords.mockClear();
    fetchSeoContentRecord.mockClear();
    updateSeoFields.mockClear();
});
describe('Content SEO inline editor pane', () => {
    it('opens the pane on row click and loads the record', async () => {
        render(_jsx(ContentTab, {}));
        const rowButton = await screen.findByRole('button', { name: /Home/ });
        expect(rowButton.getAttribute('aria-expanded')).toBe('false');
        fireEvent.click(rowButton);
        expect(screen.getByRole('region', { name: 'SEO editor' })).toBeTruthy();
        expect(rowButton.getAttribute('aria-expanded')).toBe('true');
        await waitFor(() => expect(fetchSeoContentRecord).toHaveBeenCalledWith('page', 'p1'));
        // Form loads the stored page portion; preview applies the site title template.
        expect(await screen.findByDisplayValue('Home')).toBeTruthy();
        const editor = screen.getByRole('region', { name: 'SEO editor' });
        expect(within(editor).getAllByText('Home — Example').length).toBeGreaterThan(0);
    });
    it('toggles the pane closed when the same row is clicked again', async () => {
        render(_jsx(ContentTab, {}));
        const rowButton = await screen.findByRole('button', { name: /Home/ });
        fireEvent.click(rowButton);
        expect(screen.getByRole('region', { name: 'SEO editor' })).toBeTruthy();
        fireEvent.click(rowButton);
        expect(screen.queryByRole('region', { name: 'SEO editor' })).toBeNull();
    });
    it('saves edited fields through updateSeoFields', async () => {
        render(_jsx(ContentTab, {}));
        fireEvent.click(await screen.findByRole('button', { name: /Home/ }));
        const titleInput = await screen.findByDisplayValue('Home');
        fireEvent.change(titleInput, { target: { value: 'New Meta Title' } });
        fireEvent.click(screen.getByRole('button', { name: 'Save Changes' }));
        await waitFor(() => expect(updateSeoFields).toHaveBeenCalledWith('page', 'p1', expect.objectContaining({ metaTitle: 'New Meta Title' })));
        // The list refetches after a successful save.
        await waitFor(() => expect(fetchSeoContentRecords.mock.calls.length).toBeGreaterThan(1));
    });
    it('opens the pane on mount for deep links', async () => {
        render(_jsx(ContentTab, { initialEntityType: "post", initialEntityId: "p2" }));
        await waitFor(() => expect(fetchSeoContentRecord).toHaveBeenCalledWith('post', 'p2'));
        expect(screen.getByRole('region', { name: 'SEO editor' })).toBeTruthy();
    });
});
//# sourceMappingURL=seo-content-pane.render.test.js.map