"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.publishOrContinueClaimedPass = publishOrContinueClaimedPass;
const catalog_1 = require("../../i18n/catalog");
const failure_recovery_1 = require("./failure-recovery");
const handoffs_1 = require("./handoffs");
const execution_1 = require("./publish/execution");
const run_state_helpers_1 = require("./run-state-helpers");
const transition_summary_1 = require("./transition-summary");
async function publishOrContinueClaimedPass(input) {
    const completedWorkerState = requireCompletedWorkerState(input.context, input.workerAttempt);
    try {
        await input.suspendHeartbeat();
    }
    catch {
        // no-op
    }
    try {
        const summary = await (0, execution_1.publishCompletedWorkerExecution)({
            context: input.context,
            claimedIssue: input.claimedIssue,
            logger: input.logger,
            workflowLabels: input.workflowLabels,
            workspace: input.workspace,
            cursorBefore: input.freshnessBefore.cursor,
            freshnessPass: input.freshnessPass,
            latestIssue: input.freshnessAfter.latestIssue,
            workerExecution: completedWorkerState.workerExecution,
            syncPlannedIssueDependencies: input.dependencies.syncPlannedIssueDependencies,
        });
        if (summary === null) {
            input.resumeHeartbeat();
            return {
                kind: "continue",
                failureSeed: input.workerAttempt.failureSeed,
                continuationContext: (0, run_state_helpers_1.buildContinuationContext)(completedWorkerState.workerExecution, (0, run_state_helpers_1.resolvePublishSideRerunReason)(completedWorkerState.workerExecution)),
            };
        }
        return {
            kind: "complete",
            failureSeed: input.workerAttempt.failureSeed,
            continuationContext: null,
            summary,
        };
    }
    catch (error) {
        if ((0, run_state_helpers_1.isWorkspaceSyncBlockedError)(error)) {
            return {
                kind: "complete",
                failureSeed: input.workerAttempt.failureSeed,
                continuationContext: null,
                summary: await (0, handoffs_1.handOffWorkspaceSyncBlockedIssueToHuman)({
                    context: input.context,
                    claimedIssue: input.claimedIssue,
                    logger: input.logger,
                    failureSeed: input.workerAttempt.failureSeed,
                    blockedError: error,
                    workflowLabels: input.workflowLabels,
                    dependencies: {
                        publishTransitionSummary: transition_summary_1.publishTransitionSummary,
                    },
                }),
            };
        }
        return {
            kind: "complete",
            failureSeed: input.workerAttempt.failureSeed,
            continuationContext: null,
            summary: await (0, failure_recovery_1.handleClaimedIssueFailure)(input.context, input.claimedIssue, input.logger, input.workerAttempt.failureSeed, error, input.workflowLabels),
        };
    }
}
function requireCompletedWorkerState(context, workerAttempt) {
    if (workerAttempt.workerState !== null &&
        workerAttempt.workerState.kind === "completed") {
        return workerAttempt.workerState;
    }
    throw new Error((0, catalog_1.translate)(context.config.i18n.defaultLanguage, "run.error.workerCycleNoResult"));
}
