"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveTrackerIssueEffectiveConfig = resolveTrackerIssueEffectiveConfig;
exports.isTrackerIssueAutoMergeEnabled = isTrackerIssueAutoMergeEnabled;
const control_plane_config_1 = require("../config/control-plane-config");
const config_override_validation_1 = require("../workflows/config-override-validation");
const shared_1 = require("./run-once-cycle/shared");
async function resolveTrackerIssueEffectiveConfig(input) {
    if (input.comments) {
        return resolveThreadEffectiveConfigOrBase(input.config, input.comments, {
            issueBody: input.issue.body ?? null,
        });
    }
    if (typeof input.ticketTracker.listComments !== "function") {
        return input.config;
    }
    const comments = await (0, shared_1.loadSortedIssueComments)(input.ticketTracker, input.issue.id, {
        timeline: "issue-only",
    });
    return resolveThreadEffectiveConfigOrBase(input.config, comments, {
        issueBody: input.issue.body ?? null,
    });
}
async function isTrackerIssueAutoMergeEnabled(input) {
    return (0, control_plane_config_1.isIssueCompletionAutoMergeEnabled)(await resolveTrackerIssueEffectiveConfig(input));
}
function resolveThreadEffectiveConfigOrBase(config, comments, options) {
    try {
        return (0, control_plane_config_1.resolveIssueEffectiveConfigFromComments)(config, comments, undefined, options);
    }
    catch (error) {
        if (error instanceof config_override_validation_1.UnavailableWorkerConfigOverrideError) {
            return config;
        }
        throw error;
    }
}
