import { readJsonFile } from '../lib/json.js';
import { getPackagePath } from '../lib/paths.js';
import { ItemInsightsFileSchema, ProvidersFileSchema, RankingPolicySchema, RecommendationWeightsSchema, RegistriesFileSchema, SecurityPolicySchema } from '../lib/validation/contracts.js';
const REGISTRIES_PATH = getPackagePath('config/registries.json');
const SECURITY_POLICY_PATH = getPackagePath('config/security-policy.json');
const RANKING_POLICY_PATH = getPackagePath('config/ranking-policy.json');
const RECOMMENDATION_WEIGHTS_PATH = getPackagePath('config/recommendation-weights.json');
const PROVIDERS_PATH = getPackagePath('config/providers.json');
const ITEM_INSIGHTS_PATH = getPackagePath('config/item-insights.json');
export async function loadRegistries() {
    const raw = await readJsonFile(REGISTRIES_PATH);
    const parsed = RegistriesFileSchema.parse(raw);
    return parsed.registries.filter((registry) => registry.enabled);
}
export async function loadSecurityPolicy() {
    const raw = await readJsonFile(SECURITY_POLICY_PATH);
    return SecurityPolicySchema.parse(raw);
}
export async function loadRankingPolicy() {
    try {
        const raw = await readJsonFile(RANKING_POLICY_PATH);
        return RankingPolicySchema.parse(raw);
    }
    catch {
        const fallback = RecommendationWeightsSchema.parse(await readJsonFile(RECOMMENDATION_WEIGHTS_PATH));
        return RankingPolicySchema.parse({
            weights: {
                compatibility: fallback.compatibility,
                capabilityCoverage: fallback.capabilityCoverage,
                maintenance: fallback.maintenance,
                provenance: 18,
                adoption: fallback.adoption,
                freshnessBonusMax: 8,
                securityPenaltyMax: fallback.securityPenaltyMax,
                blockedPenalty: 40
            },
            tieBreakers: ['trust', 'risk', 'name'],
            blockedFloorTier: 'high'
        });
    }
}
export async function loadProviders() {
    const raw = await readJsonFile(PROVIDERS_PATH);
    const parsed = ProvidersFileSchema.parse(raw);
    return new Map(parsed.providers.filter((provider) => provider.enabled).map((provider) => [provider.id, provider]));
}
export async function loadItemInsights() {
    const raw = await readJsonFile(ITEM_INSIGHTS_PATH);
    const parsed = ItemInsightsFileSchema.parse(raw);
    return new Map(parsed.insights.map((item) => [item.id, item]));
}
