export function mergeAppExtensions(entries) {
    const warnings = [];
    const merged = new Map();
    for (const entry of entries) {
        const existing = merged.get(entry.propertyKey);
        if (!existing) {
            merged.set(entry.propertyKey, {
                propertyKey: entry.propertyKey,
                inferredTypeText: entry.inferredTypeText,
                pluginFiles: [entry.pluginFile],
                sourceKinds: [entry.sourceKind],
                confidence: entry.confidence,
                conflict: false,
            });
            continue;
        }
        existing.pluginFiles = sortUnique([...existing.pluginFiles, entry.pluginFile]);
        existing.sourceKinds = sortUnique([...existing.sourceKinds, entry.sourceKind]);
        existing.confidence = mergeConfidence(existing.confidence, entry.confidence);
        if (existing.inferredTypeText !== entry.inferredTypeText) {
            warnings.push(`Conflicting inferred types for app.extend("${entry.propertyKey}") in ${entry.pluginFile}; falling back to unknown.`);
            existing.inferredTypeText = "unknown";
            existing.conflict = true;
            existing.confidence = "low";
        }
    }
    return {
        entries: [...merged.values()].sort((a, b) => a.propertyKey.localeCompare(b.propertyKey)),
        warnings,
    };
}
function mergeConfidence(left, right) {
    const rank = {
        high: 3,
        medium: 2,
        low: 1,
    };
    return rank[left] <= rank[right] ? left : right;
}
function sortUnique(values) {
    return [...new Set(values)].sort((a, b) => a.localeCompare(b));
}
