import { buildPrBody, createPullRequestSafely, getGitProvider, stripTaktManagedPrMarker, } from '../../infra/git/index.js';
import { error, info, success } from '../../shared/ui/index.js';
import { expandPipelineTemplate } from './templateExpander.js';
function buildPipelinePrBody(pipelineConfig, issue, report) {
    const template = pipelineConfig?.prBodyTemplate;
    if (template) {
        return expandPipelineTemplate(template, {
            title: issue?.title ?? '',
            issue: issue ? String(issue.number) : '',
            issue_body: issue?.body || issue?.title || '',
            report,
        });
    }
    return buildPrBody(issue ? [issue] : undefined, report);
}
function requireBaseBranch(baseBranch) {
    if (!baseBranch) {
        throw new Error('Base branch is required (pull request creation)');
    }
    return baseBranch;
}
export function submitPullRequest(projectCwd, branch, baseBranch, taskContent, workflow, pipelineConfig, options) {
    info('Creating pull request...');
    const prTitle = taskContent.issue ? `[#${taskContent.issue.number}] ${taskContent.issue.title}` : (options.task ?? 'Pipeline task');
    const report = `Workflow \`${workflow}\` completed successfully.`;
    const prBody = stripTaktManagedPrMarker(buildPipelinePrBody(pipelineConfig, taskContent.issue, report));
    const prResult = createPullRequestSafely(getGitProvider(), {
        branch,
        title: prTitle,
        body: prBody,
        base: requireBaseBranch(baseBranch),
        repo: options.repo,
        draft: options.draftPr,
    }, projectCwd);
    if (prResult.success) {
        success(`PR created: ${prResult.url}`);
        return prResult.url;
    }
    error(`PR creation failed: ${prResult.error}`);
    return undefined;
}
//# sourceMappingURL=prSubmission.js.map