import { a as normalizeLowercaseStringOrEmpty } from "./string-coerce-Bje8XVt9.js";
import { r as theme } from "./theme-B128avno.js";
import { r as parseRegistryNpmSpec } from "./npm-registry-spec-BaUygPdz.js";
import { t as applyExclusiveSlotSelection } from "./slots-tjp1BFoh.js";
import { n as defaultRuntime } from "./runtime-CIJX7ulQ.js";
import { t as CLAWHUB_INSTALL_ERROR_CODE } from "./clawhub-DVIkH66x.js";
import { a as buildPluginDiagnosticsReport } from "./status-idhZd57c.js";
//#region src/cli/plugins-command-helpers.ts
function resolveFileNpmSpecToLocalPath(raw) {
	const trimmed = raw.trim();
	if (!normalizeLowercaseStringOrEmpty(trimmed).startsWith("file:")) return null;
	const rest = trimmed.slice(5);
	if (!rest) return {
		ok: false,
		error: "unsupported file: spec: missing path"
	};
	if (rest.startsWith("///")) return {
		ok: true,
		path: rest.slice(2)
	};
	if (rest.startsWith("//localhost/")) return {
		ok: true,
		path: rest.slice(11)
	};
	if (rest.startsWith("//")) return {
		ok: false,
		error: "unsupported file: URL host (expected \"file:<path>\" or \"file:///abs/path\")"
	};
	return {
		ok: true,
		path: rest
	};
}
function applySlotSelectionForPlugin(config, pluginId) {
	const report = buildPluginDiagnosticsReport({ config });
	const plugin = report.plugins.find((entry) => entry.id === pluginId);
	if (!plugin) return {
		config,
		warnings: []
	};
	const result = applyExclusiveSlotSelection({
		config,
		selectedId: plugin.id,
		selectedKind: plugin.kind,
		registry: report
	});
	return {
		config: result.config,
		warnings: result.warnings
	};
}
function createPluginInstallLogger() {
	return {
		info: (msg) => defaultRuntime.log(msg),
		warn: (msg) => defaultRuntime.log(theme.warn(msg))
	};
}
function createHookPackInstallLogger() {
	return {
		info: (msg) => defaultRuntime.log(msg),
		warn: (msg) => defaultRuntime.log(theme.warn(msg))
	};
}
function enableInternalHookEntries(config, hookNames) {
	const entries = { ...config.hooks?.internal?.entries };
	for (const hookName of hookNames) entries[hookName] = {
		...entries[hookName],
		enabled: true
	};
	return {
		...config,
		hooks: {
			...config.hooks,
			internal: {
				...config.hooks?.internal,
				enabled: true,
				entries
			}
		}
	};
}
function formatPluginInstallWithHookFallbackError(pluginError, hookError) {
	if (/plugin already exists: .+ \(delete it first\)/.test(pluginError)) return `${pluginError}\nUse \`openclaw plugins update <id-or-npm-spec>\` to upgrade the tracked plugin, or rerun install with \`--force\` to replace it.`;
	if (pluginError.startsWith("Invalid extensions directory:") || pluginError === "Invalid path: must stay within extensions directory") return pluginError;
	return `${pluginError}\nAlso not a valid hook pack: ${hookError}`;
}
function logHookPackRestartHint() {
	defaultRuntime.log("Restart the gateway to load hooks.");
}
function logSlotWarnings(warnings) {
	if (warnings.length === 0) return;
	for (const warning of warnings) defaultRuntime.log(theme.warn(warning));
}
function buildPreferredClawHubSpec(raw) {
	const parsed = parseRegistryNpmSpec(raw);
	if (!parsed) return null;
	return `clawhub:${parsed.name}${parsed.selector ? `@${parsed.selector}` : ""}`;
}
function parseNpmPrefixSpec(raw) {
	const trimmed = raw.trim();
	if (!normalizeLowercaseStringOrEmpty(trimmed).startsWith("npm:")) return null;
	return trimmed.slice(4).trim();
}
const PREFERRED_CLAWHUB_FALLBACK_DECISION = {
	FALLBACK_TO_NPM: "fallback_to_npm",
	STOP: "stop"
};
function decidePreferredClawHubFallback(params) {
	if (params.code === CLAWHUB_INSTALL_ERROR_CODE.PACKAGE_NOT_FOUND || params.code === CLAWHUB_INSTALL_ERROR_CODE.VERSION_NOT_FOUND) return PREFERRED_CLAWHUB_FALLBACK_DECISION.FALLBACK_TO_NPM;
	return PREFERRED_CLAWHUB_FALLBACK_DECISION.STOP;
}
//#endregion
export { createPluginInstallLogger as a, formatPluginInstallWithHookFallbackError as c, parseNpmPrefixSpec as d, resolveFileNpmSpecToLocalPath as f, createHookPackInstallLogger as i, logHookPackRestartHint as l, applySlotSelectionForPlugin as n, decidePreferredClawHubFallback as o, buildPreferredClawHubSpec as r, enableInternalHookEntries as s, PREFERRED_CLAWHUB_FALLBACK_DECISION as t, logSlotWarnings as u };
