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';
/**
 * PostFieldsModal — control selection is driven by the post type's field
 * config: a `select` category renders a structured dropdown (UpChat taxonomy
 * feedback P1), an `array` tags field renders the chip input (P2). Free-text
 * configs keep their legacy inputs so existing sites are unaffected.
 */
const cmsApi = vi.fn(async () => ({ data: { field: 'tags', values: [] }, status: 200 }));
vi.mock('../../lib/api.js', () => ({
    cmsApi: (...args) => cmsApi(...args),
    ensureCsrfToken: vi.fn(async () => { }),
}));
// The rich text editor pulls in heavy dependencies; stub it.
vi.mock('../../fields/RichTextField.js', () => ({
    RichTextField: () => _jsx("div", { "data-testid": "rich-text" }),
}));
const { PostFieldsModal } = await import('../../views/post-editor/PostFieldsModal.js');
function basePost(over = {}) {
    return {
        id: 'p1',
        postType: 'posts',
        title: 'Hello',
        slug: 'hello',
        excerpt: '',
        featuredImage: '',
        body: '',
        category: '',
        tags: [],
        status: 'DRAFT',
        publishDate: null,
        sections: [],
        seoTitle: '',
        seoDescription: '',
        publishedAt: null,
        updatedAt: null,
        ...over,
    };
}
const SELECT_FIELDS = {
    category: {
        type: 'select',
        options: [
            { label: 'News', value: 'news' },
            { label: 'Tutorials', value: 'tutorials' },
        ],
    },
    tags: { type: 'array', maxRows: 12 },
};
beforeEach(() => {
    cmsApi.mockClear();
});
describe('PostFieldsModal', () => {
    it('renders a category dropdown when the field config is a select', () => {
        render(_jsx(PostFieldsModal, { open: true, post: basePost(), canEdit: true, fields: SELECT_FIELDS, onClose: () => { }, onSave: () => { } }));
        const select = screen.getByLabelText('Category');
        expect(select.tagName).toBe('SELECT');
        const labels = [...select.options].map((o) => o.textContent);
        expect(labels).toEqual(['No category', 'News', 'Tutorials']);
    });
    it('keeps a legacy free-text category selectable instead of discarding it', () => {
        render(_jsx(PostFieldsModal, { open: true, post: basePost({ category: 'Hand Typed' }), canEdit: true, fields: SELECT_FIELDS, onClose: () => { }, onSave: () => { } }));
        const select = screen.getByLabelText('Category');
        expect(select.value).toBe('Hand Typed');
    });
    it('falls back to a free-text category input without field config', () => {
        render(_jsx(PostFieldsModal, { open: true, post: basePost(), canEdit: true, onClose: () => { }, onSave: () => { } }));
        const input = screen.getByLabelText('Category');
        expect(input.tagName).toBe('INPUT');
        // No tags config → no tags control.
        expect(screen.queryByLabelText('Tags')).toBeNull();
    });
    it('saves selected category and edited tags in the patch', () => {
        const onSave = vi.fn();
        render(_jsx(PostFieldsModal, { open: true, post: basePost({ tags: ['AI'] }), canEdit: true, fields: SELECT_FIELDS, onClose: () => { }, onSave: onSave }));
        fireEvent.change(screen.getByLabelText('Category'), { target: { value: 'tutorials' } });
        const tagInput = screen.getByLabelText('Tags');
        fireEvent.change(tagInput, { target: { value: 'CMS' } });
        fireEvent.keyDown(tagInput, { key: 'Enter' });
        fireEvent.click(screen.getByRole('button', { name: 'Done' }));
        expect(onSave).toHaveBeenCalledWith(expect.objectContaining({ category: 'tutorials', tags: ['AI', 'CMS'] }));
    });
});
//# sourceMappingURL=post-fields-modal.render.test.js.map