const require_errors = require('./errors-BtJEw2Z-.cjs');

//#region src/internal/issue-helpers.ts
/**
* Extracts the Sentry issue ID and organization slug from a full URL
*
* @param url - A full Sentry issue URL
* @returns Object containing the numeric issue ID and organization slug (if found)
* @throws Error if the input is invalid
*/
function extractIssueId(url) {
	if (!url || typeof url !== "string") throw new require_errors.UserInputError("Invalid Sentry issue URL. URL must be a non-empty string.");
	if (!url.startsWith("http://") && !url.startsWith("https://")) throw new require_errors.UserInputError("Invalid Sentry issue URL. Must start with http:// or https://");
	let parsedUrl;
	try {
		parsedUrl = new URL(url);
	} catch (error) {
		throw new require_errors.UserInputError(`Invalid Sentry issue URL. Unable to parse URL: ${url}`);
	}
	const pathParts = parsedUrl.pathname.split("/").filter(Boolean);
	if (pathParts.length < 2 || !pathParts.includes("issues")) throw new require_errors.UserInputError("Invalid Sentry issue URL. Path must contain '/issues/{issue_id}'");
	const issueId = pathParts[pathParts.indexOf("issues") + 1];
	if (!issueId) throw new require_errors.UserInputError("Unable to determine issue ID from URL.");
	let organizationSlug;
	if (pathParts.includes("organizations")) organizationSlug = pathParts[pathParts.indexOf("organizations") + 1];
	else if (pathParts.length > 1 && pathParts[0] !== "issues") organizationSlug = pathParts[0];
	else {
		const hostParts = parsedUrl.hostname.split(".");
		if (hostParts.length > 2 && hostParts[0] !== "www") organizationSlug = hostParts[0];
	}
	if (!organizationSlug) throw new require_errors.UserInputError("Invalid Sentry issue URL. Could not determine organization.");
	return {
		issueId,
		organizationSlug
	};
}
/**
* Sometimes the LLM will pass in a funky issue shortId. For example it might pass
* in "CLOUDFLARE-MCP-41." instead of "CLOUDFLARE-MCP-41". This function attempts to
* fix common issues.
*
* @param issueId - The issue ID to parse
* @returns The parsed issue ID
*/
function parseIssueId(issueId) {
	if (!issueId.trim()) throw new require_errors.UserInputError("Issue ID cannot be empty");
	let finalIssueId = issueId;
	finalIssueId = finalIssueId.replace(/[^\w-]/g, "");
	if (!finalIssueId) throw new require_errors.UserInputError("Issue ID cannot be empty after removing special characters");
	const validFormatRegex = /^(\d+|[A-Za-z0-9][\w-]*-[A-Za-z0-9]+)$/;
	if (!validFormatRegex.test(finalIssueId)) throw new require_errors.UserInputError(`Invalid issue ID format: "${finalIssueId}". Expected either a numeric ID or a project code followed by an alphanumeric identifier (e.g., "PROJECT-ABC123").`);
	return finalIssueId;
}
/**
* Parses issue parameters from a variety of formats.
*
* @param params - Object containing issue URL, issue ID, and organization slug
* @returns Object containing the parsed organization slug and issue ID
* @throws Error if the input is invalid
*/
function parseIssueParams({ issueUrl, issueId, organizationSlug }) {
	if (issueUrl) {
		const resolved = extractIssueId(issueUrl);
		if (!resolved) throw new Error("Invalid Sentry issue URL. Path should contain '/issues/{issue_id}'");
		return {
			...resolved,
			issueId: parseIssueId(resolved.issueId)
		};
	}
	if (!organizationSlug) throw new require_errors.UserInputError("Organization slug is required");
	if (issueId) return {
		organizationSlug,
		issueId: parseIssueId(issueId)
	};
	throw new require_errors.UserInputError("Either issueId or issueUrl must be provided");
}

//#endregion
Object.defineProperty(exports, 'extractIssueId', {
  enumerable: true,
  get: function () {
    return extractIssueId;
  }
});
Object.defineProperty(exports, 'parseIssueId', {
  enumerable: true,
  get: function () {
    return parseIssueId;
  }
});
Object.defineProperty(exports, 'parseIssueParams', {
  enumerable: true,
  get: function () {
    return parseIssueParams;
  }
});
//# sourceMappingURL=issue-helpers-DbPTv_qC.cjs.map