import { GlobalConfig } from "../../../config/global.js";
import { logger } from "../../../logger/index.js";
import { platform } from "../../../modules/platform/index.js";
import { ensureComment } from "../../../modules/platform/comment.js";
import { getDepWarningsOnboardingPR, getErrors, getWarnings } from "../errors-warnings.js";
import { getBaseBranchDesc } from "../onboarding/pr/base-branch.js";
import { getScheduleDesc } from "../onboarding/pr/config-description.js";
import { getPackageFilesDesc, getPackageFilesSummary } from "../onboarding/pr/package-files.js";
import { getExpectedPrList, getExpectedPrListSummary } from "../onboarding/pr/pr-list.js";
import { isArray, isString } from "@sindresorhus/is";
//#region lib/workers/repository/reconfigure/comment.ts
async function ensureReconfigurePrComment(config, packageFiles, branches, branchName, pr) {
	logger.debug("ensureReconfigurePrComment()");
	logger.trace({ config });
	let prCommentTemplate = `This is a reconfigure PR comment to help you understand and re-configure your renovate bot settings. If this Reconfigure PR were to be merged, we'd expect to see the following outcome:\n\n`;
	prCommentTemplate += `
---
{{PACKAGE FILES}}
{{CONFIG}}
{{BASEBRANCH}}
{{PRLIST}}
{{WARNINGS}}
{{ERRORS}}
`;
	let prBody = prCommentTemplate;
	const packageFilesDesc = getPackageFilesDesc(packageFiles);
	if (packageFilesDesc) prBody = `${prBody.replace("{{PACKAGE FILES}}", packageFilesDesc)}\n`;
	else prBody = prBody.replace("{{PACKAGE FILES}}\n", "");
	let configDesc = "";
	if (GlobalConfig.get("dryRun")) logger.info(`DRY-RUN: Would check branch ${branchName}`);
	else configDesc = getConfigDesc(config);
	prBody = prBody.replace("{{CONFIG}}\n", configDesc);
	prBody = prBody.replace("{{WARNINGS}}\n", getWarnings(config) + getDepWarningsOnboardingPR(packageFiles, config));
	prBody = prBody.replace("{{ERRORS}}\n", getErrors(config));
	prBody = prBody.replace("{{BASEBRANCH}}\n", getBaseBranchDesc(config));
	const prList = getExpectedPrList(config, branches);
	prBody = prBody.replace("{{PRLIST}}\n", prList);
	if (prBody.length > platform.maxBodyLength()) {
		logger.debug("Reconfigure PR body exceeds platform limit, switching to summary PR list and package files");
		prBody = prBody.replace(prList, getExpectedPrListSummary(config, branches));
		if (packageFilesDesc) prBody = prBody.replace(packageFilesDesc, `### Detected Package Files\n\n${getPackageFilesSummary(packageFiles)}`);
	}
	logger.trace(`prBody:\n${prBody}`);
	prBody = platform.massageMarkdown(prBody);
	if (GlobalConfig.get("dryRun")) {
		logger.info("DRY-RUN: Would ensure comment");
		return true;
	}
	return await ensureComment({
		number: pr.number,
		topic: "Reconfigure PR Results",
		content: prBody
	});
}
function getDescriptionArray(config) {
	logger.debug("getDescriptionArray()");
	logger.trace({ config });
	return (isArray(config.description, isString) ? config.description : []).concat(getScheduleDesc(config));
}
function getConfigDesc(config) {
	logger.debug("getConfigDesc()");
	logger.trace({ config });
	const descriptionArr = getDescriptionArray(config);
	if (!descriptionArr.length) {
		logger.debug("No config description found");
		return "";
	}
	logger.debug(`Found description array with length:${descriptionArr.length}`);
	let desc = `\n### Configuration Summary\n\nBased on the default config's presets, Renovate will:\n\n`;
	descriptionArr.forEach((d) => {
		desc += `  - ${d}\n`;
	});
	desc += "\n\n---\n";
	return desc;
}
//#endregion
export { ensureReconfigurePrComment, getConfigDesc };

//# sourceMappingURL=comment.js.map