import { analyzeDiff, generateSpecDraft, runGitDiff, runGhPrDiff, } from '../engine/diff-spec-generator.js';
// ---------------------------------------------------------------------------
// Shared output formatter
// ---------------------------------------------------------------------------
function formatDraft(draft) {
    const criteria = draft.acceptanceCriteria.map((c, i) => `${i + 1}. ${c}`).join('\n');
    const files = draft.affectedFiles.length > 0
        ? draft.affectedFiles.slice(0, 5).join(', ') +
            (draft.affectedFiles.length > 5 ? ` (+${draft.affectedFiles.length - 5} more)` : '')
        : '(none detected)';
    return [
        `## Generated Spec Draft`,
        '',
        `**Title**: ${draft.title}`,
        `**Source**: ${draft.sourceType} — \`${draft.sourceRef}\``,
        `**Effort**: ${draft.estimatedEffort ?? 'Unknown'}`,
        `**Generated At**: ${draft.generatedAt}`,
        '',
        `### Description`,
        draft.description,
        '',
        `### Acceptance Criteria`,
        criteria,
        '',
        `### Affected Files`,
        files,
    ].join('\n');
}
// ---------------------------------------------------------------------------
// Handlers
// ---------------------------------------------------------------------------
export function handleGenerateSpecFromDiff(input) {
    const analysis = analyzeDiff(input.diff);
    const draft = generateSpecDraft(analysis, 'diff', 'inline-diff', input.title);
    if (input.outputMode === 'create') {
        return Promise.resolve({
            content: [
                {
                    type: 'text',
                    text: `${formatDraft(draft)}\n\nTo create this spec, call create_spec with the title and criteria above.`,
                },
            ],
        });
    }
    return Promise.resolve({ content: [{ type: 'text', text: formatDraft(draft) }] });
}
export function handleGenerateSpecFromPr(input) {
    let rawDiff;
    try {
        rawDiff = runGhPrDiff(input.repoPath ?? input.projectPath, input.prNumber);
    }
    catch (err) {
        return Promise.resolve({
            content: [
                {
                    type: 'text',
                    text: `Error: ${err instanceof Error ? err.message : String(err)}`,
                },
            ],
            isError: true,
        });
    }
    const analysis = analyzeDiff(rawDiff, `PR #${input.prNumber}`);
    const draft = generateSpecDraft(analysis, 'pr', `PR #${input.prNumber}`);
    return Promise.resolve({ content: [{ type: 'text', text: formatDraft(draft) }] });
}
export function handleGenerateSpecFromCommitRange(input) {
    let rawDiff;
    try {
        rawDiff = runGitDiff(input.projectPath, input.commitRange);
    }
    catch (err) {
        return Promise.resolve({
            content: [
                {
                    type: 'text',
                    text: `Error: ${err instanceof Error ? err.message : String(err)}`,
                },
            ],
            isError: true,
        });
    }
    const analysis = analyzeDiff(rawDiff, input.commitRange);
    const draft = generateSpecDraft(analysis, 'commit-range', input.commitRange);
    return Promise.resolve({ content: [{ type: 'text', text: formatDraft(draft) }] });
}
//# sourceMappingURL=diff-spec-generator-handler.js.map