// tools/generate-spec-from-issue.ts — SPEC-472: MCP handler
import { inferSpecFromIssue } from '../engine/spec-generators/issue-extractor.js';
import { trackCost } from '../engine/cost-tracking/operation-tracker.js';
import { formatKeyValue } from './output-formatter.js';
/** Stub extractor — real implementations call source-specific APIs (linear-sync, jira-sync, etc.) */
async function extractIssue(input) {
    // For each source, dynamically import the existing sync handler and reuse its API client.
    // Initial implementation: return a structured stub that downstream code can populate.
    // This file is the entry point — full per-source extractors are in src/engine/spec-generators/issue-extractor/{source}.ts
    const mod = await import('../engine/spec-generators/issue-extractor/github.js');
    return mod.extractGithubIssue(input.issueId, input.projectPath);
}
export async function handleGenerateSpecFromIssue(input) {
    return trackCost(input.projectPath, 'generate_spec_from_issue', async () => {
        try {
            const issue = await extractIssue(input);
            if (!issue) {
                return {
                    content: [
                        {
                            type: 'text',
                            text: `Could not extract issue ${input.issueId} from ${input.source}. Verify the source is configured (configure_${input.source}) and the issue ID is valid.`,
                        },
                    ],
                    isError: true,
                };
            }
            const result = inferSpecFromIssue(issue);
            const summary = [
                `Spec draft generated from ${input.source} issue ${input.issueId}.`,
                '',
                formatKeyValue({
                    Title: result.specDraft.title,
                    Target: result.specDraft.target,
                    Risk: result.specDraft.risk,
                    Type: result.specDraft.type,
                    Scope: result.specDraft.scope,
                }),
                '',
                'Call `create_spec` with this draft to persist it.',
            ].join('\n');
            return {
                content: [
                    { type: 'text', text: summary },
                    {
                        type: 'text',
                        text: `Spec draft generated from ${input.source} issue ${input.issueId}. ` +
                            `Title: "${result.specDraft.title}". Target: ${result.specDraft.target}. ` +
                            `Risk: ${result.specDraft.risk}. Call create_spec with this draft to persist it.`,
                    },
                ],
                structuredContent: result,
            };
        }
        catch (err) {
            const message = err instanceof Error ? err.message : String(err);
            return {
                content: [{ type: 'text', text: `Failed to generate spec from issue: ${message}` }],
                isError: true,
            };
        }
    }); // end trackCost
}
//# sourceMappingURL=generate-spec-from-issue.js.map