import { DEFAULT_BUILT_IN_BACKEND_PROFILES, getBuiltInBackendProfile } from './builtInBackendProfiles.js';
function normalizeQuery(query) {
    return typeof query === 'string' ? query.trim() : '';
}
function normalizeName(name) {
    return typeof name === 'string' ? name.trim().toLowerCase() : '';
}
function toCandidate(profile) {
    return { id: profile.id, name: profile.name, isBuiltIn: profile.isBuiltIn === true };
}
export function resolveBackendProfile(params) {
    const query = normalizeQuery(params.query);
    if (!query)
        return { ok: false, reason: 'not_found', query: '' };
    const customProfiles = params.customProfiles ?? [];
    const customById = customProfiles.find((p) => p.id === query) ?? null;
    if (customById)
        return { ok: true, profile: customById, resolvedBy: 'id' };
    const builtInById = getBuiltInBackendProfile(query);
    if (builtInById)
        return { ok: true, profile: builtInById, resolvedBy: 'id' };
    const byNameKey = normalizeName(query);
    const matchesByName = [];
    for (const profile of customProfiles) {
        if (normalizeName(profile.name) === byNameKey) {
            matchesByName.push(profile);
        }
    }
    for (const profile of DEFAULT_BUILT_IN_BACKEND_PROFILES) {
        if (normalizeName(profile.name) === byNameKey) {
            matchesByName.push(profile);
        }
    }
    if (matchesByName.length === 1) {
        return { ok: true, profile: matchesByName[0], resolvedBy: 'name' };
    }
    if (matchesByName.length > 1) {
        return {
            ok: false,
            reason: 'ambiguous_name',
            query,
            candidates: matchesByName.map((p) => toCandidate(p)),
        };
    }
    return { ok: false, reason: 'not_found', query };
}
//# sourceMappingURL=resolveBackendProfile.js.map