import { loadIdeas } from '../storage/ideas-store.js';
const STATUS_LABELS = {
    new: 'New',
    'in-review': 'In Review',
    converted: 'Converted',
    discarded: 'Discarded',
};
function isExpired(idea) {
    if (!idea.expiresAt) {
        return false;
    }
    return new Date(idea.expiresAt) < new Date();
}
function formatIdea(idea) {
    const expiredFlag = isExpired(idea) ? ' ⚠️ expired' : '';
    const status = STATUS_LABELS[idea.status];
    const tags = idea.tags.length > 0 ? ` [${idea.tags.join(', ')}]` : '';
    return `- **${idea.id}**${expiredFlag}: ${idea.title}${tags} (${status})`;
}
/** Group ideas by first tag (or "untagged" if no tags). */
function groupByTag(ideas) {
    const groups = new Map();
    for (const idea of ideas) {
        const key = idea.tags[0] ?? 'untagged';
        const group = groups.get(key) ?? [];
        group.push(idea);
        groups.set(key, group);
    }
    return groups;
}
/** Handle list_backlog tool call. */
export async function handleListBacklog(input) {
    const { projectPath, status, tag } = input;
    const all = await loadIdeas(projectPath);
    let filtered = all;
    if (status) {
        filtered = filtered.filter((i) => i.status === status);
    }
    if (tag) {
        filtered = filtered.filter((i) => i.tags.includes(tag));
    }
    if (filtered.length === 0) {
        return {
            content: [{ type: 'text', text: 'No ideas found matching the given filters.' }],
        };
    }
    const groups = groupByTag(filtered);
    const lines = [
        `## Ideas Backlog (${filtered.length} idea${filtered.length !== 1 ? 's' : ''})`,
        '',
    ];
    for (const [groupTag, groupIdeas] of groups) {
        lines.push(`### ${groupTag}`);
        for (const idea of groupIdeas) {
            lines.push(formatIdea(idea));
        }
        lines.push('');
    }
    return {
        content: [{ type: 'text', text: lines.join('\n') }],
    };
}
//# sourceMappingURL=list-backlog-handler.js.map