"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.loadPullRequestIssueForHandoff = loadPullRequestIssueForHandoff;
const errors_1 = require("../../platform/ticket-tracker/errors");
const types_1 = require("../../platform/ticket-tracker/types");
async function loadPullRequestIssueForHandoff(context) {
    try {
        return {
            issue: await context.ticketTracker.getIssue(context.state.pullRequestThreadId),
            allowInitialTitleSync: true,
            currentLabelNames: undefined,
        };
    }
    catch (error) {
        if (!(0, errors_1.isTrackerMissingWorkflowLabelSetError)(error)) {
            throw error;
        }
    }
    const issueRecord = await context.ticketTracker.getIssueRecord(context.state.pullRequestThreadId);
    return {
        issue: buildPullRequestIssueForHandoff({
            issueRecord,
            sourceBranchName: context.state.branchName,
            workflowState: context.state.handoffPullRequestSnapshot,
        }),
        allowInitialTitleSync: false,
        currentLabelNames: issueRecord.labelNames ?? [],
    };
}
function buildPullRequestIssueForHandoff(input) {
    if (input.issueRecord.kind !== types_1.TRACKER_ITEM_KINDS.pullRequest) {
        throw new Error("Expected PR handoff target to be a pull request.");
    }
    return {
        ...input.issueRecord,
        sourceBranchName: input.issueRecord.sourceBranchName ?? input.sourceBranchName,
        workflowState: { ...input.workflowState },
        labelNames: input.issueRecord.labelNames ?? [],
    };
}
