import { resolveCliCommandPath } from "./argv.js";
import { cliCommandCatalog } from "./command-catalog.js";
import { matchesCommandPath } from "./command-path-matches.js";
import { resolveGatewayCatalogCommandPath } from "./gateway-run-argv.js";
//#region src/cli/command-path-policy.ts
const DEFAULT_CLI_COMMAND_PATH_POLICY = {
	loadExtensions: "when-configured",
	gatewaySubcommands: "eager"
};
function resolveCliCommandPathPolicy(commandPath) {
	const resolvedPolicy = { ...DEFAULT_CLI_COMMAND_PATH_POLICY };
	for (const entry of cliCommandCatalog) {
		if (!entry.policy) continue;
		if (!matchesCommandPath(commandPath, entry.commandPath, { exact: entry.exact })) continue;
		Object.assign(resolvedPolicy, entry.policy);
	}
	return resolvedPolicy;
}
function isCommandPathPrefix(commandPath, pattern) {
	return pattern.every((segment, index) => commandPath[index] === segment);
}
function resolveCliCatalogCommandPath(argv) {
	const tokens = resolveGatewayCatalogCommandPath(argv) ?? resolveCliCommandPath(argv);
	if (tokens.length === 0) return [];
	let bestMatch = null;
	for (const entry of cliCommandCatalog) {
		if (!isCommandPathPrefix(tokens, entry.commandPath)) continue;
		if (!bestMatch || entry.commandPath.length > bestMatch.length) bestMatch = entry.commandPath;
	}
	return bestMatch ? [...bestMatch] : [tokens[0]];
}
//#endregion
export { resolveCliCatalogCommandPath, resolveCliCommandPathPolicy };

//# sourceMappingURL=command-path-policy.js.map