// Copyright (c) 2026 Modelware. All rights reserved.
import { getTemplateById } from './catalog.js';
export function resolveTemplateById(catalog, templateId) {
    return getTemplateById(catalog, templateId);
}
export function resolveTemplateForNavigation(catalog, rdfTypes) {
    const candidates = collectNavigationCandidates(catalog, rdfTypes);
    const template = selectBestTemplate(candidates);
    return { template, candidates };
}
function collectNavigationCandidates(catalog, rdfTypes) {
    const normalizedTypes = [...new Set(rdfTypes.map(normalizeIri).filter((value) => value.length > 0))];
    const normalizedTypeSet = new Set(normalizedTypes);
    const results = [];
    for (const template of catalog.all) {
        const exposures = template.exposures?.filter((exposure) => exposure.kind === 'navigation') ?? [];
        if (exposures.length === 0) {
            continue;
        }
        let bestScoreForTemplate;
        for (const exposure of exposures) {
            const score = scoreNavigationExposure(exposure, normalizedTypeSet);
            if (score === undefined) {
                continue;
            }
            if (bestScoreForTemplate === undefined || score > bestScoreForTemplate) {
                bestScoreForTemplate = score;
            }
        }
        if (bestScoreForTemplate === undefined) {
            continue;
        }
        const rank = Number.isFinite(template.rank) ? Number(template.rank) : 0;
        results.push({ template, score: bestScoreForTemplate + rank });
    }
    return results.sort((left, right) => right.score - left.score || left.template.id.localeCompare(right.template.id));
}
function scoreNavigationExposure(exposure, rdfTypeSet) {
    const anyTypeOf = (exposure.match?.anyTypeOf ?? []).map(normalizeIri).filter((value) => value.length > 0);
    const allTypesOf = (exposure.match?.allTypesOf ?? []).map(normalizeIri).filter((value) => value.length > 0);
    if (allTypesOf.length > 0 && allTypesOf.some((iri) => !rdfTypeSet.has(iri))) {
        return undefined;
    }
    if (anyTypeOf.length > 0 && !anyTypeOf.some((iri) => rdfTypeSet.has(iri))) {
        return undefined;
    }
    if (allTypesOf.length === 0 && anyTypeOf.length === 0) {
        return 0;
    }
    return (allTypesOf.length * 10) + anyTypeOf.length;
}
function selectBestTemplate(candidates) {
    return candidates.length > 0 ? candidates[0].template : undefined;
}
function normalizeIri(value) {
    return value.trim().replace(/^<|>$/g, '');
}
//# sourceMappingURL=resolver.js.map