import { parseArgs } from "node:util";

//#region src/cli/parse.ts
function parseArgv(argv) {
	const options = {
		"access-token": { type: "string" },
		host: { type: "string" },
		url: { type: "string" },
		"mcp-url": { type: "string" },
		"sentry-dsn": { type: "string" },
		"openai-base-url": { type: "string" },
		"organization-slug": { type: "string" },
		"project-slug": { type: "string" },
		scopes: { type: "string" },
		"add-scopes": { type: "string" },
		"all-scopes": { type: "boolean" },
		agent: { type: "boolean" },
		help: {
			type: "boolean",
			short: "h"
		},
		version: {
			type: "boolean",
			short: "v"
		}
	};
	const { values, positionals, tokens } = parseArgs({
		args: argv,
		options,
		allowPositionals: false,
		strict: false,
		tokens: true
	});
	const knownLong = new Set(Object.keys(options));
	const knownShort = new Set([...Object.values(options).map((o) => "short" in o ? o.short : void 0).filter(Boolean)]);
	const unknownArgs = [];
	for (const t of tokens || []) if (t.kind === "option") {
		const name = t.name;
		if (name && !(knownLong.has(name) || knownShort.has(name))) unknownArgs.push(t.raw ?? `--${name}`);
	} else if (t.kind === "positional") unknownArgs.push(t.raw ?? String(t.value ?? ""));
	return {
		accessToken: values["access-token"],
		host: values.host,
		url: values.url,
		mcpUrl: values["mcp-url"],
		sentryDsn: values["sentry-dsn"],
		openaiBaseUrl: values["openai-base-url"],
		organizationSlug: values["organization-slug"],
		projectSlug: values["project-slug"],
		scopes: values.scopes,
		addScopes: values["add-scopes"],
		allScopes: values["all-scopes"] === true,
		agent: values.agent === true,
		help: values.help === true,
		version: values.version === true,
		unknownArgs: unknownArgs.length > 0 ? unknownArgs : positionals || []
	};
}
function parseEnv(env) {
	const fromEnv = {};
	if (env.SENTRY_ACCESS_TOKEN) fromEnv.accessToken = env.SENTRY_ACCESS_TOKEN;
	if (env.SENTRY_URL) fromEnv.url = env.SENTRY_URL;
	if (env.SENTRY_HOST) fromEnv.host = env.SENTRY_HOST;
	if (env.MCP_URL) fromEnv.mcpUrl = env.MCP_URL;
	if (env.SENTRY_DSN || env.DEFAULT_SENTRY_DSN) fromEnv.sentryDsn = env.SENTRY_DSN || env.DEFAULT_SENTRY_DSN;
	if (env.MCP_SCOPES) fromEnv.scopes = env.MCP_SCOPES;
	if (env.MCP_ADD_SCOPES) fromEnv.addScopes = env.MCP_ADD_SCOPES;
	return fromEnv;
}
function merge(cli, env) {
	const merged = {
		accessToken: cli.accessToken ?? env.accessToken,
		url: cli.url ?? env.url,
		host: cli.host ?? env.host,
		mcpUrl: cli.mcpUrl ?? env.mcpUrl,
		sentryDsn: cli.sentryDsn ?? env.sentryDsn,
		openaiBaseUrl: cli.openaiBaseUrl,
		scopes: cli.scopes ?? env.scopes,
		addScopes: cli.addScopes ?? env.addScopes,
		allScopes: cli.allScopes === true,
		agent: cli.agent === true,
		organizationSlug: cli.organizationSlug,
		projectSlug: cli.projectSlug,
		help: cli.help === true,
		version: cli.version === true,
		unknownArgs: cli.unknownArgs
	};
	if (cli.scopes) merged.addScopes = cli.addScopes;
	if (cli.addScopes) merged.scopes = cli.scopes;
	return merged;
}

//#endregion
export { merge, parseArgv, parseEnv };
//# sourceMappingURL=parse-Da0oa8W8.js.map