import { p as resolveUserPath } from "./utils-CCC-BEJH.js";
import { t as loadInstalledPluginIndexInstallRecords } from "./installed-plugin-index-record-reader-BPrT_Xp2.js";
import "./installed-plugin-index-records-BZTYWCfi.js";
import { t as enablePluginInConfig } from "./enable-DWEH_UDj.js";
import { existsSync } from "node:fs";
import path from "node:path";
//#region src/commands/runtime-plugin-install.ts
function isInstalledRecordPresentOnDisk(record, env) {
	const installPath = record?.installPath?.trim();
	if (!installPath) return false;
	return existsSync(path.join(resolveUserPath(installPath, env), "package.json"));
}
async function ensureRuntimePluginForModelSelection(params) {
	if (!params.shouldEnsure({
		cfg: params.cfg,
		model: params.model
	})) return {
		cfg: params.cfg,
		required: false,
		installed: false
	};
	if (isInstalledRecordPresentOnDisk((await loadInstalledPluginIndexInstallRecords({ env: process.env }))[params.descriptor.pluginId], process.env)) {
		const repair = await repairRuntimePluginInstallForModelSelection({
			cfg: params.cfg,
			model: params.model,
			env: process.env,
			descriptor: params.descriptor,
			shouldEnsure: params.shouldEnsure
		});
		for (const change of repair.changes) params.runtime.log?.(change);
		for (const warning of repair.warnings) params.runtime.log?.(`${params.descriptor.warningLabel} update warning: ${warning}`);
		const enableResult = enablePluginInConfig(params.cfg, params.descriptor.pluginId);
		return {
			cfg: enableResult.enabled ? enableResult.config : params.cfg,
			required: true,
			installed: true,
			status: "installed"
		};
	}
	const { ensureOnboardingPluginInstalled } = await import("./onboarding-plugin-install-BC4zH7HX.js");
	const result = await ensureOnboardingPluginInstalled({
		cfg: params.cfg,
		entry: {
			pluginId: params.descriptor.pluginId,
			label: params.descriptor.label,
			install: {
				npmSpec: params.descriptor.npmSpec,
				defaultChoice: "npm"
			},
			trustedSourceLinkedOfficialInstall: true,
			preferRemoteInstall: true
		},
		prompter: params.prompter,
		runtime: params.runtime,
		...params.workspaceDir !== void 0 ? { workspaceDir: params.workspaceDir } : {},
		promptInstall: false,
		autoConfirmSingleSource: true
	});
	return {
		cfg: result.cfg,
		required: true,
		installed: result.installed,
		status: result.status
	};
}
async function repairRuntimePluginInstallForModelSelection(params) {
	if (!params.shouldEnsure({
		cfg: params.cfg,
		model: params.model
	})) return {
		required: false,
		changes: [],
		warnings: []
	};
	const { repairMissingPluginInstallsForIds } = await import("./missing-configured-plugin-install-DU7ihELV.js");
	const result = await repairMissingPluginInstallsForIds({
		cfg: params.cfg,
		pluginIds: [params.descriptor.pluginId],
		...params.env !== void 0 ? { env: params.env } : {}
	});
	return {
		required: true,
		changes: result.changes,
		warnings: result.warnings
	};
}
function createRuntimePluginModelSelectionHelpers(params) {
	return {
		ensure: (ensureParams) => ensureRuntimePluginForModelSelection({
			...ensureParams,
			descriptor: params.descriptor,
			shouldEnsure: params.shouldEnsure
		}),
		repair: (repairParams) => repairRuntimePluginInstallForModelSelection({
			...repairParams,
			descriptor: params.descriptor,
			shouldEnsure: params.shouldEnsure
		})
	};
}
//#endregion
export { createRuntimePluginModelSelectionHelpers as t };
