// tools/github-release-handler.ts — Handlers for create_release and generate_changelog.
import { createPlatformClient, getRemoteUrl } from '../engine/github/client.js';
import { detectAuth } from '../engine/github/auth.js';
import { fetchMergedPRs, formatChangelog, formatChangelogJson, } from '../engine/github/changelog.js';
import { execFile as nodeExecFile } from 'node:child_process';
import { promisify } from 'node:util';
const defaultExec = async (cmd, args) => {
    return promisify(nodeExecFile)(cmd, args, { maxBuffer: 10 * 1024 * 1024 });
};
export async function handleCreateRelease(args, exec = defaultExec) {
    const auth = await detectAuth(exec);
    if (auth.method === 'none') {
        return {
            content: [{ type: 'text', text: `GitHub auth required.\n${auth.message ?? ''}` }],
            isError: true,
        };
    }
    const remoteUrl = await getRemoteUrl(exec);
    const client = createPlatformClient(remoteUrl, exec);
    const result = await client.createRelease({
        version: args.version,
        generateNotes: args.generateNotes,
        draft: args.draft,
        prerelease: args.prerelease,
    });
    return {
        content: [
            {
                type: 'text',
                text: [
                    `Release created successfully!`,
                    `  Tag: ${result.tagName}`,
                    `  URL: ${result.url}`,
                    `  Draft: ${String(args.draft)}`,
                    `  Prerelease: ${String(args.prerelease)}`,
                ].join('\n'),
            },
        ],
    };
}
export async function handleGenerateChangelog(args, exec = defaultExec) {
    const entries = await fetchMergedPRs(args.since, args.until, exec);
    if (entries.length === 0) {
        return {
            content: [{ type: 'text', text: `No merged PRs found since ${args.since}.` }],
        };
    }
    const output = args.format === 'json'
        ? formatChangelogJson(entries)
        : formatChangelog(entries, args.since, args.until);
    return {
        content: [{ type: 'text', text: output }],
    };
}
//# sourceMappingURL=github-release-handler.js.map