"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.isTerminalSourceFinalizationWorkflowState = isTerminalSourceFinalizationWorkflowState;
exports.resolveTerminalSourceFinalizationCandidate = resolveTerminalSourceFinalizationCandidate;
exports.resolveTerminalSourcePullRequest = resolveTerminalSourcePullRequest;
exports.resolveWorkingInPrMergeFinalizationCandidate = resolveWorkingInPrMergeFinalizationCandidate;
const constants_1 = require("../../platform/constants");
const types_1 = require("../../platform/ticket-tracker/types");
const execution_ownership_1 = require("../execution-ownership");
const plan_progress_1 = require("../../workflows/plan-progress");
const recurring_1 = require("../../workflows/recurring");
const default_issue_workspace_manager_1 = require("../../workspaces/default-issue-workspace-manager");
const issue_branch_target_1 = require("./issue-branch-target");
const pull_request_completion_1 = require("./pull-request-completion");
const results_1 = require("./results");
function isTerminalSourceFinalizationWorkflowState(workflowState) {
    if (workflowState?.plan === null || workflowState === null) {
        return false;
    }
    return (workflowState.pendingPlanningIntent === undefined &&
        (workflowState.pendingChildIssueDecisions ?? []).length === 0 &&
        !(0, execution_ownership_1.isExecutionOwnershipActive)(workflowState.executionOwnership) &&
        !(0, recurring_1.isRecurringParentWorkflowState)(workflowState) &&
        (0, plan_progress_1.isCompletedWorkflowPlan)(workflowState.plan) &&
        (0, plan_progress_1.selectNextReadyWorkflowStep)(workflowState.plan) === null);
}
async function resolveTerminalSourceFinalizationCandidate(input) {
    if (input.issue.kind !== types_1.TRACKER_ITEM_KINDS.issue ||
        !input.issue.isOpen ||
        !isTerminalSourceFinalizationWorkflowState(input.workflowState)) {
        return null;
    }
    const { branchName, pullRequest } = await resolveTerminalSourcePullRequest({
        config: input.config,
        repositoryHost: input.repositoryHost,
        issue: input.issue,
        workflowState: input.workflowState,
    });
    const completion = await (0, pull_request_completion_1.resolvePullRequestCompletion)({
        repositoryHost: input.repositoryHost,
        pullRequest,
        branchName,
    });
    if (completion.status !== "completed") {
        return null;
    }
    return {
        issue: input.issue,
        workflowState: input.workflowState,
        branchName,
        pullRequest: completion.pullRequest,
        completion,
    };
}
async function resolveTerminalSourcePullRequest(input) {
    const branchName = (0, default_issue_workspace_manager_1.resolveIssueBranchName)(input.config.branchPrefix, input.issue);
    return {
        branchName,
        pullRequest: (0, results_1.filterPullRequestByBase)(await input.repositoryHost.findPullRequestByBranch(branchName), (0, issue_branch_target_1.resolveIssueRunBaseBranch)(input.workflowState)),
    };
}
async function resolveWorkingInPrMergeFinalizationCandidate(input) {
    if (input.issue.kind !== types_1.TRACKER_ITEM_KINDS.issue ||
        !input.issue.isOpen ||
        input.workflowState === null) {
        return null;
    }
    const { branchName, pullRequest } = await resolveTerminalSourcePullRequest({
        config: input.config,
        repositoryHost: input.repositoryHost,
        issue: input.issue,
        workflowState: input.workflowState,
    });
    if (pullRequest?.state !== constants_1.PLATFORM_PULL_REQUEST_STATES.merged) {
        return null;
    }
    return {
        issue: input.issue,
        workflowState: input.workflowState,
        branchName,
        pullRequest,
    };
}
