import { registerSerializer } from '../registry.js';
function readParams(entity) {
    const raw = entity.data.params;
    if (!Array.isArray(raw))
        return [];
    return raw
        .filter((p) => p !== null && typeof p === 'object')
        .map((p) => ({
        name: String(p.name ?? ''),
        in: String(p.in ?? ''),
        ...(typeof p.type === 'string' ? { type: p.type } : {}),
        ...(typeof p.required === 'boolean' ? { required: p.required } : {}),
        ...(typeof p.default === 'string' ? { default: p.default } : {}),
        ...(typeof p.description === 'string' ? { description: p.description } : {}),
    }));
}
function baseSingle(entity) {
    return {
        type: 'ui-view',
        slug: entity.slug,
        name: entity.data.name ?? entity.slug,
        url: entity.data.url ?? null,
        description: entity.data.description ?? null,
        params: readParams(entity),
        tags: entity.tags,
    };
}
function trimItem(entity) {
    const params = readParams(entity);
    return {
        type: 'ui-view',
        slug: entity.slug,
        name: entity.data.name ?? entity.slug,
        url: entity.data.url ?? null,
        description: entity.data.description ?? null,
        paramCount: params.length,
        tags: entity.tags,
    };
}
const PARAM_OBJECT_SCHEMA = {
    type: 'object',
    required: ['name', 'in'],
    properties: {
        name: { type: 'string' },
        in: { type: 'string', enum: ['path', 'query', 'hash'] },
        type: { type: 'string' },
        required: { type: 'boolean' },
        default: { type: 'string' },
        description: { type: 'string' },
    },
    additionalProperties: false,
};
const SINGLE_ELEMENT_SCHEMA = {
    type: 'object',
    required: ['type', 'slug', 'name', 'params', 'tags'],
    properties: {
        type: { const: 'ui-view' },
        slug: { type: 'string' },
        name: { type: 'string' },
        url: { type: ['string', 'null'] },
        description: { type: ['string', 'null'] },
        params: { type: 'array', items: PARAM_OBJECT_SCHEMA },
        tags: { type: 'array', items: { type: 'string' } },
    },
};
const uiViewSerializer = {
    type: 'ui-view',
    version: '1.0.0',
    inlineMention: (entity) => ({
        type: 'ui-view',
        slug: entity.slug,
        label: entity.data.name ?? entity.slug,
        url: entity.data.url ?? null,
        href: `/ui-views/${entity.slug}`,
    }),
    singleElement: (entity) => baseSingle(entity),
    elementListItem: (entity) => trimItem(entity),
    taggedListItem: (entity) => trimItem(entity),
    detail: (entity, ctx) => {
        const base = baseSingle(entity);
        const references = ctx.reader.findSectionReferences('ui-view', entity.id).map((r) => ({
            anchor: r.anchor,
            pagePath: r.pagePath,
            headingText: r.headingText,
            relation: r.relation,
        }));
        return {
            ...base,
            _references: references,
        };
    },
    schema: (view) => {
        if (view === 'single_element' || view === 'detail')
            return SINGLE_ELEMENT_SCHEMA;
        if (view === 'inline_mention') {
            return {
                type: 'object',
                required: ['type', 'slug', 'label', 'href'],
                properties: {
                    type: { const: 'ui-view' },
                    slug: { type: 'string' },
                    label: { type: 'string' },
                    url: { type: ['string', 'null'] },
                    href: { type: 'string' },
                },
            };
        }
        return {
            type: 'object',
            required: ['type', 'slug', 'name', 'paramCount', 'tags'],
            properties: {
                type: { const: 'ui-view' },
                slug: { type: 'string' },
                name: { type: 'string' },
                url: { type: ['string', 'null'] },
                description: { type: ['string', 'null'] },
                paramCount: { type: 'number' },
                tags: { type: 'array', items: { type: 'string' } },
            },
        };
    },
};
registerSerializer(uiViewSerializer);
//# sourceMappingURL=ui-view.js.map