/**
 * autonomous-command-args.js — validates `/autonomous` command arguments.
 *
 * Purpose: keep autonomous run control strict and explainable by accepting only
 * the small documented argument set; invented knobs fail as unsupported input.
 *
 * Consumer: headless.ts before machine-surface startup and the interactive
 * autonomous command handler before dispatching the run loop.
 */

const VALUE_FLAGS = new Set(["--yolo", "-y"]);
const SWITCH_FLAGS = new Set(["--verbose", "--debug"]);
const MILESTONE_TARGET_RE = /^M\d+(?:-[a-z0-9]{6})?$/i;

/**
 * Return autonomous arguments that are not part of the supported command grammar.
 *
 * Purpose: reject stale or invented knobs before they can be confused with run
 * control, permission profiles, or output formats.
 *
 * Consumer: headless machine-surface validation and `/autonomous` routing.
 */
export function findUnsupportedAutonomousArgs(args) {
	const unsupported = [];
	for (let i = 0; i < args.length; i++) {
		const arg = String(args[i] ?? "").trim();
		if (!arg) continue;
		if (VALUE_FLAGS.has(arg)) {
			const value = String(args[i + 1] ?? "").trim();
			if (!value || value.startsWith("-")) {
				unsupported.push(arg);
			} else {
				i += 1;
			}
			continue;
		}
		if (SWITCH_FLAGS.has(arg) || MILESTONE_TARGET_RE.test(arg)) continue;
		unsupported.push(arg);
	}
	return unsupported;
}

/**
 * Format unsupported autonomous arguments for human and machine-surface stderr.
 *
 * Purpose: give callers a direct correction path without giving unsupported
 * arguments product meaning.
 *
 * Consumer: headless.ts and the autonomous command handler.
 */
export function formatUnsupportedAutonomousArgs(args) {
	return `Unsupported /autonomous argument(s): ${args.join(", ")}. Supported arguments: --verbose, --debug, --yolo <file>, and optional M### milestone target.`;
}
