import { DEFAULT_SCOPES } from "./constants-BPM_OWt0.js";
import { validateAndParseSentryUrlThrows, validateOpenAiBaseUrlThrows, validateSentryHostThrows } from "./url-utils-d9bPTnOl.js";
import { ALL_SCOPES, parseScopes, resolveScopes } from "./permissions-xBjHy6Qx.js";

//#region src/cli/resolve.ts
function formatInvalid(invalid, envName) {
	const where = envName ? `${envName} provided` : "Invalid scopes provided";
	return `Error: ${where}: ${invalid.join(", ")}\nAvailable scopes: ${ALL_SCOPES.join(", ")}`;
}
function finalize(input) {
	if (!input.accessToken) throw new Error("Error: No access token was provided. Pass one with `--access-token` or via `SENTRY_ACCESS_TOKEN`.");
	let sentryHost = "sentry.io";
	if (input.url) sentryHost = validateAndParseSentryUrlThrows(input.url);
	else if (input.host) {
		validateSentryHostThrows(input.host);
		sentryHost = input.host;
	}
	let finalScopes = void 0;
	if (input.allScopes) finalScopes = new Set(ALL_SCOPES);
	else if (input.scopes || input.addScopes) {
		if (input.scopes) {
			const { valid, invalid } = parseScopes(input.scopes);
			if (invalid.length > 0) throw new Error(formatInvalid(invalid));
			if (valid.size === 0) throw new Error("Error: Invalid scopes provided. No valid scopes found.");
			finalScopes = resolveScopes({
				override: valid,
				defaults: DEFAULT_SCOPES
			});
		} else if (input.addScopes) {
			const { valid, invalid } = parseScopes(input.addScopes);
			if (invalid.length > 0) throw new Error(formatInvalid(invalid));
			if (valid.size === 0) throw new Error("Error: Invalid additional scopes provided. No valid scopes found.");
			finalScopes = resolveScopes({
				add: valid,
				defaults: DEFAULT_SCOPES
			});
		}
	}
	const resolvedOpenAiBaseUrl = input.openaiBaseUrl ? validateOpenAiBaseUrlThrows(input.openaiBaseUrl) : void 0;
	return {
		accessToken: input.accessToken,
		sentryHost,
		mcpUrl: input.mcpUrl,
		sentryDsn: input.sentryDsn,
		openaiBaseUrl: resolvedOpenAiBaseUrl,
		finalScopes,
		organizationSlug: input.organizationSlug,
		projectSlug: input.projectSlug
	};
}

//#endregion
export { finalize, formatInvalid };
//# sourceMappingURL=resolve-C95FW12c.js.map