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';
// Own the three backends the General tab reads: the `settings` global
// (site info / locale), the shared `/seo/config` (site URL + robots), and the
// computed `/seo/index-status` (the status card).
const STATUS = {
    status: 'protected',
    severity: 'warning',
    label: 'Protected',
    description: 'Non-production environment is noindexed. Production remains unaffected.',
    environment: 'preview',
    checks: [
        { key: 'environment', label: 'Environment', value: 'Preview', status: 'pass' },
        { key: 'siteUrl', label: 'Site URL configured', value: 'Yes', status: 'pass' },
        { key: 'sitemap', label: 'Sitemap enabled', value: 'Yes', status: 'pass' },
    ],
    actions: [
        { label: 'Run SEO Audit', href: '/seo?tab=audit', action: 'run_seo_audit' },
        { label: 'View robots.txt', href: '/robots.txt' },
    ],
    lastAuditAt: null,
};
const cmsApi = vi.fn(async (endpoint, options) => {
    const method = options?.method ?? 'GET';
    if (endpoint === '/globals/settings' && method === 'GET') {
        return {
            data: {
                siteTitle: 'Actuate Media',
                tagline: 'A lightweight content management system',
                language: 'en-US',
                timezone: 'America/Los_Angeles',
                aiProvider: 'anthropic',
            },
            status: 200,
        };
    }
    if (endpoint === '/seo/config' && method === 'GET') {
        return {
            data: {
                static: { site: null },
                overrides: { site: {} },
                effective: {
                    site: {
                        siteUrl: 'https://actuatemedia.com',
                        robots: { defaultNoIndex: false, defaultNoFollow: false, noIndexNonProduction: true },
                    },
                },
            },
            status: 200,
        };
    }
    if (endpoint === '/seo/index-status' && method === 'GET') {
        return { data: STATUS, status: 200 };
    }
    return { data: {}, status: 200 };
});
vi.mock('../../lib/api.js', () => ({ cmsApi: (e, o) => cmsApi(e, o) }));
const { GeneralSettingsTab } = await import('../../views/settings/GeneralSettingsTab.js');
beforeEach(() => {
    cmsApi.mockClear();
});
describe('GeneralSettingsTab', () => {
    it('renders the index status pill, loaded site info, and a clean (disabled) save bar', async () => {
        render(_jsx(GeneralSettingsTab, { role: "ADMIN" }));
        await screen.findByLabelText('Site Title');
        const statusCard = screen.getByRole('region', { name: 'Site Index Status' });
        expect(within(statusCard).getByText('Protected')).toBeTruthy();
        expect(screen.getByLabelText('Site Title').value).toBe('Actuate Media');
        expect(screen.getByLabelText('Site URL').value).toBe('https://actuatemedia.com');
        // Nothing dirty yet → Save disabled.
        expect(screen.getByRole('button', { name: 'Save Changes' }).disabled).toBe(true);
    });
    it('enables Save when a field changes and persists site info to the settings global', async () => {
        render(_jsx(GeneralSettingsTab, { role: "ADMIN" }));
        const title = (await screen.findByLabelText('Site Title'));
        fireEvent.change(title, { target: { value: 'Actuate Media Co' } });
        const save = screen.getByRole('button', { name: 'Save Changes' });
        expect(save.disabled).toBe(false);
        fireEvent.click(save);
        await waitFor(() => expect(cmsApi).toHaveBeenCalledWith('/globals/settings', expect.objectContaining({ method: 'PUT' })));
    });
    it('blocks save and shows an error for an invalid Site URL', async () => {
        render(_jsx(GeneralSettingsTab, { role: "ADMIN" }));
        await screen.findByLabelText('Site Title');
        const url = screen.getByLabelText('Site URL');
        fireEvent.change(url, { target: { value: 'not a url' } });
        expect((await screen.findAllByText(/valid absolute URL/i)).length).toBeGreaterThan(0);
        expect(screen.getByRole('button', { name: 'Save Changes' }).disabled).toBe(true);
    });
    it('confirms before turning OFF non-production noindex', async () => {
        render(_jsx(GeneralSettingsTab, { role: "ADMIN" }));
        await screen.findByLabelText('Site Title');
        // The non-production noindex toggle is ON; clicking it should request confirmation.
        fireEvent.click(screen.getByRole('switch', { name: 'Noindex Non-Production Environments' }));
        expect(await screen.findByText(/Preview and staging environments may become indexable/i)).toBeTruthy();
    });
    it('is read-only for non-admins', async () => {
        render(_jsx(GeneralSettingsTab, { role: "EDITOR" }));
        expect(await screen.findByText(/read-only access to settings/i)).toBeTruthy();
        expect(screen.getByLabelText('Site Title').disabled).toBe(true);
    });
});
//# sourceMappingURL=general-settings.render.test.js.map