import { regEx } from "../../../util/regex.js";

//#region lib/modules/versioning/rust-release-channel/parse.ts
/**
* Parse a Rust toolchain string into a structured object.
*
* Supports the format: <channel>[-<date>][-<host>]
*
* @param input - The toolchain string to parse
* @returns Parsed toolchain object, or null if the input is invalid
*
* @example
* parse('stable') // { channel: 'stable' }
* parse('1.82.0') // { channel: { major: 1, minor: 82, patch: 0 } }
* parse('nightly-2025-11-24') // { channel: 'nightly', date: { year: 2025, month: 11, day: 24 } }
* parse('invalid') // null
*/
function parse(input) {
	if (!input) return null;
	const match = regEx(/^(?<channel>stable|beta|nightly|(?<major>\d+)\.(?<minor>\d+)(?:\.(?<patch>\d+))?(?:-(?<beta>beta)(?:\.(?<betaNumber>\d+))?)?)(?:-(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2}))?(?:-(?<host>.+))?$/).exec(input);
	if (!match?.groups) return null;
	const { channel: channelStr, major, minor, patch, beta, betaNumber, year, month, day, host } = match.groups;
	let channel;
	if (channelStr === "stable" || channelStr === "beta" || channelStr === "nightly") channel = channelStr;
	else {
		channel = {
			major: parseInt(major),
			minor: parseInt(minor)
		};
		if (patch) channel.patch = parseInt(patch);
		if (beta === "beta") {
			channel.prerelease = { name: "beta" };
			if (betaNumber) channel.prerelease.number = parseInt(betaNumber);
		}
	}
	const result = { channel };
	if (year && month && day) result.date = {
		year: parseInt(year),
		month: parseInt(month),
		day: parseInt(day)
	};
	if (host) result.host = host;
	return result;
}

//#endregion
export { parse };
//# sourceMappingURL=parse.js.map