import { DEFAULT_KNOWLEDGE_SPACE_ID, getExplicitKnowledgeSpaceId, normalizeKnowledgeSpaceId, } from '../spaces.js';
export function concreteAnswerGapSpaceId(requestedSpaceId, evidence, sources, linkedObjects) {
    const explicitRecordSpace = concreteRecordSpaceId(evidence, sources, linkedObjects);
    if (explicitRecordSpace && explicitRecordSpace !== DEFAULT_KNOWLEDGE_SPACE_ID)
        return explicitRecordSpace;
    if (normalizeKnowledgeSpaceId(requestedSpaceId) !== 'homeassistant')
        return requestedSpaceId;
    if (explicitRecordSpace)
        return explicitRecordSpace;
    return requestedSpaceId;
}
function concreteRecordSpaceId(evidence, sources, linkedObjects) {
    for (const record of [
        ...linkedObjects,
        ...sources,
        ...evidence.flatMap((item) => [item.node, item.source]),
    ]) {
        const explicitSpaceId = record ? getExplicitKnowledgeSpaceId(record) : null;
        if (!explicitSpaceId)
            continue;
        const spaceId = normalizeKnowledgeSpaceId(explicitSpaceId);
        if (spaceId !== DEFAULT_KNOWLEDGE_SPACE_ID)
            return spaceId;
    }
    return null;
}
