import { Cl as uninstallPlugin, Dn as loadConfig, Sl as resolveUninstallDirectoryTarget, io as clearPluginManifestRegistryCache, kf as applyExclusiveSlotSelection, pr as ensurePluginAllowlisted } from "./agent-scope-CLn_Ws3B.js";
import { t as enablePluginInConfig } from "./enable-DYrDatZX.js";
import { n as recordPluginInstall } from "./installs-BS-BIvab.js";
import { t as buildPluginStatusReport } from "./status-CTveTSuF.js";
import { n as updateNpmInstalledPlugins } from "./update-fKz5UGr6.js";

//#region src/plugins/lifecycle.ts
function applySlotSelectionForPlugin(config, pluginId) {
	const report = buildPluginStatusReport({ 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 addPluginLoadPath(cfg, pluginPath) {
	const existing = cfg.plugins?.load?.paths ?? [];
	const merged = Array.from(new Set([...existing, pluginPath]));
	return {
		...cfg,
		plugins: {
			...cfg.plugins,
			load: {
				...cfg.plugins?.load,
				paths: merged
			}
		}
	};
}
function finalizeInstalledPluginConfig(params) {
	if (params.refreshManifestRegistry) clearPluginManifestRegistryCache();
	let next = params.config;
	if (params.loadPath) next = addPluginLoadPath(next, params.loadPath);
	next = enablePluginInConfig(next, params.pluginId).config;
	if (params.installRecord) next = recordPluginInstall(next, {
		pluginId: params.pluginId,
		...params.installRecord
	});
	next = ensurePluginAllowlisted(next, params.pluginId, { createIfMissing: true });
	const slotResult = applySlotSelectionForPlugin(next, params.pluginId);
	return {
		config: slotResult.config,
		slotWarnings: slotResult.warnings
	};
}
function buildPluginLifecycleReport(params) {
	const config = params?.config ?? loadConfig();
	const report = params?.report ?? buildPluginStatusReport({
		config,
		...params?.workspaceDir ? { workspaceDir: params.workspaceDir } : {}
	});
	const installs = config.plugins?.installs ?? {};
	const plugins = report.plugins ?? [];
	return {
		...report,
		plugins: plugins.map((plugin) => ({
			...plugin,
			...installs[plugin.id] ? { install: installs[plugin.id] } : {},
			managed: Boolean((config.plugins?.entries ?? {})[plugin.id] || installs[plugin.id])
		}))
	};
}
function resolvePluginLifecycleEntry(params) {
	return (params.report ?? buildPluginLifecycleReport({
		config: params.config,
		...params.workspaceDir ? { workspaceDir: params.workspaceDir } : {}
	})).plugins.find((plugin) => plugin.id === params.idOrName || plugin.name === params.idOrName);
}
function buildPluginUninstallPreview(params) {
	const keepFiles = Boolean(params.keepFiles);
	const entry = resolvePluginLifecycleEntry({
		idOrName: params.idOrName,
		config: params.config,
		report: params.report
	});
	const pluginId = entry?.id ?? params.idOrName;
	const hasEntry = pluginId in (params.config.plugins?.entries ?? {});
	const hasInstall = pluginId in (params.config.plugins?.installs ?? {});
	if (!hasEntry && !hasInstall) return {
		ok: false,
		error: entry ? `Plugin "${pluginId}" is not managed by plugins config/install records and cannot be uninstalled.` : `Plugin not found: ${params.idOrName}`,
		...entry ? { pluginId } : {}
	};
	const install = params.config.plugins?.installs?.[pluginId];
	const isLinked = install?.source === "path";
	const preview = [];
	if (hasEntry) preview.push("config entry");
	if (hasInstall) preview.push("install record");
	if (params.config.plugins?.allow?.includes(pluginId)) preview.push("allowlist entry");
	if (isLinked && install?.sourcePath && params.config.plugins?.load?.paths?.includes(install.sourcePath)) preview.push("load path");
	if (params.config.plugins?.slots?.memory === pluginId) preview.push("memory slot (will reset to \"memory-core\")");
	const deleteTarget = !keepFiles ? resolveUninstallDirectoryTarget({
		pluginId,
		hasInstall,
		installRecord: install,
		extensionsDir: params.extensionsDir
	}) : null;
	if (deleteTarget) preview.push(`directory: ${deleteTarget}`);
	return {
		ok: true,
		pluginId,
		pluginName: entry?.name || pluginId,
		keepFiles,
		hasEntry,
		hasInstall,
		isLinked,
		preview,
		...deleteTarget ? { deleteTarget } : {}
	};
}
async function executePluginUninstallLifecycle(params) {
	return await uninstallPlugin(params);
}
async function executePluginUpdateLifecycle(params) {
	return await updateNpmInstalledPlugins(params);
}

//#endregion
export { executePluginUpdateLifecycle as a, executePluginUninstallLifecycle as i, buildPluginLifecycleReport as n, finalizeInstalledPluginConfig as o, buildPluginUninstallPreview as r, resolvePluginLifecycleEntry as s, applySlotSelectionForPlugin as t };