import { regEx } from "../../../util/regex.js";
import { logger } from "../../../logger/index.js";
import { createAquaToolConfig, createCargoToolConfig, createDotnetToolConfig, createGemToolConfig, createGithubToolConfig, createGoToolConfig, createNpmToolConfig, createPipxToolConfig, createSpmToolConfig, createUbiToolConfig } from "./backends.js";
import { asdfTooling, miseTooling } from "./upgradeable-tooling.js";
import { parseTomlFile } from "./utils.js";
import { isArray, isFunction, isNonEmptyObject, isNonEmptyString, isObject, isString } from "@sindresorhus/is";
//#region lib/modules/manager/mise/extract.ts
const optionInToolNameRegex = regEx(/^(?<name>.+?)(?:\[(?<options>.+)\])?$/);
function extractPackageFile(content, packageFile) {
	logger.trace(`mise.extractPackageFile(${packageFile})`);
	const misefile = parseTomlFile(content, packageFile);
	if (!misefile) return null;
	const deps = [];
	const tools = misefile.tools;
	if (tools) for (const [name, toolData] of Object.entries(tools)) {
		const version = parseVersion(toolData);
		const { name: depName, options: optionsInName } = optionInToolNameRegex.exec(name.trim()).groups;
		const delimiterIndex = name.indexOf(":");
		const backend = depName.substring(0, delimiterIndex);
		const toolName = depName.substring(delimiterIndex + 1);
		const options = parseOptions(optionsInName, isNonEmptyObject(toolData) ? toolData : {});
		const dep = createDependency(depName, version, version === null ? null : getToolConfig(backend, toolName, version, options));
		deps.push(dep);
	}
	return deps.length ? { deps } : null;
}
function parseVersion(toolData) {
	if (isNonEmptyString(toolData)) return toolData;
	if (isArray(toolData, isString)) return toolData.length ? toolData[0] : null;
	if (isObject(toolData) && isNonEmptyString(toolData.version)) return toolData.version;
	return null;
}
function parseOptions(optionsInName, toolOptions) {
	return {
		...isNonEmptyString(optionsInName) ? Object.fromEntries(optionsInName.split(",").map((option) => option.split("=", 2))) : {},
		...toolOptions
	};
}
function getToolConfig(backend, toolName, version, toolOptions) {
	switch (backend) {
		case "": return getRegistryToolConfig(toolName, version);
		case "core": return getConfigFromTooling(miseTooling, toolName, version);
		case "asdf": return getConfigFromTooling(asdfTooling, toolName, version);
		case "vfox": return getRegistryToolConfig(toolName, version);
		case "aqua": return getRegistryToolConfig(toolName, version) ?? createAquaToolConfig(toolName, version);
		case "cargo": return createCargoToolConfig(toolName, version);
		case "dotnet": return createDotnetToolConfig(toolName);
		case "gem": return createGemToolConfig(toolName);
		case "github": return createGithubToolConfig(toolName, version, toolOptions);
		case "go": return createGoToolConfig(toolName);
		case "npm": return createNpmToolConfig(toolName);
		case "pipx": return createPipxToolConfig(toolName);
		case "spm": return createSpmToolConfig(toolName);
		case "ubi": return createUbiToolConfig(toolName, version, toolOptions);
		default: return null;
	}
}
/**
* Get the tooling config for a short name defined in the default registry
* @link https://mise.jdx.dev/registry.html
*/
function getRegistryToolConfig(short, version) {
	return getConfigFromTooling(miseTooling, short, version) ?? getConfigFromTooling(asdfTooling, short, version);
}
function getConfigFromTooling(toolingSource, name, version) {
	const toolDefinition = toolingSource[name];
	if (!toolDefinition) return null;
	return (isFunction(toolDefinition.config) ? toolDefinition.config(version) : toolDefinition.config) ?? null;
}
function createDependency(name, version, config) {
	if (version === null) return {
		depName: name,
		skipReason: "unspecified-version"
	};
	if (config === null) return {
		depName: name,
		skipReason: "unsupported-datasource"
	};
	return {
		depName: name,
		currentValue: version,
		...config
	};
}
//#endregion
export { extractPackageFile };

//# sourceMappingURL=extract.js.map