import { ActivationPlanner } from "./activation-planner.js";
import { mergeActivationContext } from "./activation-context.js";
//#region src/extensions/bundled-extension-activation.ts
function asSurfaceConfig(config) {
	return config;
}
function filterStringIds(val) {
	if (!Array.isArray(val)) return [];
	return val.filter((x) => typeof x === "string");
}
function optionalEnabledIds(val) {
	if (!Array.isArray(val)) return void 0;
	const ids = filterStringIds(val);
	return ids.length > 0 ? ids : void 0;
}
function buildExtensionsRecord(base, enabled, disabled) {
	const o = { ...base };
	if (enabled !== void 0 && enabled.length > 0) o.enabled = enabled;
	else delete o.enabled;
	if (disabled.length > 0) o.disabled = disabled;
	else delete o.disabled;
	return o;
}
function isExtensionActivationEligible(loader, config, extensionId) {
	const surface = asSurfaceConfig(config);
	loader.setConfig(surface);
	return new ActivationPlanner(loader.buildManifestRegistry()).getActivatedIds(mergeActivationContext(surface)).includes(extensionId);
}
function computeBundledExtensionExtensionsPatch(loader, config, extensionId, wanted) {
	const hit = loader.discoverExtensions().find((e) => e.id === extensionId);
	if (!hit) return {
		ok: false,
		error: "Extension not found"
	};
	if (hit.source !== "bundled") return {
		ok: false,
		error: "Only bundled extensions can be toggled here (workspace or global installs override the same id — use CLI to manage those)."
	};
	const base = config.extensions ?? {};
	let enabled = optionalEnabledIds(base.enabled);
	let disabled = filterStringIds(base.disabled);
	if (wanted) {
		disabled = disabled.filter((id) => id !== extensionId);
		let candidate = buildExtensionsRecord(base, enabled, disabled);
		if (!isExtensionActivationEligible(loader, {
			...config,
			extensions: candidate
		}, extensionId)) {
			const nextEnabled = [...enabled ?? []];
			if (!nextEnabled.includes(extensionId)) nextEnabled.push(extensionId);
			enabled = nextEnabled;
			candidate = buildExtensionsRecord(base, enabled, disabled);
		}
		if (!isExtensionActivationEligible(loader, {
			...config,
			extensions: candidate
		}, extensionId)) return {
			ok: false,
			error: "Extension would still not activate with current settings (channels, environment, or activation rules)."
		};
		return {
			ok: true,
			extensions: candidate
		};
	}
	if (enabled) {
		enabled = enabled.filter((id) => id !== extensionId);
		if (enabled.length === 0) enabled = void 0;
	}
	if (!disabled.includes(extensionId)) disabled = [...disabled, extensionId];
	return {
		ok: true,
		extensions: buildExtensionsRecord(base, enabled, disabled)
	};
}
//#endregion
export { computeBundledExtensionExtensionsPatch, isExtensionActivationEligible };

//# sourceMappingURL=bundled-extension-activation.js.map