import "../../../../../../base64-CmWvODNW.mjs";
import "../../../../../../types-CuW0zWDA.mjs";
import { t as OptionsRepository } from "../../../../../../options-DTTML-Tx.mjs";
import { n as apiSuccess, r as handleError, t as apiError } from "../../../../../../error-CkAseSqZ.mjs";
import { n as parseBody, t as isParseError } from "../../../../../../parse-GmjecGct.mjs";
import { n as requirePerm } from "../../../../../../authorize-Dz88F_Fw.mjs";
import { z } from "zod";

//#region src/astro/routes/api/admin/hooks/exclusive/[hookName].ts
const prerender = false;
/** Hook name format: namespace:action (e.g., "content:beforeSave") */
const HOOK_NAME_RE = /^[a-z]+:[a-zA-Z]+$/;
const setSelectionSchema = z.object({ pluginId: z.string().min(1).nullable() });
const PUT = async ({ params, request, locals }) => {
	const { emdash, user } = locals;
	if (!emdash?.db) return apiError("NOT_CONFIGURED", "EmDash is not initialized", 500);
	const denied = requirePerm(user, "settings:manage");
	if (denied) return denied;
	const hookName = params.hookName;
	if (!hookName) return apiError("VALIDATION_ERROR", "Hook name is required", 400);
	if (!HOOK_NAME_RE.test(hookName)) return apiError("VALIDATION_ERROR", "Invalid hook name format", 400);
	try {
		const pipeline = emdash.hooks;
		if (!pipeline.isExclusiveHook(hookName)) return apiError("NOT_FOUND", `Hook '${hookName}' is not a registered exclusive hook`, 404);
		const body = await parseBody(request, setSelectionSchema);
		if (isParseError(body)) return body;
		const optionsRepo = new OptionsRepository(emdash.db);
		const optionKey = `emdash:exclusive_hook:${hookName}`;
		if (body.pluginId === null) {
			await optionsRepo.delete(optionKey);
			pipeline.clearExclusiveSelection(hookName);
		} else {
			if (!pipeline.getExclusiveHookProviders(hookName).some((p) => p.pluginId === body.pluginId)) return apiError("VALIDATION_ERROR", `Plugin '${body.pluginId}' is not a provider for hook '${hookName}'`, 400);
			await optionsRepo.set(optionKey, body.pluginId);
			pipeline.setExclusiveSelection(hookName, body.pluginId);
		}
		return apiSuccess({
			hookName,
			selectedPluginId: body.pluginId
		});
	} catch (error) {
		return handleError(error, "Failed to set exclusive hook selection", "EXCLUSIVE_HOOK_SET_ERROR");
	}
};

//#endregion
export { PUT, prerender };
//# sourceMappingURL=_hookName_.mjs.map