/**
 * Library ID parsing utilities.
 *
 * Parses the `libraryId` query parameter used by the context7-compatible API.
 * Supports two formats:
 *   - /owner/repo           (default branch)
 *   - /owner/repo/version   (specific version tag)
 */

export interface ParsedLibraryId {
	/** The canonical repository ID, e.g. "/facebook/react" */
	repositoryId: string;
	/** The version tag, e.g. "v18.3.0" — absent for default branch queries */
	version?: string;
}

/**
 * Parse a libraryId string into its constituent parts.
 *
 * @throws Error when the string does not match the expected pattern.
 */
export function parseLibraryId(libraryId: string): ParsedLibraryId {
	const match = libraryId.match(/^(\/[^/]+\/[^/]+)(\/(.+))?$/);
	if (!match) {
		throw new Error(`Invalid libraryId: ${libraryId}`);
	}

	return {
		repositoryId: match[1],
		version: match[3]
	};
}
