import "../../../constants/error-messages.js";
import { getEnv } from "../../../util/env.js";
import { logger } from "../../../logger/index.js";
import { deleteLocalFile, readLocalFile, writeLocalFile } from "../../../util/fs/index.js";
import { exec } from "../../../util/exec/index.js";
import { getRepoStatus } from "../../../util/git/index.js";
import { extractPackageFileFlags } from "../pip_requirements/common.js";
import { extractHeaderCommand, extractPythonVersion, getExecOptions, getRegistryCredVarsFromPackageFiles, matchManager } from "./common.js";
import { inferCommandExecDir } from "./utils.js";
import { quote } from "shlex";
import upath from "upath";
//#region lib/modules/manager/pip-compile/artifacts.ts
function haveCredentialsInPipEnvironmentVariables() {
	const env = getEnv();
	if (env.PIP_INDEX_URL) try {
		const indexUrl = new URL(env.PIP_INDEX_URL);
		if (!!indexUrl.username || !!indexUrl.password) return true;
	} catch {
		return true;
	}
	try {
		if (env.PIP_EXTRA_INDEX_URL) return env.PIP_EXTRA_INDEX_URL.split(" ").map((urlString) => new URL(urlString)).some((url) => !!url.username || !!url.password);
	} catch {
		return true;
	}
	return false;
}
function constructPipCompileCmd(compileArgs, upgradePackages = []) {
	if (compileArgs.commandType === "custom") throw new Error("Detected custom command, header modified or set by CUSTOM_COMPILE_COMMAND");
	if (!compileArgs.outputFile) logger.debug(`pip-compile: implicit output file`);
	if (compileArgs.commandType === "pip-compile" && !compileArgs.noEmitIndexUrl && !compileArgs.emitIndexUrl && haveCredentialsInPipEnvironmentVariables()) compileArgs.argv.splice(1, 0, "--no-emit-index-url");
	for (const dep of upgradePackages) compileArgs.argv.push(`--upgrade-package=${quote(dep.depName + "==" + dep.newVersion)}`);
	return compileArgs.argv.map(quote).join(" ");
}
async function updateArtifacts({ packageFileName: inputFileName, newPackageFileContent: newInputContent, updatedDeps, config }) {
	if (!config.lockFiles) {
		logger.warn({ packageFileName: inputFileName }, "pip-compile: No lock files associated with a package file");
		return null;
	}
	logger.debug(`pipCompile.updateArtifacts(${inputFileName}->${JSON.stringify(config.lockFiles)})`);
	const result = [];
	for (const outputFileName of config.lockFiles) {
		const existingOutput = await readLocalFile(outputFileName, "utf8");
		if (!existingOutput) {
			logger.debug("pip-compile: No output file found");
			return null;
		}
		try {
			await writeLocalFile(inputFileName, newInputContent);
			if (config.isLockFileMaintenance) await deleteLocalFile(outputFileName);
			const compileArgs = extractHeaderCommand(existingOutput, outputFileName);
			let pythonVersion;
			if (compileArgs.commandType === "uv") pythonVersion = compileArgs.pythonVersion;
			else pythonVersion = extractPythonVersion(existingOutput, outputFileName);
			const cwd = inferCommandExecDir(outputFileName, compileArgs.outputFile);
			const upgradePackages = updatedDeps.filter((dep) => dep.isLockfileUpdate);
			const packageFiles = [];
			for (const name of compileArgs.sourceFiles) if (matchManager(name) === "pip_requirements") {
				const content = await readLocalFile(upath.join(cwd, name), "utf8");
				if (content) {
					const packageFile = extractPackageFileFlags(content);
					if (packageFile) packageFiles.push(packageFile);
				}
			}
			const cmd = constructPipCompileCmd(compileArgs, upgradePackages);
			const execOptions = await getExecOptions(config, compileArgs.commandType, cwd, getRegistryCredVarsFromPackageFiles(packageFiles), pythonVersion);
			logger.trace({
				cwd,
				cmd
			}, "pip-compile command");
			logger.trace({ env: execOptions.extraEnv }, "pip-compile extra env vars");
			await exec(cmd, execOptions);
			if ((await getRepoStatus())?.modified.includes(outputFileName)) result.push({ file: {
				type: "addition",
				path: outputFileName,
				contents: await readLocalFile(outputFileName, "utf8")
			} });
		} catch (err) {
			// istanbul ignore if
			if (err.message === "temporary-error") throw err;
			logger.debug({ err }, "pip-compile: Failed to run command");
			result.push({ artifactError: {
				fileName: outputFileName,
				stderr: err.message
			} });
		}
	}
	logger.debug("pip-compile: Returning updated output file(s)");
	return result.length === 0 ? null : result;
}
//#endregion
export { updateArtifacts };

//# sourceMappingURL=artifacts.js.map