import { GlobalConfig } from "../../../../config/global.js";
import { coerceString } from "../../../../util/string.js";
import { logger } from "../../../../logger/index.js";
import { joinUrlParts } from "../../../../util/url.js";
import { platform } from "../../../../modules/platform/index.js";
import { isActiveConfidenceLevel } from "../../../../util/merge-confidence/index.js";
//#region lib/workers/repository/update/branch/status-checks.ts
async function resolveBranchStatus(branchName, internalChecksAsSuccess, ignoreTests = false) {
	logger.debug(`resolveBranchStatus(branchName=${branchName}, ignoreTests=${ignoreTests})`);
	if (ignoreTests) {
		logger.debug("Ignore tests. Return green");
		return "green";
	}
	const status = await platform.getBranchStatus(branchName, internalChecksAsSuccess);
	logger.debug(`Branch status ${status}`);
	return status;
}
async function setStatusCheck(branchName, context, description, state, url) {
	if (await platform.getBranchStatusCheck(branchName, context) === state) logger.debug(`Status check ${context} is already up-to-date`);
	else {
		if (GlobalConfig.get("dryRun")) {
			logger.info(`DRY-RUN: Would update ${context} status check state to ${state}`);
			return;
		}
		logger.debug(`Updating ${context} status check state to ${state}`);
		await platform.setBranchStatus({
			branchName,
			context,
			description,
			state,
			url
		});
	}
}
async function setStability(config) {
	if (!config.stabilityStatus) return;
	const context = config.statusCheckNames?.minimumReleaseAge;
	if (!context) {
		logger.debug("Status check is null or an empty string, skipping status check addition.");
		return;
	}
	const description = config.stabilityStatus === "green" ? "Updates have met minimum release age requirement" : "Updates have not met minimum release age requirement";
	const docsLink = joinUrlParts(coerceString(config.productLinks?.documentation), "key-concepts/minimum-release-age/");
	await setStatusCheck(config.branchName, context, description, config.stabilityStatus, docsLink);
}
async function setConfidence(config) {
	if (!config.branchName || !config.confidenceStatus || config.minimumConfidence && !isActiveConfidenceLevel(config.minimumConfidence)) return;
	const context = config.statusCheckNames?.mergeConfidence;
	if (!context) {
		logger.debug("Status check is null or an empty string, skipping status check addition.");
		return;
	}
	const description = config.confidenceStatus === "green" ? "Updates have met Merge Confidence requirement" : "Updates have not met Merge Confidence requirement";
	const docsLink = joinUrlParts(coerceString(config.productLinks?.documentation), "merge-confidence");
	await setStatusCheck(config.branchName, context, description, config.confidenceStatus, docsLink);
}
//#endregion
export { resolveBranchStatus, setConfidence, setStability };

//# sourceMappingURL=status-checks.js.map