"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.loadPullRequestDetails = loadPullRequestDetails;
const issue_show_options_1 = require("./issue-show-options");
async function loadPullRequestDetails(input) {
    if (input.pr === null || input.options.prMode === issue_show_options_1.ISSUE_SHOW_PR_MODES.none) {
        return null;
    }
    const shouldLoadFiles = input.options.prMode === issue_show_options_1.ISSUE_SHOW_PR_MODES.changes ||
        input.options.prMode === issue_show_options_1.ISSUE_SHOW_PR_MODES.all;
    const shouldLoadCommits = input.options.prMode === issue_show_options_1.ISSUE_SHOW_PR_MODES.commits ||
        input.options.prMode === issue_show_options_1.ISSUE_SHOW_PR_MODES.all;
    const [automationState, files, commits] = await Promise.all([
        loadPullRequestAutomationState(input.repositoryHost, input.pr.number),
        shouldLoadFiles
            ? loadPullRequestChangedFiles(input.repositoryHost, input.pr.number)
            : Promise.resolve([]),
        shouldLoadCommits
            ? loadPullRequestCommits(input.repositoryHost, input.pr.number)
            : Promise.resolve([]),
    ]);
    return {
        automationState,
        files: applyRelatedLimit(files, input.options.relatedLimit),
        commits: applyRelatedLimit(commits, input.options.relatedLimit),
    };
}
async function loadPullRequestAutomationState(repositoryHost, pullRequestNumber) {
    try {
        return await repositoryHost.getPullRequestAutomationState(pullRequestNumber);
    }
    catch {
        return null;
    }
}
async function loadPullRequestChangedFiles(repositoryHost, pullRequestNumber) {
    try {
        return await repositoryHost.listPullRequestChangedFiles(pullRequestNumber);
    }
    catch {
        return [];
    }
}
async function loadPullRequestCommits(repositoryHost, pullRequestNumber) {
    try {
        return await repositoryHost.listPullRequestCommits(pullRequestNumber);
    }
    catch {
        return [];
    }
}
function applyRelatedLimit(items, limit) {
    return limit === null ? [...items] : [...items].slice(0, limit);
}
