import { ar as listEnabledAddonIds, a8 as getAddonServiceNames, p as checkDocker, F as composeStop, n as buildComposeOptions, b1 as setAddonEnabled, H as createLogger } from './src-CidJbygh.js';
import { r as resetState } from './state-fxN45gBp.js';

//#region src/lib/server/addon-helpers.ts
/**
* Shared addon enable/disable logic for admin route handlers.
*
* Both /admin/addons and /admin/addons/:name share the same POST flow:
* validate → stop running services if disabling → mutate state → audit.
* This module houses the shared mutation step so neither route duplicates it.
*/
var logger = createLogger("addon-helpers");
/**
* Stop running services if disabling, then call setAddonEnabled.
* Returns the mutation result with the final enabled state.
*/
async function performAddonToggle(state, name, requestedEnabled, requestId) {
	const wasEnabled = listEnabledAddonIds(state.homeDir).includes(name);
	const nextEnabled = requestedEnabled !== void 0 ? requestedEnabled : wasEnabled;
	if (!nextEnabled && wasEnabled) {
		const serviceNames = getAddonServiceNames(state.homeDir, name);
		if (serviceNames.length > 0) {
			if ((await checkDocker()).ok) try {
				await composeStop(serviceNames, buildComposeOptions(state));
				logger.info("stopped addon services before disable", {
					name,
					services: serviceNames,
					requestId
				});
			} catch (err) {
				logger.warn("failed to stop addon services before disable", {
					name,
					services: serviceNames,
					error: String(err),
					requestId
				});
			}
		}
	}
	const mutation = setAddonEnabled(state.homeDir, state.stackDir, name, nextEnabled, state);
	if (!mutation.ok) return mutation;
	if (mutation.changed) resetState();
	return {
		ok: true,
		enabled: listEnabledAddonIds(state.homeDir).includes(name),
		changed: mutation.changed
	};
}

export { performAddonToggle as p };
//# sourceMappingURL=addon-helpers-CtXSmg5a.js.map
