import { getEnv } from "../env.js";
import { GlobalConfig } from "../../config/global.js";
import { logger } from "../../logger/index.js";
import { id } from "../../modules/versioning/pep440/index.js";
import { id as id$1 } from "../../modules/versioning/semver/index.js";
import { id as id$2 } from "../../modules/versioning/npm/index.js";
import { id as id$3 } from "../../modules/versioning/node/index.js";
import { id as id$4 } from "../../modules/versioning/ruby/index.js";
import { id as id$5 } from "../../modules/versioning/maven/index.js";
import { id as id$6 } from "../../modules/versioning/gradle/index.js";
import { id as id$7 } from "../../modules/versioning/composer/index.js";
import { id as id$8 } from "../../modules/versioning/conda/index.js";
import { id as id$9 } from "../../modules/versioning/python/index.js";
import { id as id$10 } from "../../modules/versioning/semver-coerced/index.js";
import { get } from "../../modules/versioning/index.js";
import { isString } from "@sindresorhus/is";
import { quote } from "shlex";

//#region lib/util/exec/containerbase.ts
const allToolConfig = {
	bun: {
		datasource: "github-releases",
		packageName: "oven-sh/bun",
		extractVersion: "^bun-v(?<version>.*)$",
		versioning: id$2
	},
	bundler: {
		datasource: "rubygems",
		packageName: "bundler",
		versioning: id$4
	},
	cocoapods: {
		datasource: "rubygems",
		packageName: "cocoapods",
		versioning: id$4
	},
	composer: {
		datasource: "github-releases",
		packageName: "containerbase/composer-prebuild",
		versioning: id$7
	},
	conan: {
		datasource: "pypi",
		packageName: "conan",
		versioning: id
	},
	copier: {
		datasource: "pypi",
		packageName: "copier",
		versioning: id
	},
	corepack: {
		datasource: "npm",
		packageName: "corepack",
		versioning: id$2
	},
	devbox: {
		datasource: "github-releases",
		packageName: "jetify-com/devbox",
		versioning: id$1
	},
	dotnet: {
		datasource: "dotnet-version",
		packageName: "dotnet-sdk",
		versioning: id$1
	},
	erlang: {
		datasource: "github-releases",
		packageName: "containerbase/erlang-prebuild",
		versioning: id$10
	},
	elixir: {
		datasource: "github-releases",
		packageName: "elixir-lang/elixir",
		versioning: id$1
	},
	flux: {
		datasource: "github-releases",
		packageName: "fluxcd/flux2",
		versioning: id$1
	},
	gleam: {
		datasource: "github-releases",
		packageName: "gleam-lang/gleam",
		versioning: id$1
	},
	golang: {
		datasource: "github-releases",
		packageName: "containerbase/golang-prebuild",
		versioning: id$2
	},
	gradle: {
		datasource: "gradle-version",
		packageName: "gradle",
		versioning: id$6
	},
	hashin: {
		datasource: "pypi",
		packageName: "hashin",
		versioning: id
	},
	helm: {
		datasource: "github-releases",
		packageName: "helm/helm",
		versioning: id$1
	},
	helmfile: {
		datasource: "github-releases",
		packageName: "helmfile/helmfile",
		versioning: id$1
	},
	java: {
		datasource: "java-version",
		packageName: "java?system=true",
		versioning: id$2
	},
	"java-maven": {
		datasource: "java-version",
		packageName: "java?system=true",
		versioning: id$5
	},
	jb: {
		datasource: "github-releases",
		packageName: "jsonnet-bundler/jsonnet-bundler",
		versioning: id$1
	},
	kustomize: {
		datasource: "github-releases",
		packageName: "kubernetes-sigs/kustomize",
		extractVersion: "^kustomize/v(?<version>.*)$",
		versioning: id$1
	},
	maven: {
		datasource: "github-releases",
		packageName: "containerbase/maven-prebuild",
		versioning: id$5
	},
	nix: {
		datasource: "github-releases",
		packageName: "containerbase/nix-prebuild",
		versioning: id$1
	},
	node: {
		datasource: "github-releases",
		packageName: "containerbase/node-prebuild",
		versioning: id$3
	},
	npm: {
		datasource: "npm",
		packageName: "npm",
		versioning: id$2
	},
	pdm: {
		datasource: "github-releases",
		packageName: "pdm-project/pdm",
		versioning: id$1
	},
	php: {
		datasource: "github-releases",
		packageName: "containerbase/php-prebuild",
		versioning: id$7
	},
	"pip-tools": {
		datasource: "pypi",
		packageName: "pip-tools",
		versioning: id
	},
	pipenv: {
		datasource: "pypi",
		packageName: "pipenv",
		versioning: id
	},
	pnpm: {
		datasource: "npm",
		packageName: "pnpm",
		versioning: id$2
	},
	pixi: {
		datasource: "github-releases",
		packageName: "prefix-dev/pixi",
		versioning: id$8,
		extractVersion: "^v(?<version>.*)$"
	},
	poetry: {
		datasource: "pypi",
		packageName: "poetry",
		versioning: id
	},
	python: {
		datasource: "github-releases",
		packageName: "containerbase/python-prebuild",
		versioning: id$9
	},
	ruby: {
		datasource: "github-releases",
		packageName: "containerbase/ruby-prebuild",
		versioning: id$4
	},
	rust: {
		datasource: "docker",
		packageName: "rust",
		versioning: id$1
	},
	uv: {
		datasource: "pypi",
		packageName: "uv",
		versioning: id
	},
	yarn: {
		datasource: "npm",
		packageName: "yarn",
		versioning: id$2
	},
	"yarn-slim": {
		datasource: "npm",
		packageName: "yarn",
		versioning: id$2
	},
	dart: {
		datasource: "dart-version",
		packageName: "dart",
		versioning: id$2
	},
	flutter: {
		datasource: "github-releases",
		packageName: "containerbase/flutter-prebuild",
		versioning: id$2
	},
	vendir: {
		datasource: "github-releases",
		packageName: "carvel-dev/vendir",
		versioning: id$1
	}
};
let _getPkgReleases = null;
async function getPkgReleases(toolConfig) {
	_getPkgReleases ??= import("../../modules/datasource/index.js");
	const { getPkgReleases } = await _getPkgReleases;
	return getPkgReleases(toolConfig);
}
function supportsDynamicInstall(toolName) {
	return !!allToolConfig[toolName];
}
function isContainerbase() {
	return !!getEnv().CONTAINERBASE;
}
function isDynamicInstall(toolConstraints) {
	if (GlobalConfig.get("binarySource") !== "install") return false;
	if (!isContainerbase()) {
		logger.debug("Falling back to binarySource=global");
		return false;
	}
	return !toolConstraints || toolConstraints.every((toolConstraint) => supportsDynamicInstall(toolConstraint.toolName));
}
function isStable(version, versioningApi, latest) {
	if (!versioningApi.isStable(version)) return false;
	if (isString(latest)) {
		if (versioningApi.isGreaterThan(version, latest)) return false;
	}
	return true;
}
async function resolveConstraint(toolConstraint) {
	const { toolName } = toolConstraint;
	const toolConfig = allToolConfig[toolName];
	if (!toolConfig) throw new Error(`Invalid tool to install: ${toolName}`);
	const versioning = get(toolConfig.versioning);
	let constraint = toolConstraint.constraint;
	if (constraint) if (versioning.isValid(constraint)) {
		if (versioning.isSingleVersion(constraint)) return constraint.replace(/^=+/, "").trim();
	} else {
		logger.warn({
			toolName,
			constraint,
			versioning: toolConfig.versioning
		}, "Invalid tool constraint");
		constraint = void 0;
	}
	const pkgReleases = await getPkgReleases(toolConfig);
	const releases = pkgReleases?.releases ?? [];
	if (!releases?.length) {
		logger.warn({ toolConfig }, "No tool releases found.");
		throw new Error("No tool releases found.");
	}
	const matchingReleases = releases.filter((r) => !constraint || versioning.matches(r.version, constraint));
	const stableMatchingVersion = matchingReleases.filter((r) => isStable(r.version, versioning, pkgReleases?.tags?.latest)).pop()?.version;
	if (stableMatchingVersion) {
		logger.debug({
			toolName,
			constraint,
			resolvedVersion: stableMatchingVersion
		}, "Resolved stable matching version");
		return stableMatchingVersion;
	}
	const unstableMatchingVersion = matchingReleases.pop()?.version;
	if (unstableMatchingVersion) {
		logger.debug({
			toolName,
			constraint,
			resolvedVersion: unstableMatchingVersion
		}, "Resolved unstable matching version");
		return unstableMatchingVersion;
	}
	const stableVersion = releases.filter((r) => isStable(r.version, versioning, pkgReleases?.tags?.latest)).pop()?.version;
	if (stableVersion) logger.warn({
		toolName,
		constraint,
		stableVersion
	}, "No matching tool versions found for constraint - using latest stable version");
	const highestVersion = releases.pop().version;
	logger.warn({
		toolName,
		constraint,
		highestVersion
	}, "No matching or stable tool versions found - using an unstable version");
	return highestVersion;
}
async function generateInstallCommands(toolConstraints) {
	const installCommands = [];
	if (toolConstraints?.length) for (const toolConstraint of toolConstraints) {
		const toolVersion = await resolveConstraint(toolConstraint);
		const { toolName } = toolConstraint;
		const installCommand = `install-tool ${toolName} ${quote(toolVersion)}`;
		installCommands.push(installCommand);
	}
	return installCommands;
}

//#endregion
export { generateInstallCommands, isDynamicInstall };
//# sourceMappingURL=containerbase.js.map