import { DURABLE_MEMORY_INDEX_MAX_CHARS } from './constants.js';
function summarizeTopic(topic) {
    return topic.entries[0]?.summary ?? topic.title;
}
function buildTopicIndexLines(topic) {
    const when = topic.whenToRead.slice(0, 2).join('; ');
    const lines = [`- \`${topic.fileName}\`: ${summarizeTopic(topic)}`];
    if (when) {
        lines.push(`  - Read when: ${when}`);
    }
    return lines;
}
export function deriveMemoryIndex(topics, maxChars = DURABLE_MEMORY_INDEX_MAX_CHARS) {
    if (topics.length === 0)
        return null;
    const sortedTopics = [...topics].sort((left, right) => left.fileName.localeCompare(right.fileName));
    const lines = [
        '# Durable Memory Index',
        '',
        'Read only the topic files that are clearly relevant to the current task.'
    ];
    for (const topic of sortedTopics) {
        const topicLines = buildTopicIndexLines(topic);
        const candidate = [...lines, '', ...topicLines].join('\n').trimEnd();
        if (candidate.length > maxChars)
            break;
        lines.push('', ...topicLines);
    }
    return lines.join('\n').trim();
}
//# sourceMappingURL=derive-index.js.map