import { UserInputError } from "./errors-D_R568zP.js";
import { defineTool } from "./define-2Lj8Lm1t.js";
import { logError } from "./logging-DqqDTe02.js";
import { ApiNotFoundError } from "./errors-CQZ-QlOB.js";
import { apiServiceFromContext } from "./api-CkLTFexT.js";
import { ParamIssueShortId, ParamIssueUrl, ParamOrganizationSlug, ParamRegionUrl } from "./schema-rwexXWFL.js";
import { parseIssueParams } from "./issue-helpers-8Bubch8E.js";
import { formatIssueOutput } from "./formatting-DytLf-hH.js";
import { enhanceNotFoundError } from "./enhance-error-D3QfZOhm.js";
import { setTag } from "@sentry/core";
import { z } from "zod";

//#region src/tools/get-issue-details.ts
var get_issue_details_default = defineTool({
	name: "get_issue_details",
	requiredScopes: ["event:read"],
	description: [
		"Get detailed information about a specific Sentry issue by ID.",
		"",
		"🔍 USE THIS TOOL WHEN USERS:",
		"- Provide a specific issue ID (e.g., 'CLOUDFLARE-MCP-41', 'PROJECT-123')",
		"- Ask to 'explain [ISSUE-ID]', 'tell me about [ISSUE-ID]'",
		"- Want details/stacktrace/analysis for a known issue",
		"- Provide a Sentry issue URL",
		"",
		"❌ DO NOT USE for:",
		"- General searching or listing issues (use search_issues)",
		"- Root cause analysis (use analyze_issue_with_seer)",
		"",
		"TRIGGER PATTERNS:",
		"- 'Explain ISSUE-123' → use get_issue_details",
		"- 'Tell me about PROJECT-456' → use get_issue_details",
		"- 'What happened in [issue URL]' → use get_issue_details",
		"",
		"<examples>",
		"### With Sentry URL (recommended - simplest approach)",
		"```",
		"get_issue_details(issueUrl='https://sentry.sentry.io/issues/6916805731/?project=4509062593708032&query=is%3Aunresolved')",
		"```",
		"",
		"### With issue ID and organization",
		"```",
		"get_issue_details(organizationSlug='my-organization', issueId='CLOUDFLARE-MCP-41')",
		"```",
		"",
		"### With event ID and organization",
		"```",
		"get_issue_details(organizationSlug='my-organization', eventId='c49541c747cb4d8aa3efb70ca5aba243')",
		"```",
		"</examples>",
		"",
		"<hints>",
		"- **IMPORTANT**: If user provides a Sentry URL, pass the ENTIRE URL to issueUrl parameter unchanged",
		"- When using issueUrl, all other parameters are automatically extracted - don't provide them separately",
		"- If using issueId (not URL), then organizationSlug is required",
		"</hints>"
	].join("\n"),
	inputSchema: {
		organizationSlug: ParamOrganizationSlug.optional(),
		regionUrl: ParamRegionUrl.optional(),
		issueId: ParamIssueShortId.optional(),
		eventId: z.string().trim().describe("The ID of the event.").optional(),
		issueUrl: ParamIssueUrl.optional()
	},
	annotations: {
		readOnlyHint: true,
		openWorldHint: true
	},
	async handler(params, context) {
		const apiService = apiServiceFromContext(context, { regionUrl: params.regionUrl });
		if (params.eventId) {
			const orgSlug$1 = params.organizationSlug;
			const eventId = params.eventId;
			if (!orgSlug$1) throw new UserInputError("`organizationSlug` is required when providing `eventId`");
			setTag("organization.slug", orgSlug$1);
			const [issue$1] = await apiService.listIssues({
				organizationSlug: orgSlug$1,
				query: eventId
			});
			if (!issue$1) return `# Event Not Found\n\nNo issue found for Event ID: ${eventId}`;
			let event$1;
			try {
				event$1 = await apiService.getEventForIssue({
					organizationSlug: orgSlug$1,
					issueId: issue$1.shortId,
					eventId
				});
			} catch (error) {
				if (error instanceof ApiNotFoundError) throw enhanceNotFoundError(error, {
					organizationSlug: orgSlug$1,
					issueId: issue$1.shortId,
					eventId
				});
				throw error;
			}
			let autofixState$1;
			try {
				autofixState$1 = await apiService.getAutofixState({
					organizationSlug: orgSlug$1,
					issueId: issue$1.shortId
				});
			} catch (error) {}
			const performanceTrace$1 = await maybeFetchPerformanceTrace({
				apiService,
				organizationSlug: orgSlug$1,
				event: event$1
			});
			return formatIssueOutput({
				organizationSlug: orgSlug$1,
				issue: issue$1,
				event: event$1,
				apiService,
				autofixState: autofixState$1,
				performanceTrace: performanceTrace$1
			});
		}
		if (!params.issueUrl && !params.issueId) throw new UserInputError("Either `issueId` or `issueUrl` must be provided");
		if (!params.issueUrl && !params.organizationSlug) throw new UserInputError("`organizationSlug` is required when providing `issueId`");
		const { organizationSlug: orgSlug, issueId: parsedIssueId } = parseIssueParams({
			organizationSlug: params.organizationSlug,
			issueId: params.issueId,
			issueUrl: params.issueUrl
		});
		setTag("organization.slug", orgSlug);
		let issue;
		try {
			issue = await apiService.getIssue({
				organizationSlug: orgSlug,
				issueId: parsedIssueId
			});
		} catch (error) {
			if (error instanceof ApiNotFoundError) throw enhanceNotFoundError(error, {
				organizationSlug: orgSlug,
				issueId: parsedIssueId
			});
			throw error;
		}
		const event = await apiService.getLatestEventForIssue({
			organizationSlug: orgSlug,
			issueId: issue.shortId
		});
		let autofixState;
		try {
			autofixState = await apiService.getAutofixState({
				organizationSlug: orgSlug,
				issueId: issue.shortId
			});
		} catch (error) {}
		const performanceTrace = await maybeFetchPerformanceTrace({
			apiService,
			organizationSlug: orgSlug,
			event
		});
		return formatIssueOutput({
			organizationSlug: orgSlug,
			issue,
			event,
			apiService,
			autofixState,
			performanceTrace
		});
	}
});
async function maybeFetchPerformanceTrace({ apiService, organizationSlug, event }) {
	const context = shouldFetchTraceForEvent(event);
	if (!context) return void 0;
	try {
		return await apiService.getTrace({
			organizationSlug,
			traceId: context.traceId,
			limit: 1e4
		});
	} catch (error) {
		logError(error);
		return void 0;
	}
}
function isErrorEvent(event) {
	return event.type === "error" || event.type === "default";
}
function shouldFetchTraceForEvent(event) {
	if (isErrorEvent(event)) return null;
	const traceId = event.contexts?.trace?.trace_id;
	if (typeof traceId !== "string" || traceId.length === 0) return null;
	return { traceId };
}

//#endregion
export { get_issue_details_default };
//# sourceMappingURL=get-issue-details-CucQ2nf7.js.map