import { t as formatCliCommand } from "./command-format-tWmMYcvZ.js";
import { t as resolveCliArgvInvocation } from "./argv-invocation-BVIVPYjV.js";
import { t as formatDocsLink } from "./links-CM5vg8_V.js";
import { r as theme } from "./theme-D58JpUfy.js";
import { n as defaultRuntime } from "./runtime-E_A14BX_.js";
import { t as danger } from "./globals-DhGeOA1P.js";
import { n as shouldEagerRegisterSubcommands } from "./command-registration-policy-DNB5-kPy.js";
import { t as addGatewayClientOptions } from "./gateway-rpc-BANTsTln.js";
import { t as formatHelpExamples } from "./help-format-DXVAk021.js";
import { i as registerCommandGroups } from "./register-command-groups-BJJkSUKS.js";
import "./cli-runtime-DzLh8RWR.js";
import "./core-api-15EcrNg-.js";
//#region extensions/browser/src/cli/browser-cli-examples.ts
const browserCoreExamples = [
	"daocore browser status",
	"daocore browser start",
	"daocore browser start --headless",
	"daocore browser stop",
	"daocore browser tabs",
	"daocore browser open https://example.com",
	"daocore browser focus abcd1234",
	"daocore browser close abcd1234",
	"daocore browser screenshot",
	"daocore browser screenshot --full-page",
	"daocore browser screenshot --ref 12",
	"daocore browser snapshot",
	"daocore browser snapshot --format aria --limit 200",
	"daocore browser snapshot --efficient",
	"daocore browser snapshot --labels"
];
const browserActionExamples = [
	"daocore browser navigate https://example.com",
	"daocore browser resize 1280 720",
	"daocore browser click 12 --double",
	"daocore browser click-coords 120 340",
	"daocore browser type 23 \"hello\" --submit",
	"daocore browser press Enter",
	"daocore browser hover 44",
	"daocore browser drag 10 11",
	"daocore browser select 9 OptionA OptionB",
	"daocore browser upload /tmp/daocore/uploads/file.pdf",
	"daocore browser fill --fields '[{\"ref\":\"1\",\"value\":\"Ada\"}]'",
	"daocore browser dialog --accept",
	"daocore browser wait --text \"Done\"",
	"daocore browser evaluate --fn '(el) => el.textContent' --ref 7",
	"daocore browser console --level error",
	"daocore browser pdf"
];
//#endregion
//#region extensions/browser/src/cli/browser-cli.ts
const ROOT_BOOLEAN_OPTIONS = new Set(["--dev", "--no-color"]);
const ROOT_VALUE_OPTIONS = new Set([
	"--profile",
	"--log-level",
	"--container"
]);
const BROWSER_BOOLEAN_OPTIONS = new Set(["--json", "--expect-final"]);
const BROWSER_VALUE_OPTIONS = new Set([
	"--browser-profile",
	"--url",
	"--token",
	"--timeout"
]);
const command = (name, description, options) => ({
	name,
	description,
	...options ? { options } : {}
});
const browserCommandGroupDefinitions = [
	{
		placeholders: [
			command("status", "Show browser status"),
			command("start", "Start the browser (no-op if already running)"),
			command("stop", "Stop the browser (best-effort)"),
			command("reset-profile", "Reset browser profile (moves it to Trash)"),
			command("tabs", "List open tabs"),
			command("tab", "Tab shortcuts (index-based)"),
			command("open", "Open a URL in a new tab"),
			command("focus", "Focus a tab by target id, tab id, label, or unique target id prefix"),
			command("close", "Close a tab (target id optional)"),
			command("profiles", "List all browser profiles"),
			command("create-profile", "Create a new browser profile"),
			command("delete-profile", "Delete a browser profile"),
			command("doctor", "Check browser plugin readiness", [{
				flags: "--deep",
				description: "Run a live snapshot probe"
			}])
		],
		register: async (args) => {
			(await import("./browser-cli-manage-DxyNyyfu.js")).registerBrowserManageCommands(args.browser, args.parentOpts);
		}
	},
	{
		placeholders: [command("screenshot", "Capture a screenshot (MEDIA:<path>)"), command("snapshot", "Capture a snapshot (default: ai; aria is the accessibility tree)")],
		register: async (args) => {
			(await import("./browser-cli-inspect-bdB11tMm.js")).registerBrowserInspectCommands(args.browser, args.parentOpts);
		}
	},
	{
		placeholders: [
			command("navigate", "Navigate the current tab to a URL"),
			command("resize", "Resize the viewport"),
			command("click", "Click an element by ref from snapshot"),
			command("click-coords", "Click viewport coordinates"),
			command("type", "Type into an element by ref from snapshot"),
			command("press", "Press a key"),
			command("hover", "Hover an element by ai ref"),
			command("scrollintoview", "Scroll an element into view by ref from snapshot"),
			command("drag", "Drag from one ref to another"),
			command("select", "Select option(s) in a select element"),
			command("upload", "Arm file upload for the next file chooser"),
			command("waitfordownload", "Wait for the next download (and save it)"),
			command("download", "Click a ref and save the resulting download"),
			command("dialog", "Arm the next modal dialog (alert/confirm/prompt)"),
			command("fill", "Fill a form with JSON field descriptors"),
			command("wait", "Wait for time, selector, URL, load state, or JS conditions"),
			command("evaluate", "Evaluate a function against the page or a ref")
		],
		register: async (args) => {
			(await import("./browser-cli-actions-input-pLyGefrN.js")).registerBrowserActionInputCommands(args.browser, args.parentOpts);
		}
	},
	{
		placeholders: [
			command("console", "Get recent console messages"),
			command("pdf", "Save page as PDF"),
			command("responsebody", "Wait for a network response and return its body")
		],
		register: async (args) => {
			(await import("./browser-cli-actions-observe-yIc8XHto.js")).registerBrowserActionObserveCommands(args.browser, args.parentOpts);
		}
	},
	{
		placeholders: [
			command("highlight", "Highlight an element by ref"),
			command("errors", "Get recent page errors"),
			command("requests", "Get recent network requests (best-effort)"),
			command("trace", "Record a Playwright trace")
		],
		register: async (args) => {
			(await import("./browser-cli-debug-XNnJPUBx.js")).registerBrowserDebugCommands(args.browser, args.parentOpts);
		}
	},
	{
		placeholders: [
			command("cookies", "Read/write cookies"),
			command("storage", "Read/write localStorage/sessionStorage"),
			command("set", "Browser environment settings")
		],
		register: async (args) => {
			(await import("./browser-cli-state-CNGbNPwa.js")).registerBrowserStateCommands(args.browser, args.parentOpts);
		}
	}
];
function buildBrowserCommandGroups(params) {
	return browserCommandGroupDefinitions.map((entry) => ({
		placeholders: entry.placeholders,
		register: async () => await entry.register(params)
	}));
}
function isValueToken(arg) {
	return Boolean(arg && arg !== "--" && (!arg.startsWith("-") || /^-\d+(?:\.\d+)?$/.test(arg)));
}
function consumeOption(args, index, booleanOptions, valueOptions) {
	const arg = args[index];
	if (!arg || arg === "--" || !arg.startsWith("-")) return 0;
	const equalsIndex = arg.indexOf("=");
	const flag = equalsIndex === -1 ? arg : arg.slice(0, equalsIndex);
	if (booleanOptions.has(flag)) return equalsIndex === -1 ? 1 : 0;
	if (!valueOptions.has(flag)) return 0;
	if (equalsIndex !== -1) return arg.slice(equalsIndex + 1).trim() ? 1 : 0;
	return isValueToken(args[index + 1]) ? 2 : 1;
}
function resolveBrowserLazySubcommand(argv) {
	const { primary } = resolveCliArgvInvocation(argv);
	if (primary !== "browser") return null;
	const args = argv.slice(2);
	let sawBrowser = false;
	for (let i = 0; i < args.length; i += 1) {
		const arg = args[i];
		if (!arg || arg === "--") break;
		if (!sawBrowser) {
			const consumed = consumeOption(args, i, ROOT_BOOLEAN_OPTIONS, ROOT_VALUE_OPTIONS);
			if (consumed > 0) {
				i += consumed - 1;
				continue;
			}
			if (arg.startsWith("-")) continue;
			if (arg === "browser") {
				sawBrowser = true;
				continue;
			}
			return null;
		}
		const consumed = consumeOption(args, i, BROWSER_BOOLEAN_OPTIONS, BROWSER_VALUE_OPTIONS);
		if (consumed > 0) {
			i += consumed - 1;
			continue;
		}
		if (arg.startsWith("-")) continue;
		return arg;
	}
	return null;
}
function resolveBrowserParentOpts(cmd) {
	for (let current = cmd; current; current = current.parent) if (current.name() === "browser") return current.opts();
	return cmd.parent?.opts?.();
}
function registerLazyBrowserCommands(browser, parentOpts, argv) {
	const subcommand = resolveBrowserLazySubcommand(argv);
	registerCommandGroups(browser, buildBrowserCommandGroups({
		browser,
		parentOpts
	}), {
		eager: shouldEagerRegisterSubcommands(),
		primary: subcommand,
		registerPrimaryOnly: subcommand !== null
	});
}
function registerBrowserCli(program, argv = process.argv) {
	const browser = program.command("browser").description("Manage DaoCore's dedicated browser (Chrome/Chromium)").option("--browser-profile <name>", "Browser profile name (default from config)").option("--json", "Output machine-readable JSON", false).addHelpText("after", () => `\n${theme.heading("Examples:")}\n${formatHelpExamples([...browserCoreExamples, ...browserActionExamples].map((cmd) => [cmd, ""]), true)}\n\n${theme.muted("Docs:")} ${formatDocsLink("/cli/browser", "docs.openclaw.ai/cli/browser")}\n`).action(() => {
		browser.outputHelp();
		defaultRuntime.error(danger(`Missing subcommand. Try: "${formatCliCommand("daocore browser status")}"`));
		defaultRuntime.exit(1);
	});
	addGatewayClientOptions(browser);
	registerLazyBrowserCommands(browser, resolveBrowserParentOpts, argv);
}
//#endregion
export { registerBrowserCli as t };
