import { regEx } from "../../../util/regex.js";
import upath from "upath";
//#region lib/modules/manager/mise/lockfile.ts
/**
* Parses the config file name to determine its type (local, env-specific, or default).
* Used to derive the correct lock file name and `mise lock` flags.
*/
function getConfigType(configPath) {
	const filename = upath.basename(configPath);
	const isLocal = filename.endsWith(".local.toml");
	const envMatch = regEx(/^(?:\.?mise|config)\.(?<env>[^.]+)(?:\.local)?\.toml$/).exec(filename);
	return {
		isLocal,
		env: envMatch?.groups?.env === "local" ? void 0 : envMatch?.groups?.env
	};
}
/**
* Derives the lock file path from a mise config file path.
* Matches mise's lockfile_path_for_config() logic from src/lockfile.rs
*/
function getLockFileName(configPath) {
	const dirname = upath.dirname(configPath);
	const lockDir = upath.basename(dirname) === "conf.d" ? upath.dirname(dirname) : dirname;
	const { isLocal, env } = getConfigType(configPath);
	let lockFileName;
	if (env && isLocal) lockFileName = `mise.${env}.local.lock`;
	else if (env) lockFileName = `mise.${env}.lock`;
	else if (isLocal) lockFileName = "mise.local.lock";
	else lockFileName = "mise.lock";
	return upath.join(lockDir, lockFileName);
}
/**
* Get the locked version for a dependency from the parsed lock file.
*
* Mise lock files use different key formats depending on whether a tool is in
* the mise registry:
* - Registry tools (e.g., node, python): key is the short name ("node")
* - Non-registry tools (e.g., ubi:owner/repo): key is the full name ("ubi:owner/repo")
*
* When a user specifies "core:node" in their config, mise resolves it to the
* registry short name "node" in the lock file. But "aqua:cli/cli" (not in
* registry) stays as "aqua:cli/cli" in the lock file.
*
* We use a fallback approach (Option A) rather than checking the registry (Option B):
*
* Option A (implemented): Try full depName first, then try stripped short name.
*   Pros: Simple, no registry dependency, works even if Renovate's registry
*         is out of sync with mise's registry.
*   Cons: Theoretical collision risk if both "foo" and "backend:foo" exist
*         in the same lock file (practically impossible - mise wouldn't generate this).
*
* Option B (not implemented): Check miseTooling/asdfTooling to determine if
*   the tool is in the registry, then use short name or full name accordingly.
*   Pros: Matches mise's exact logic.
*   Cons: More complex, requires passing registry data, fails if Renovate's
*         registry doesn't include a tool that mise's registry does.
*
* Option A is preferred because it's simpler, has no practical downsides, and
* doesn't couple lock file parsing to Renovate's registry coverage.
*/
function getLockedVersion(lockFileData, depName) {
	let lockedTools = lockFileData.tools[depName];
	if (!lockedTools) {
		const delimiterIndex = depName.indexOf(":");
		if (delimiterIndex !== -1) {
			const shortName = depName.substring(delimiterIndex + 1);
			lockedTools = lockFileData.tools[shortName];
		}
	}
	return lockedTools?.[0]?.version;
}
//#endregion
export { getConfigType, getLockFileName, getLockedVersion };

//# sourceMappingURL=lockfile.js.map