import { logger } from "../../../logger/index.js";
import { getCache, isCacheModified } from "../../../util/cache/repository/index.js";
import { addBranchStats } from "../../../instrumentation/reporting.js";

//#region lib/workers/repository/finalize/repository-statistics.ts
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.debug({ stats: prStats }, `Renovate repository PR statistics`);
}
function branchCacheToMetadata({ automerge, baseBranch, baseBranchSha, branchName, isModified, pristine: isPristine, sha: branchSha }) {
	return {
		automerge,
		baseBranch,
		baseBranchSha,
		branchName,
		branchSha,
		isModified,
		isPristine
	};
}
function filterDependencyDashboardData(branches) {
	const branchesFiltered = [];
	for (const branch of branches) {
		const upgradesFiltered = [];
		const { branchName, prNo, prTitle, result, upgrades, prBlockedBy } = branch;
		for (const upgrade of upgrades ?? []) {
			const { datasource, depName, displayPending, fixedVersion, currentVersion, currentValue, currentDigest, newValue, newVersion, newDigest, packageFile, updateType, packageName } = upgrade;
			const filteredUpgrade = {
				datasource,
				depName,
				displayPending,
				fixedVersion,
				currentVersion,
				currentValue,
				currentDigest,
				newValue,
				newVersion,
				newDigest,
				packageFile,
				updateType,
				packageName
			};
			upgradesFiltered.push(filteredUpgrade);
		}
		const filteredBranch = {
			branchName,
			prNo,
			prTitle,
			result,
			prBlockedBy,
			upgrades: upgradesFiltered
		};
		branchesFiltered.push(filteredBranch);
	}
	return branchesFiltered;
}
function runBranchSummary(config) {
	const defaultBranch = config.defaultBranch;
	const { scan, branches } = 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: isCacheModified(),
		baseBranches: baseMetadata,
		branches: branchMetadata,
		defaultBranch,
		inactiveBranches
	};
	logger.debug(res, "Branch summary");
	if (branches?.length) {
		const branchesInformation = filterDependencyDashboardData(branches);
		addBranchStats(config, branchesInformation);
		logger.debug({ branchesInformation }, "branches info extended");
	}
}

//#endregion
export { runBranchSummary, runRenovateRepoStats };
//# sourceMappingURL=repository-statistics.js.map