import { registerSerializer } from '../registry.js';
function label(entity) {
    const method = entity.data.method ?? '';
    const path = entity.data.path ?? '';
    return `${method} ${path}`.trim();
}
function href(entity) {
    return `/endpoints/${entity.slug}`;
}
function baseSingle(entity, ctx, includeDtos = true) {
    const dtos = includeDtos ? ctx.reader.findEndpointDtos(entity.id) : undefined;
    return {
        type: 'endpoint',
        slug: entity.slug,
        method: entity.data.method,
        path: entity.data.path,
        summary: entity.data.summary ?? '',
        description: entity.data.description ?? null,
        tags: entity.tags,
        ...(dtos !== undefined ? { dtos: formatDtos(dtos) } : {}),
    };
}
function formatDtos(dtos) {
    return dtos.map((d) => ({
        dtoSlug: d.dtoSlug,
        dtoName: d.dtoName,
        relation: d.relation,
        statusCode: d.statusCode,
    }));
}
const endpointSerializer = {
    type: 'endpoint',
    version: '1.0.0',
    inlineMention: (entity) => ({
        type: 'endpoint',
        slug: entity.slug,
        label: label(entity),
        href: href(entity),
    }),
    singleElement: (entity, ctx) => baseSingle(entity, ctx, true),
    elementListItem: (entity, ctx) => {
        const base = baseSingle(entity, ctx, true);
        const description = base.description ? base.description.split('\n')[0] : null;
        return { ...base, description };
    },
    taggedListItem: (entity, ctx) => {
        const base = baseSingle(entity, ctx, true);
        const description = base.description ? base.description.split('\n')[0] : null;
        return { ...base, description };
    },
    detail: (entity, ctx) => {
        const base = baseSingle(entity, ctx, true);
        const brokenRefs = [];
        const dtos = ctx.reader.findEndpointDtos(entity.id);
        const dtoObjects = dtos.map((link) => {
            const dto = ctx.reader.getEntity('dto', link.dtoSlug);
            if (!dto) {
                brokenRefs.push(`dto:${link.dtoSlug}`);
                return { ...link, dto: null };
            }
            if (ctx.depth >= ctx.maxDepth) {
                return { ...link, dto: { slug: dto.slug, name: dto.data.name, _truncated: true } };
            }
            return {
                ...link,
                dto: {
                    slug: dto.slug,
                    name: dto.data.name,
                    description: dto.data.description ?? null,
                    fields: dto.data.fields,
                    tags: dto.tags,
                },
            };
        });
        const references = ctx.reader.findSectionReferences('endpoint', entity.id);
        return {
            ...base,
            dtos: dtoObjects,
            _references: formatReferences(references),
            ...(brokenRefs.length ? { _brokenRefs: brokenRefs } : {}),
        };
    },
};
function formatReferences(refs) {
    return refs.map((r) => ({
        anchor: r.anchor,
        pagePath: r.pagePath,
        headingText: r.headingText,
        relation: r.relation,
    }));
}
registerSerializer(endpointSerializer);
//# sourceMappingURL=endpoint.js.map