"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildCommandlessPlanSpec = buildCommandlessPlanSpec;
const issue_completion_1 = require("../../config/issue-completion");
const types_1 = require("../../platform/ticket-tracker/types");
const types_2 = require("../../platform/workflow-state/types");
const publish_precondition_support_1 = require("../../workflows/publish-precondition-support");
const publisher_1 = require("../../workflows/publisher");
const publisher_events_1 = require("../../workflows/publisher-events");
const workflow_projection_support_1 = require("../../workflows/workflow-projection-support");
const workflow_timeline_scan_1 = require("../../workflows/workflow-timeline-scan");
const config_decision_approval_support_1 = require("./config-decision-approval-support");
const config_human_stop_support_1 = require("./config-human-stop-support");
const config_publish_spec_support_1 = require("./config-publish-spec-support");
const config_recurring_sync_support_1 = require("./config-recurring-sync-support");
const config_scan_helpers_1 = require("./config-scan-helpers");
async function buildCommandlessPlanSpec(context) {
    const { effectiveConfig, normalizedStickyState } = (0, config_decision_approval_support_1.resolveServiceRuntimeStartApprovalNormalization)({
        config: context.config,
        issue: context.issue,
        stickyState: context.stickyState,
    });
    const normalizedContext = {
        ...context,
        stickyState: normalizedStickyState,
    };
    const decision = await (0, config_scan_helpers_1.resolveCommandlessWorkflowScanDecision)({
        ticketTracker: context.ticketTracker,
        config: effectiveConfig,
        comments: context.comments,
        scan: context.scan,
        issue: context.issue,
        stickyState: context.stickyState,
        currentAssignee: context.issue.assignees.length === 1
            ? (context.issue.assignees[0] ?? null)
            : null,
        currentNeedsHumanReason: context.currentNeedsHumanReason,
        responseLanguage: context.responseLanguage,
        enableImplicitReplan: context.enableImplicitReplan,
    });
    switch (decision.kind) {
        case "assignee_resolution_required":
            return (0, config_human_stop_support_1.buildAssigneeResolutionPlanSpec)({
                config: normalizedContext.config,
                issue: normalizedContext.issue,
                stickyState: normalizedContext.stickyState,
                currentNeedsHumanReason: normalizedContext.currentNeedsHumanReason,
                workflowLabels: normalizedContext.workflowLabels,
                responseLanguage: normalizedContext.responseLanguage,
                currentWorkflowStateCommentId: normalizedContext.currentWorkflowStateCommentId,
                scannedInputCursor: normalizedContext.scannedInputCursor,
                resolution: decision.resolution,
            });
        case "assignee_resolution_released_to_start_approval":
            return (0, config_human_stop_support_1.buildStartApprovalWaitPlanSpec)({
                config: normalizedContext.config,
                issue: normalizedContext.issue,
                stickyState: normalizedContext.stickyState,
                currentNeedsHumanReason: normalizedContext.currentNeedsHumanReason,
                workflowLabels: normalizedContext.workflowLabels,
                responseLanguage: normalizedContext.responseLanguage,
                scannedInputCursor: normalizedContext.scannedInputCursor,
            });
        case "assignee_resolution_released_to_ready":
            return buildNeedsHumanReleasedToReadyPlanSpec(normalizedContext);
        case "foreign_approval_required":
            return (0, config_human_stop_support_1.buildForeignApprovalPlanSpec)({
                config: normalizedContext.config,
                issue: normalizedContext.issue,
                stickyState: normalizedContext.stickyState,
                currentNeedsHumanReason: normalizedContext.currentNeedsHumanReason,
                workflowLabels: normalizedContext.workflowLabels,
                responseLanguage: normalizedContext.responseLanguage,
                currentWorkflowStateCommentId: normalizedContext.currentWorkflowStateCommentId,
                scannedInputCursor: normalizedContext.scannedInputCursor,
                triggerComment: decision.comment,
            });
        case "revise_plan_feedback":
            return buildRevisePlanSpec(normalizedContext, decision.comment);
        case "resume_from_human_input":
            return buildResumeFromHumanInputPlanSpec(normalizedContext);
        case "implicit_replan":
            return buildImplicitReplanPlanSpec(normalizedContext, decision.comment, decision.reason);
        case "recurring_projection_sync":
            if (shouldReleaseReviewReadyAfterIssueCompletionChange({
                context: normalizedContext,
                effectiveConfig,
            })) {
                return buildNeedsHumanReleasedToReadyPlanSpec(normalizedContext);
            }
            return (0, config_recurring_sync_support_1.buildRecurringSyncPlanSpec)({
                issue: normalizedContext.issue,
                workflowState: normalizedContext.stickyState,
                workflowLabels: normalizedContext.workflowLabels,
                responseLanguage: normalizedContext.responseLanguage,
                currentNeedsHumanReason: normalizedContext.currentNeedsHumanReason,
            });
    }
}
function shouldReleaseReviewReadyAfterIssueCompletionChange(input) {
    if (input.context.stickyState === null ||
        input.context.issue.workflowState.status !==
            types_2.WORKFLOW_STATUSES.aiNeedsHuman ||
        input.context.issue.workflowState.rules.includes(types_2.WORKFLOW_RULES.aiDisabled) ||
        input.context.currentNeedsHumanReason !==
            publisher_events_1.WORKFLOW_NEEDS_HUMAN_REASONS.reviewReady) {
        return false;
    }
    if (input.context.issue.kind === types_1.TRACKER_ITEM_KINDS.issue) {
        return input.effectiveConfig.issueCompletion.target !== "pause_on_issue";
    }
    if (input.context.issue.kind === types_1.TRACKER_ITEM_KINDS.pullRequest) {
        return (0, issue_completion_1.shouldReleaseReviewReadyPullRequest)(input.effectiveConfig.issueCompletion);
    }
    return false;
}
function buildNeedsHumanReleasedToReadyPlanSpec(context) {
    const nextWorkflowState = context.stickyState === null
        ? null
        : {
            ...context.stickyState,
            needsHumanReason: null,
        };
    const nextSnapshot = (0, workflow_projection_support_1.withWorkflowStatus)(context.issue.workflowState, types_2.WORKFLOW_STATUSES.aiReady);
    return (0, config_publish_spec_support_1.buildServiceRuntimeProjectedPublishSpec)({
        decisionKind: "commandless",
        issue: context.issue,
        responseLanguage: context.responseLanguage,
        workflowState: nextWorkflowState,
        scannedInputCursor: null,
        commentCursorPolicyMode: publisher_1.WORKFLOW_COMMENT_CURSOR_POLICY_MODES.preserve,
        workflowLabels: context.workflowLabels,
        nextWorkflowSnapshot: nextSnapshot,
        previousWorkflowState: context.stickyState,
        nextWorkflowState: nextWorkflowState,
        previousNeedsHumanReason: context.currentNeedsHumanReason,
        nextNeedsHumanReason: null,
    });
}
function buildRevisePlanSpec(context, comment) {
    const nextSnapshot = (0, workflow_projection_support_1.withWorkflowStatus)(context.issue.workflowState, types_2.WORKFLOW_STATUSES.aiReady);
    return {
        kind: "same_issue_replan",
        decisionKind: "commandless",
        responseLanguage: context.responseLanguage,
        intent: {
            kind: "revise_plan",
            issue: context.issue,
            previousWorkflowState: context.stickyState,
            publishPrecondition: (0, publish_precondition_support_1.buildWorkflowPublishPrecondition)({
                currentWorkflowStateCommentId: context.currentWorkflowStateCommentId,
                workflowState: context.stickyState,
            }),
            previousProjection: context.issue.workflowState,
            nextProjection: nextSnapshot,
            previousNeedsHumanReason: publisher_events_1.WORKFLOW_NEEDS_HUMAN_REASONS.startApproval,
            nextNeedsHumanReason: null,
            reason: comment.body,
            triggerCursor: (0, workflow_timeline_scan_1.toWorkflowCommentCursor)(comment),
        },
    };
}
function buildResumeFromHumanInputPlanSpec(context) {
    const nextSnapshot = (0, workflow_projection_support_1.withWorkflowStatus)(context.issue.workflowState, types_2.WORKFLOW_STATUSES.aiRunning);
    return (0, config_publish_spec_support_1.buildServiceRuntimeProjectedPublishSpec)({
        decisionKind: "commandless",
        issue: context.issue,
        responseLanguage: context.responseLanguage,
        workflowState: context.stickyState,
        scannedInputCursor: context.scannedInputCursor,
        commentCursorPolicyMode: publisher_1.WORKFLOW_COMMENT_CURSOR_POLICY_MODES.advanceToScannedInput,
        workflowLabels: context.workflowLabels,
        nextWorkflowSnapshot: nextSnapshot,
        previousWorkflowState: context.stickyState,
        nextWorkflowState: context.stickyState,
        previousNeedsHumanReason: context.currentNeedsHumanReason,
        nextNeedsHumanReason: null,
    });
}
function buildImplicitReplanPlanSpec(context, comment, reason) {
    const nextSnapshot = (0, workflow_projection_support_1.withWorkflowStatus)(context.issue.workflowState, types_2.WORKFLOW_STATUSES.aiReady);
    return {
        kind: "same_issue_replan",
        decisionKind: "commandless",
        responseLanguage: context.responseLanguage,
        intent: {
            kind: "service_implicit_replan",
            issue: context.issue,
            previousWorkflowState: context.stickyState,
            publishPrecondition: (0, publish_precondition_support_1.buildWorkflowPublishPrecondition)({
                currentWorkflowStateCommentId: context.currentWorkflowStateCommentId,
                workflowState: context.stickyState,
            }),
            previousProjection: context.issue.workflowState,
            nextProjection: nextSnapshot,
            previousNeedsHumanReason: context.currentNeedsHumanReason,
            nextNeedsHumanReason: null,
            reason,
            triggerCursor: (0, workflow_timeline_scan_1.toWorkflowCommentCursor)(comment),
        },
    };
}
