import { loadIdeas, updateIdea } from '../storage/ideas-store.js';
import { handleCreateSpec } from './create-spec.js';
/** Extract specId from create_spec response text (format: "SPEC-NNN"). */
function extractSpecId(text) {
    const match = /SPEC-\d+/.exec(text);
    return match?.[0];
}
/** Handle promote_idea tool call. */
export async function handlePromoteIdea(input) {
    const { projectPath, ideaId } = input;
    const ideas = await loadIdeas(projectPath);
    const idea = ideas.find((i) => i.id === ideaId);
    if (!idea) {
        return {
            content: [{ type: 'text', text: `Idea not found: ${ideaId}` }],
            isError: true,
        };
    }
    if (idea.status !== 'new' && idea.status !== 'in-review') {
        return {
            content: [
                {
                    type: 'text',
                    text: `Cannot promote idea with status "${idea.status}". Only "new" or "in-review" ideas can be promoted.`,
                },
            ],
            isError: true,
        };
    }
    const createResult = await handleCreateSpec({
        title: idea.title,
        description: idea.description,
        projectPath,
        tags: idea.tags,
    });
    const resultText = createResult.content.map((c) => c.text).join('\n');
    const specId = extractSpecId(resultText);
    await updateIdea(projectPath, ideaId, {
        status: 'converted',
        specId,
    });
    const confirmation = specId
        ? `Idea "${idea.title}" promoted to spec ${specId}.`
        : `Idea "${idea.title}" promoted to spec.`;
    return {
        content: [{ type: 'text', text: confirmation }],
    };
}
//# sourceMappingURL=promote-idea-handler.js.map