"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.runBranchSummary = exports.runRenovateRepoStats = void 0;
const logger_1 = require("../../../logger");
const array_1 = require("../../../util/array");
const repository_1 = require("../../../util/cache/repository");
function runRenovateRepoStats(config, prList) {
    const prStats = { total: 0, open: 0, closed: 0, merged: 0 };
    for (const pr of prList) {
        if (pr.title === 'Configure Renovate' ||
            pr.title === config.onboardingPrTitle) {
            continue;
        }
        prStats.total += 1;
        switch (pr.state) {
            case 'merged':
                prStats.merged += 1;
                break;
            case 'closed':
                prStats.closed += 1;
                break;
            case 'open':
                prStats.open += 1;
                break;
            default:
                break;
        }
    }
    logger_1.logger.debug({ stats: prStats }, `Renovate repository PR statistics`);
}
exports.runRenovateRepoStats = runRenovateRepoStats;
function branchCacheToMetadata({ automerge, baseBranch, baseBranchSha, branchName, isModified, pristine: isPristine, sha: branchSha, }) {
    return {
        automerge,
        baseBranch,
        baseBranchSha,
        branchName,
        branchSha,
        isModified,
        isPristine,
    };
}
function filterDependencyLookupData(branches) {
    const branchesFiltered = [];
    for (const branch of branches) {
        const upgradesFiltered = [];
        const { branchName, prTitle, upgrades } = branch;
        for (const upgrade of (0, array_1.coerceArray)(upgrades)) {
            const { datasource, depName, fixedVersion, currentVersion, currentValue, newValue, newVersion, packageFile, updateType, packageName, } = upgrade;
            const filteredUpgrade = {
                datasource,
                depName,
                fixedVersion,
                currentVersion,
                currentValue,
                newValue,
                newVersion,
                packageFile,
                updateType,
                packageName,
            };
            upgradesFiltered.push(filteredUpgrade);
        }
        const filteredBranch = {
            branchName,
            prTitle,
            result: 'no-work',
            upgrades: upgradesFiltered,
        };
        branchesFiltered.push(filteredBranch);
    }
    return branchesFiltered;
}
function filterDependencyDashboardData(branches) {
    const branchesFiltered = [];
    for (const branch of branches) {
        const upgradesFiltered = [];
        const { branchName, prNo, prTitle, result, upgrades, prBlockedBy } = branch;
        for (const upgrade of (0, array_1.coerceArray)(upgrades)) {
            const { datasource, depName, displayPending, fixedVersion, currentVersion, currentValue, newValue, newVersion, packageFile, updateType, packageName, } = upgrade;
            const filteredUpgrade = {
                datasource,
                depName,
                displayPending,
                fixedVersion,
                currentVersion,
                currentValue,
                newValue,
                newVersion,
                packageFile,
                updateType,
                packageName,
            };
            upgradesFiltered.push(filteredUpgrade);
        }
        const filteredBranch = {
            branchName,
            prNo,
            prTitle,
            result,
            prBlockedBy,
            upgrades: upgradesFiltered,
        };
        branchesFiltered.push(filteredBranch);
    }
    return branchesFiltered;
}
function runBranchSummary(config, lookupBranchConfig) {
    const defaultBranch = config.defaultBranch;
    const { scan, branches } = (0, repository_1.getCache)();
    const baseMetadata = [];
    for (const [branchName, cached] of Object.entries(scan ?? {})) {
        baseMetadata.push({ branchName, sha: cached.sha });
    }
    const branchMetadata = [];
    const inactiveBranches = [];
    for (const branch of branches ?? []) {
        if (branch.sha) {
            branchMetadata.push(branchCacheToMetadata(branch));
        }
        else {
            inactiveBranches.push(branch.branchName);
        }
    }
    const res = {
        cacheModified: (0, repository_1.isCacheModified)(),
        baseBranches: baseMetadata,
        branches: branchMetadata,
        defaultBranch,
        inactiveBranches,
    };
    logger_1.logger.debug(res, 'Branch summary');
    let branchesInformation;
    if (branches?.length) {
        branchesInformation = filterDependencyDashboardData(branches);
    }
    else if (lookupBranchConfig?.length) {
        branchesInformation = filterDependencyLookupData(lookupBranchConfig);
    }
    if (branchesInformation) {
        logger_1.logger.debug({ branchesInformation }, 'branches info extended');
    }
}
exports.runBranchSummary = runBranchSummary;
//# sourceMappingURL=repository-statistics.js.map