// POST /api/v1/library/custom — creating a custom entry can install a command the host later runs
// as the host user (`prep`, or a `command` launch), so it joins hooks/update-apply/raw-install
// behind the console password when — and only when — the payload carries one of those fields.
// See util/libraryConfirm.ts for the reasoning; 2026-08-05 review M-6.
//
// Wins over the `/api/**` catch-all by h3 route specificity.
import { defineEventHandler, readBody } from "h3";
import { forwardJson } from "../../../../util/forward";
import { confirmIfCommandExecution } from "../../../../util/libraryConfirm";

export default defineEventHandler(async (event) => {
	const body = await readBody<Record<string, unknown>>(event);
	confirmIfCommandExecution(event, body, body?.password);
	// Strip the confirmation before forwarding — the host has no such field and it must not leak
	// upstream or into `library.json`.
	const { password: _password, ...entry } = body ?? {};
	return forwardJson(event, "/api/v1/library/custom", "POST", entry);
});
