import { newlineRegex, regEx } from "../../../util/regex.js";
import { logger } from "../../../logger/index.js";
import { getChildEnv } from "../../../util/exec/utils.js";
import { withCache } from "../../../util/cache/package/with-cache.js";
import { Datasource } from "../datasource.js";
import { getRemoteUrlWithToken } from "../../../util/git/url.js";
import { getGitEnvironmentVariables } from "../../../util/git/auth.js";
import { simpleGitConfig } from "../../../util/git/config.js";
import { isTruthy } from "@sindresorhus/is";
import { simpleGit } from "simple-git";
//#region lib/modules/datasource/git-refs/base.ts
const refMatch = regEx(/(?<hash>.*?)\s+refs\/(?<type>.*?)\/(?<value>.*)/);
const headMatch = regEx(/(?<hash>.*?)\s+HEAD/);
const gitId = "git";
var GitDatasource = class extends Datasource {
	static id = gitId;
	constructor(id) {
		super(id);
	}
	async _getRawRefs({ packageName }) {
		const gitEnv = getChildEnv({ env: getGitEnvironmentVariables([this.id]) });
		const lsRemote = await simpleGit(simpleGitConfig()).env(gitEnv).listRemote([getRemoteUrlWithToken(packageName, this.id)]);
		if (!lsRemote) return null;
		const allRefs = lsRemote.trim().split(newlineRegex).map((line) => line.trim()).map((line) => {
			let match = refMatch.exec(line);
			if (match?.groups) return {
				type: match.groups.type,
				value: match.groups.value,
				hash: match.groups.hash
			};
			match = headMatch.exec(line);
			if (match?.groups) return {
				type: "",
				value: "HEAD",
				hash: match.groups.hash
			};
			logger.trace(`malformed ref: ${line}`);
			return null;
		}).filter(isTruthy).filter((ref) => ref.type !== "pull");
		const dereferencedTags = {};
		for (const ref of allRefs) if (ref.value.endsWith("^{}")) dereferencedTags[ref.value.slice(0, -3)] = ref.hash;
		return allRefs.filter((ref) => !ref.value.endsWith("^{}")).map((ref) => {
			const dereferencedHash = dereferencedTags[ref.value];
			if (dereferencedHash) return {
				...ref,
				hash: dereferencedHash
			};
			return ref;
		});
	}
	getRawRefs(config) {
		return withCache({
			namespace: `datasource-${gitId}`,
			key: config.packageName
		}, () => this._getRawRefs(config));
	}
};
//#endregion
export { GitDatasource };

//# sourceMappingURL=base.js.map