import "../../../constants/error-messages.js";
import { logger } from "../../../logger/index.js";
import { findAll } from "../../../util/host-rules.js";
import { deleteLocalFile, readLocalFile, writeLocalFile } from "../../../util/fs/index.js";
import { exec } from "../../../util/exec/index.js";
import { processHostRules } from "../npm/post-update/rules.js";
import { getNpmrcContent, resetNpmrcContent, updateNpmrcContent } from "../npm/utils.js";
import { isEmptyArray } from "@sindresorhus/is";
import upath from "upath";
//#region lib/modules/manager/deno/artifacts.ts
async function updateArtifacts(updateArtifact) {
	const { packageFileName, updatedDeps, newPackageFileContent, config } = updateArtifact;
	logger.debug(`deno.updateArtifacts(${packageFileName})`);
	const isLockFileMaintenance = config.updateType === "lockFileMaintenance";
	if (isEmptyArray(updatedDeps) && !isLockFileMaintenance) {
		logger.debug("No updated deno deps - returning null");
		return null;
	}
	const lockFileName = updatedDeps[0]?.lockFiles?.[0] ?? config.lockFiles?.[0];
	if (!lockFileName) {
		logger.debug("No lock file found. Skipping artifact update.");
		return null;
	}
	const oldLockFileContent = await readLocalFile(lockFileName);
	if (!oldLockFileContent) {
		logger.debug(`Failed to read ${lockFileName}. Skipping artifact update.`);
		return [{ artifactError: {
			fileName: lockFileName,
			stderr: `Failed to read "${lockFileName}"`
		} }];
	}
	for (const updateDep of updatedDeps) if (updateDep.depType === "tasks" || updateDep.depType === "tasks.command") {
		logger.warn({
			depType: updateDep.depType,
			depName: updateDep.depName,
			lockFileName
		}, "Dependency can't be updated with a lock file");
		return [{ artifactError: {
			fileName: lockFileName,
			stderr: `depType: "${updateDep.depType}", depName: "${updateDep.depName}" can't be updated with a lock file: "${lockFileName}"`
		} }];
	}
	const pkgFileDir = upath.dirname(packageFileName);
	const { additionalNpmrcContent } = processHostRules();
	const npmrcContent = await getNpmrcContent(pkgFileDir);
	await updateNpmrcContent(pkgFileDir, npmrcContent, additionalNpmrcContent);
	try {
		await writeLocalFile(packageFileName, newPackageFileContent);
		if (isLockFileMaintenance) await deleteLocalFile(lockFileName);
		const execOptions = {
			cwdFile: updatedDeps.find((dep) => dep.managerData?.importMapReferrer)?.managerData?.importMapReferrer ?? packageFileName,
			docker: {},
			toolConstraints: [{
				toolName: "deno",
				constraint: config.constraints?.deno
			}]
		};
		let command = "deno install --frozen=false";
		const defaultImportHosts = [
			"deno.land:443",
			"esm.sh:443",
			"jsr.io:443",
			"cdn.jsdelivr.net:443",
			"raw.githubusercontent.com:443",
			"gist.githubusercontent.com:443"
		];
		const additionalImportHosts = findAll({ hostType: "npm" }).filter((rule) => rule.resolvedHost).map((rule) => rule.resolvedHost);
		if (additionalImportHosts.length > 0) {
			const importHosts = [.../* @__PURE__ */ new Set([...defaultImportHosts, ...additionalImportHosts])].join(",");
			command += ` --allow-import=${importHosts}`;
		}
		await exec(command, execOptions);
		await resetNpmrcContent(pkgFileDir, npmrcContent);
		const newLockFileContent = await readLocalFile(lockFileName);
		if (!newLockFileContent || Buffer.compare(oldLockFileContent, newLockFileContent) === 0) return null;
		return [{ file: {
			type: "addition",
			path: lockFileName,
			contents: newLockFileContent
		} }];
	} catch (err) {
		if (err.message === "temporary-error") throw err;
		logger.warn({
			lockfile: lockFileName,
			err
		}, `Failed to update lock file`);
		return [{ artifactError: {
			fileName: lockFileName,
			stderr: err.message
		} }];
	}
}
//#endregion
export { updateArtifacts };

//# sourceMappingURL=artifacts.js.map