"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.supportsServiceConfigScan = supportsServiceConfigScan;
exports.promoteDueScheduledIssue = promoteDueScheduledIssue;
exports.scanIssueWorkflowConfiguration = scanIssueWorkflowConfiguration;
exports.promoteDueRecurringIssue = promoteDueRecurringIssue;
const types_1 = require("../../platform/workflow-state/types");
const recurring_1 = require("../../workflows/recurring");
const recurring_auto_stop_1 = require("../../workflows/recurring-auto-stop");
const scheduled_run_1 = require("../../workflows/scheduled-run");
const config_decision_1 = require("./config-decision");
const config_plan_support_1 = require("./config-plan-support");
const config_recurring_sync_support_1 = require("./config-recurring-sync-support");
const config_scan_helpers_1 = require("./config-scan-helpers");
const config_scan_plan_1 = require("./config-scan-plan");
const config_scheduled_sync_support_1 = require("./config-scheduled-sync-support");
function supportsServiceConfigScan(ticketTracker) {
    return (typeof ticketTracker.listComments === "function" &&
        typeof ticketTracker.getRepositoryOwnerKind === "function" &&
        typeof ticketTracker.getOrganizationMembership === "function");
}
async function promoteDueScheduledIssue(input) {
    return promoteDueSchedulingIssue(input, {
        decisionKind: "scheduled_sync",
        isDue: scheduled_run_1.isScheduledRunDue,
        isPromotableIssue: ({ issue }) => issue.workflowState.status === types_1.WORKFLOW_STATUSES.aiScheduled &&
            !issue.workflowState.rules.includes(types_1.WORKFLOW_RULES.aiDisabled),
        buildPlanSpec: ({ issue, workflowState, config, responseLanguage, currentNeedsHumanReason, }) => (0, config_scheduled_sync_support_1.buildScheduledRunSyncPlanSpec)({
            issue,
            workflowState,
            workflowLabels: config.ticketTracker.workflowLabels,
            responseLanguage,
            currentNeedsHumanReason,
        }),
    });
}
async function promoteDueSchedulingIssue(input, policy) {
    const responseLanguage = input.config.i18n.defaultLanguage;
    const scanState = await (0, config_scan_helpers_1.resolveWorkflowScanState)({
        ticketTracker: input.ticketTracker,
        issue: input.issue,
        responseLanguage,
    });
    if (scanState.stickyState === null) {
        return {
            promoted: false,
            plan: (0, config_scan_plan_1.buildServiceRuntimeNoopPlan)(policy.decisionKind, input.issue.id),
        };
    }
    if (!policy.isDue(scanState.stickyState)) {
        return {
            promoted: false,
            plan: (0, config_scan_plan_1.buildServiceRuntimeNoopPlan)(policy.decisionKind, input.issue.id),
        };
    }
    if (!policy.isPromotableIssue({
        issue: input.issue,
        config: input.config,
    })) {
        return {
            promoted: false,
            plan: (0, config_scan_plan_1.buildServiceRuntimeNoopPlan)(policy.decisionKind, input.issue.id),
        };
    }
    return {
        promoted: true,
        plan: (0, config_plan_support_1.materializeServiceRuntimeConfigScanPlan)({
            planSpec: policy.buildPlanSpec({
                issue: input.issue,
                workflowState: scanState.stickyState,
                config: input.config,
                responseLanguage,
                currentNeedsHumanReason: scanState.currentNeedsHumanReason,
            }),
            issueComments: scanState.comments,
            workflowLabels: input.config.ticketTracker.workflowLabels,
        }),
    };
}
async function scanIssueWorkflowConfiguration(input) {
    const responseLanguage = input.config.i18n.defaultLanguage;
    const scanState = await (0, config_scan_helpers_1.resolveWorkflowScanState)({
        ticketTracker: input.ticketTracker,
        issue: input.issue,
        responseLanguage,
    });
    return (0, config_decision_1.resolveServiceRuntimeConfigScanPlan)({
        repoRoot: input.repoRoot,
        config: input.config,
        ticketTracker: input.ticketTracker,
        issue: input.issue,
        scanState,
        enableImplicitReplan: input.enableImplicitReplan === true,
    });
}
async function promoteDueRecurringIssue(input) {
    return promoteDueSchedulingIssue(input, {
        decisionKind: "recurring_sync",
        isDue: recurring_auto_stop_1.isRecurringLifecycleTaskDue,
        isPromotableIssue: ({ issue, config }) => (0, recurring_1.isRecurringMonitoringIssueProjection)({
            issue,
            workflowLabels: config.ticketTracker.workflowLabels,
        }),
        buildPlanSpec: ({ issue, workflowState, config, responseLanguage, currentNeedsHumanReason, }) => (0, config_recurring_sync_support_1.buildRecurringSyncPlanSpec)({
            issue,
            workflowState,
            workflowLabels: config.ticketTracker.workflowLabels,
            responseLanguage,
            currentNeedsHumanReason,
        }),
    });
}
