import { Router } from 'express';
import { pluginHost } from '../core/plugin-host/host.js';
import { errorHandler } from './errors.js';
/**
 * Validate `type` URL param against the plugin host registry; `section` is
 * accepted as a special non-entity case used by the references service.
 */
function assertType(type) {
    if (type === 'section')
        return type;
    if (pluginHost.getAvailable(type))
        return type;
    throw new Error(`unsupported entity type '${type}'`);
}
export function referencesRouter(references) {
    const router = Router();
    router.get('/', async (req, res, next) => {
        try {
            const type = typeof req.query.type === 'string' ? assertType(req.query.type) : null;
            const slug = typeof req.query.slug === 'string' ? req.query.slug : null;
            if (!type || !slug) {
                return res.status(400).json({ error: { code: 'VALIDATION', message: 'type and slug query params required' } });
            }
            const hits = await references.findReferences(type, slug);
            res.json({ references: hits });
        }
        catch (err) {
            next(err);
        }
    });
    router.use(errorHandler);
    return router;
}
//# sourceMappingURL=references.js.map