import "../../../../../../dialect-helpers-DRI5pyY3.mjs";
import "../../../../../../base64-CmWvODNW.mjs";
import "../../../../../../types-CuW0zWDA.mjs";
import "../../../../../../request-cache-KCNHp_RJ.mjs";
import "../../../../../../loader-CB6YHeul.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 { g as updateWidgetBody } from "../../../../../../redirects-DXEXBAII.mjs";
import "../../../../../../byline-fields-2Lp5Hbgf.mjs";
import "../../../../../../status-CqJQX1QN.mjs";
import "../../../../../../api/schemas/index.mjs";
import { i as rowToWidget } from "../../../../../../widgets-hWcIicX8.mjs";
import { n as requirePerm } from "../../../../../../authorize-Dz88F_Fw.mjs";

//#region src/astro/routes/api/widget-areas/[name]/widgets/[id].ts
const prerender = false;
const PUT = async ({ params, request, locals }) => {
	const { emdash, user } = locals;
	const db = emdash.db;
	const { name, id } = params;
	const denied = requirePerm(user, "widgets:manage");
	if (denied) return denied;
	if (!name || !id) return apiError("VALIDATION_ERROR", "name and id are required", 400);
	try {
		const area = await db.selectFrom("_emdash_widget_areas").select("id").where("name", "=", name).executeTakeFirst();
		if (!area) return apiError("NOT_FOUND", `Widget area "${name}" not found`, 404);
		if (!await db.selectFrom("_emdash_widgets").select("id").where("id", "=", id).where("area_id", "=", area.id).executeTakeFirst()) return apiError("NOT_FOUND", `Widget "${id}" not found in area "${name}"`, 404);
		const body = await parseBody(request, updateWidgetBody);
		if (isParseError(body)) return body;
		const updates = {};
		if (body.title !== void 0) updates.title = body.title || null;
		if (body.type !== void 0) updates.type = body.type;
		if (body.content !== void 0) updates.content = body.content ? JSON.stringify(body.content) : null;
		if (body.menuName !== void 0) updates.menu_name = body.menuName || null;
		if (body.componentId !== void 0) updates.component_id = body.componentId || null;
		if (body.componentProps !== void 0) updates.component_props = body.componentProps ? JSON.stringify(body.componentProps) : null;
		if (Object.keys(updates).length === 0) return apiError("VALIDATION_ERROR", "No fields to update", 400);
		await db.updateTable("_emdash_widgets").set(updates).where("id", "=", id).execute();
		return apiSuccess(rowToWidget(await db.selectFrom("_emdash_widgets").selectAll().$castTo().where("id", "=", id).executeTakeFirstOrThrow()));
	} catch (error) {
		return handleError(error, "Failed to update widget", "WIDGET_UPDATE_ERROR");
	}
};
const DELETE = async ({ params, locals }) => {
	const { emdash, user } = locals;
	const db = emdash.db;
	const { name, id } = params;
	const denied = requirePerm(user, "widgets:manage");
	if (denied) return denied;
	if (!name || !id) return apiError("VALIDATION_ERROR", "name and id are required", 400);
	try {
		const area = await db.selectFrom("_emdash_widget_areas").select("id").where("name", "=", name).executeTakeFirst();
		if (!area) return apiError("NOT_FOUND", `Widget area "${name}" not found`, 404);
		if (!await db.selectFrom("_emdash_widgets").select("id").where("id", "=", id).where("area_id", "=", area.id).executeTakeFirst()) return apiError("NOT_FOUND", `Widget "${id}" not found in area "${name}"`, 404);
		await db.deleteFrom("_emdash_widgets").where("id", "=", id).execute();
		return apiSuccess({ deleted: true });
	} catch (error) {
		return handleError(error, "Failed to delete widget", "WIDGET_DELETE_ERROR");
	}
};

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