import { describe, expect, it } from 'vitest';
import { defineSectionTypes, validateSectionTypeInputs, SECTION_FIELD_TYPES } from '../define.js';
const pricing = {
    id: 'pricing',
    name: 'Pricing Table',
    scope: ['page'],
    fields: [
        { key: 'heading', label: 'Heading', type: 'text', required: true },
        {
            key: 'tier',
            label: 'Tier',
            type: 'select',
            options: [{ label: 'Free', value: 'free' }],
        },
    ],
};
describe('defineSectionTypes', () => {
    it('returns the input unchanged for valid definitions', () => {
        const input = [pricing];
        expect(defineSectionTypes(input)).toBe(input);
    });
    it('accepts a type with no editable fields', () => {
        expect(() => defineSectionTypes([{ id: 'divider', name: 'Divider', scope: ['page'], fields: [] }])).not.toThrow();
    });
    it('throws when an id collides with a built-in section type', () => {
        expect(() => defineSectionTypes([{ id: 'hero', name: 'Evil Hero', scope: ['page'], fields: [] }])).toThrow(/"hero" collides with a built-in/);
    });
    it('throws on duplicate ids within the array', () => {
        expect(() => defineSectionTypes([pricing, { ...pricing }])).toThrow(/"pricing" is duplicated/);
    });
    it('throws on a blank id and a blank name', () => {
        expect(() => defineSectionTypes([{ id: ' ', name: '', scope: ['page'], fields: [] }])).toThrow(/id must be a non-empty string[\s\S]*name must be a non-empty string/);
    });
    it('throws on an empty or invalid scope', () => {
        expect(() => defineSectionTypes([{ ...pricing, id: 'a', scope: [] }])).toThrow(/scope must be a non-empty array/);
        expect(() => defineSectionTypes([{ ...pricing, id: 'b', scope: ['blog'] }])).toThrow(/valid scopes are 'page' and 'post'/);
    });
    it('throws on an unknown field type and names the valid ones', () => {
        expect(() => defineSectionTypes([
            {
                id: 'broken',
                name: 'Broken',
                scope: ['page'],
                fields: [{ key: 'x', label: 'X', type: 'markdown' }],
            },
        ])).toThrow(/"markdown" is not a known field type[\s\S]*text, textarea/);
    });
    it('throws on duplicate field keys within a type', () => {
        expect(() => defineSectionTypes([
            {
                id: 'dupes',
                name: 'Dupes',
                scope: ['page'],
                fields: [
                    { key: 'heading', label: 'A', type: 'text' },
                    { key: 'heading', label: 'B', type: 'text' },
                ],
            },
        ])).toThrow(/"heading" is duplicated/);
    });
    it('requires options for select fields and a sub-schema for repeaters', () => {
        expect(() => defineSectionTypes([
            {
                id: 'sel',
                name: 'Sel',
                scope: ['page'],
                fields: [{ key: 'mode', label: 'Mode', type: 'select' }],
            },
        ])).toThrow(/select field and must declare a non-empty `options` array/);
        expect(() => defineSectionTypes([
            {
                id: 'rep',
                name: 'Rep',
                scope: ['page'],
                fields: [{ key: 'rows', label: 'Rows', type: 'repeater' }],
            },
        ])).toThrow(/repeater and must declare a non-empty `fields` sub-schema/);
    });
    it('validates repeater sub-fields recursively', () => {
        expect(() => defineSectionTypes([
            {
                id: 'plans',
                name: 'Plans',
                scope: ['page'],
                fields: [
                    {
                        key: 'rows',
                        label: 'Rows',
                        type: 'repeater',
                        fields: [{ key: '', label: 'Bad', type: 'text' }],
                    },
                ],
            },
        ])).toThrow(/fields\[0\]\.fields\[0\]\.key must be a non-empty string/);
    });
    it('aggregates every problem into one error', () => {
        let message = '';
        try {
            defineSectionTypes([
                { id: 'hero', name: '', scope: [], fields: [] },
                { id: '', name: 'Two', scope: ['page'], fields: [] },
            ]);
        }
        catch (err) {
            message = err.message;
        }
        expect(message).toMatch(/Invalid custom section type definitions:/);
        // One bullet per problem, each prefixed with its path.
        expect(message.match(/^ {2}- /gm)?.length).toBeGreaterThanOrEqual(4);
        expect(message).toContain('types[0]');
        expect(message).toContain('types[1]');
    });
});
describe('validateSectionTypeInputs', () => {
    it('returns an empty array for valid input', () => {
        expect(validateSectionTypeInputs([pricing])).toEqual([]);
    });
    it('rejects non-array input', () => {
        expect(validateSectionTypeInputs({})).toEqual([
            'defineSectionTypes expects an array of section type definitions',
        ]);
    });
});
describe('SECTION_FIELD_TYPES', () => {
    it('covers the repeater shorthands and core kinds', () => {
        expect(SECTION_FIELD_TYPES).toContain('stats');
        expect(SECTION_FIELD_TYPES).toContain('gallery');
        expect(SECTION_FIELD_TYPES).toContain('repeater');
        expect(SECTION_FIELD_TYPES).toContain('text');
    });
});
//# sourceMappingURL=define.test.js.map