function getScopeMappings(adapter, scope) {
    return scope === 'project'
        ? adapter.projectMappings
        : scope === 'user'
            ? adapter.userMappings
            : [...adapter.projectMappings, ...adapter.userMappings];
}
export async function getActiveAdapters(adapters, scopeRoot) {
    const detected = await Promise.all(adapters.map(async (adapter) => ({
        adapter,
        isDetected: await adapter.detect(scopeRoot),
    })));
    return detected
        .filter((result) => result.isDetected)
        .map((result) => result.adapter);
}
export async function getConfigAwareAdapters(adapters, scopeRoot, config) {
    const detected = await Promise.all(adapters.map(async (adapter) => ({
        adapter,
        isDetected: await adapter.detect(scopeRoot),
        configuredEnabled: config.providers[adapter.name]?.enabled,
    })));
    const activeAdapters = [];
    const detectedUnset = [];
    const detectedDisabled = [];
    for (const result of detected) {
        if (result.configuredEnabled === true) {
            activeAdapters.push(result.adapter);
            continue;
        }
        if (result.configuredEnabled === false) {
            if (result.isDetected) {
                detectedDisabled.push(result.adapter.name);
            }
            continue;
        }
        if (result.isDetected) {
            activeAdapters.push(result.adapter);
            detectedUnset.push(result.adapter.name);
        }
    }
    return {
        activeAdapters,
        detectedUnset,
        detectedDisabled,
    };
}
export function getSyncMappings(adapter, scope) {
    const syncMappings = getScopeMappings(adapter, scope).filter((mapping) => !mapping.nativeRead);
    const seen = new Set();
    return syncMappings.filter((mapping) => {
        const key = `${mapping.contentType}::${mapping.canonicalDir}::${mapping.providerDir}`;
        if (seen.has(key)) {
            return false;
        }
        seen.add(key);
        return true;
    });
}
export function getAdoptionSources(adapter, scope) {
    const sources = getScopeMappings(adapter, scope).flatMap((mapping) => {
        const directories = mapping.nativeRead
            ? (mapping.adoptionSourceDirs ?? [])
            : [mapping.providerDir];
        return directories.map((directory) => ({
            contentType: mapping.contentType,
            directory,
            mapping,
        }));
    });
    const seen = new Set();
    return sources.filter((source) => {
        const key = `${source.contentType}::${source.directory}`;
        if (seen.has(key)) {
            return false;
        }
        seen.add(key);
        return true;
    });
}
