function resolveCachedDiarizationProvider(metadata) {
    const provider = metadata?.diarizationProvider;
    return provider === "elevenlabs" || provider === "openai" ? provider : null;
}
function isCachedDiarizationCompatible(metadata, preference) {
    if (!preference)
        return true;
    const hasSpeakerLabels = metadata?.speakerLabels === true;
    const provider = resolveCachedDiarizationProvider(metadata);
    return hasSpeakerLabels && (preference === "auto" || provider === preference);
}
function scopeUrl(scope, url) {
    return `summarize-diarize:${scope}:${url}`;
}
export function resolveTranscriptDiarizationCacheReadScopes(preference) {
    if (!preference)
        return null;
    if (preference === "auto") {
        return ["elevenlabs", "openai", "auto"];
    }
    return [preference, "auto"];
}
export function resolveTranscriptDiarizationCacheWriteScope({ preference, metadata, }) {
    if (!preference)
        return null;
    if (preference === "auto") {
        return resolveCachedDiarizationProvider(metadata) ?? "auto";
    }
    return preference;
}
export function scopeTranscriptCacheForDiarization(cache, preference) {
    const readScopes = resolveTranscriptDiarizationCacheReadScopes(preference);
    if (!cache.store || !readScopes)
        return cache;
    const base = cache.store.transcriptCache;
    const transcriptCache = {
        get: async (args) => {
            let fallback = null;
            for (const scope of readScopes) {
                const cached = await base.get({ ...args, url: scopeUrl(scope, args.url) });
                if (!cached)
                    continue;
                fallback ??= cached;
                if (cached.expired)
                    continue;
                if (!isCachedDiarizationCompatible(cached.metadata, preference))
                    continue;
                return cached;
            }
            return fallback;
        },
        set: async (args) => {
            const scope = resolveTranscriptDiarizationCacheWriteScope({
                preference,
                metadata: args.metadata ?? null,
            });
            return await base.set({
                ...args,
                url: scope ? scopeUrl(scope, args.url) : args.url,
            });
        },
    };
    return {
        ...cache,
        store: {
            ...cache.store,
            transcriptCache,
        },
    };
}
//# sourceMappingURL=transcript-diarization-cache-scope.js.map