import { logger } from "../../../logger/index.js";
import "../../../util/streams.js";
import { coreApi, policyApi } from "./azure-got-wrapper.js";
import "../util.js";
import "./schema.js";
import "./util.js";
import { GitPullRequestMergeStrategy } from "azure-devops-node-api/interfaces/GitInterfaces.js";

//#region lib/modules/platform/azure/azure-helper.ts
const mergePolicyGuid = "fa4e907d-c16b-4a4c-9dfa-4916e5d171ab";
async function getMergeMethod(repoId, project, branchRef, defaultBranch) {
	logger.debug(`getMergeMethod(branchRef=${branchRef}, defaultBranch=${defaultBranch})`);
	const isRelevantScope = (scope) => {
		if (scope.matchKind === "DefaultBranch" && (!branchRef || branchRef === `refs/heads/${defaultBranch}`)) return true;
		if (scope.repositoryId !== repoId && scope.repositoryId !== null) return false;
		if (!branchRef) return true;
		return scope.matchKind === "Exact" ? scope.refName === branchRef : branchRef.startsWith(scope.refName);
	};
	const policyConfigurations = (await (await policyApi()).getPolicyConfigurations(project, void 0, mergePolicyGuid)).filter((p) => p.settings.scope.some(isRelevantScope)).map((p) => p.settings)[0];
	logger.debug(`getMergeMethod(branchRef=${branchRef}) determining mergeMethod from matched policy:\n${JSON.stringify(policyConfigurations, null, 4)}`);
	try {
		return Object.keys(policyConfigurations).map((p) => GitPullRequestMergeStrategy[p.slice(5)]).find((p) => p);
	} catch {
		return GitPullRequestMergeStrategy.NoFastForward;
	}
}
async function getAllProjectTeams(projectId) {
	const allTeams = [];
	const azureApiCore = await coreApi();
	const top = 100;
	let skip = 0;
	let length = 0;
	do {
		const teams = await azureApiCore.getTeams(projectId, void 0, top, skip);
		length = teams.length;
		allTeams.push(...teams);
		skip += top;
	} while (top <= length);
	return allTeams;
}

//#endregion
export { getAllProjectTeams, getMergeMethod };
//# sourceMappingURL=azure-helper.js.map