import { Router } from 'express';
import { DomainError } from '../services/tags.js';
import { errorHandler } from './errors.js';
function resolveEntityId(type, slug, endpoints, dtos, databaseTables, uiViews) {
    if (type === 'endpoint') {
        const id = endpoints.getIdBySlug(slug);
        if (id == null)
            throw new DomainError('NOT_FOUND', `endpoint '${slug}' not found`);
        return id;
    }
    if (type === 'dto') {
        const id = dtos.getIdBySlug(slug);
        if (id == null)
            throw new DomainError('NOT_FOUND', `dto '${slug}' not found`);
        return id;
    }
    if (type === 'database-table') {
        const id = databaseTables.getIdBySlug(slug);
        if (id == null)
            throw new DomainError('NOT_FOUND', `database table '${slug}' not found`);
        return id;
    }
    if (type === 'ui-view') {
        const id = uiViews.getIdBySlug(slug);
        if (id == null)
            throw new DomainError('NOT_FOUND', `ui view '${slug}' not found`);
        return id;
    }
    throw new DomainError('NOT_FOUND', `entity type '${type}' not supported yet`);
}
function assertType(type) {
    if (type === 'endpoint' ||
        type === 'dto' ||
        type === 'database-table' ||
        type === 'ui-view' ||
        type === 'section')
        return type;
    throw new DomainError('VALIDATION', `unsupported entity type '${type}'`);
}
export function entitiesRouter(endpoints, dtos, databaseTables, uiViews, tags, versions) {
    const router = Router();
    router.get('/:type/:slug/versions', (req, res, next) => {
        try {
            const type = assertType(req.params.type);
            const id = resolveEntityId(type, req.params.slug, endpoints, dtos, databaseTables, uiViews);
            res.json({ versions: versions.listVersions(type, id) });
        }
        catch (err) {
            next(err);
        }
    });
    router.get('/:type/:slug/versions/:version', (req, res, next) => {
        try {
            const type = assertType(req.params.type);
            const id = resolveEntityId(type, req.params.slug, endpoints, dtos, databaseTables, uiViews);
            const version = Number(req.params.version);
            const detail = versions.getVersion(type, id, version);
            if (!detail)
                return res.status(404).json({ error: { code: 'NOT_FOUND', message: 'version not found' } });
            res.json(detail);
        }
        catch (err) {
            next(err);
        }
    });
    router.post('/:type/:slug/tags', (req, res, next) => {
        try {
            const type = assertType(req.params.type);
            const id = resolveEntityId(type, req.params.slug, endpoints, dtos, databaseTables, uiViews);
            const body = req.body;
            if (!Array.isArray(body.tags)) {
                return res.status(400).json({ error: { code: 'VALIDATION', message: 'tags[] required' } });
            }
            const assigned = tags.assignTags(type, id, body.tags);
            res.json({ tags: assigned });
        }
        catch (err) {
            next(err);
        }
    });
    router.use(errorHandler);
    return router;
}
//# sourceMappingURL=entities.js.map