import { listGeneratedKnowledgePages, } from '../generated-pages.js';
import { isGeneratedPageSource, readRecord } from './helpers.js';
export async function listHomeGraphPages(input) {
    return listGeneratedKnowledgePages({
        artifactStore: input.artifactStore,
        spaceId: input.spaceId,
        sources: input.sources,
        nodes: input.nodes,
        edges: input.edges,
        limit: input.limit,
        includeMarkdown: input.includeMarkdown,
        isGeneratedSource: isGeneratedPageSource,
        isPageSubjectNode,
        resolvePageSubject,
        projectNode: pageGraphNode,
        compareGeneratedPages,
    });
}
function resolvePageSubject(target, graph) {
    if (target.kind === 'ha_device_passport') {
        const deviceEdge = graph.edges.find((edge) => (edge.fromKind === 'node'
            && edge.fromId === target.id
            && edge.toKind === 'node'
            && edge.relation === 'source_for'));
        return deviceEdge ? graph.nodesById.get(deviceEdge.toId) : undefined;
    }
    if (isPageSubjectNode(target))
        return target;
    return undefined;
}
function isPageSubjectNode(node) {
    if (node.status === 'stale')
        return false;
    if (node.metadata.semanticKind)
        return false;
    return !['fact', 'knowledge_gap', 'wiki_page', 'ha_device_passport'].includes(node.kind);
}
function pageGraphNode(node) {
    const ha = readRecord(node.metadata.homeAssistant);
    return {
        id: node.id,
        kind: node.kind,
        title: node.title,
        ...(typeof ha.objectKind === 'string' ? { objectKind: ha.objectKind } : {}),
        ...(typeof ha.objectId === 'string' ? { objectId: ha.objectId } : {}),
        ...(typeof ha.entityId === 'string' ? { entityId: ha.entityId } : {}),
        ...(typeof ha.deviceId === 'string' ? { deviceId: ha.deviceId } : {}),
        ...(typeof ha.areaId === 'string' ? { areaId: ha.areaId } : {}),
        ...(typeof ha.integrationId === 'string' ? { integrationId: ha.integrationId } : {}),
    };
}
function compareGeneratedPages(left, right, graph) {
    const leftKind = typeof left.metadata.projectionKind === 'string' ? left.metadata.projectionKind : '';
    const rightKind = typeof right.metadata.projectionKind === 'string' ? right.metadata.projectionKind : '';
    return generatedPagePriority(right, graph) - generatedPagePriority(left, graph)
        || leftKind.localeCompare(rightKind)
        || (left.title ?? left.id).localeCompare(right.title ?? right.id)
        || left.id.localeCompare(right.id);
}
function generatedPagePriority(source, graph) {
    const target = graph.targetBySourceId.get(source.id);
    const subject = target ? resolvePageSubject(target, graph) : undefined;
    const projectionKind = typeof source.metadata.projectionKind === 'string' ? source.metadata.projectionKind : '';
    const subjectMetadata = subject?.metadata ?? {};
    const text = [
        source.title,
        source.summary,
        subject?.title,
        subject?.summary,
        typeof subjectMetadata.manufacturer === 'string' ? subjectMetadata.manufacturer : undefined,
        typeof subjectMetadata.model === 'string' ? subjectMetadata.model : undefined,
    ].filter(Boolean).join(' ').toLowerCase();
    let score = 0;
    if (projectionKind === 'device-passport')
        score += 30;
    if (projectionKind === 'room-page')
        score += 18;
    if (subject?.kind === 'ha_device')
        score += 24;
    if (subject?.kind === 'ha_area' || subject?.kind === 'ha_room')
        score += 16;
    if (/\b(tv|television|webos|iphone|phone|router|printer|camera|thermostat|lock|garage|espresso|appliance|esp32|proxy|speaker|receiver)\b/.test(text))
        score += 12;
    if (/\b(home assistant|plugin|theme|card|conversation|tts|stt|task|backup)\b/.test(text))
        score -= 10;
    return score;
}
