import "../../../../../../dialect-helpers-DRI5pyY3.mjs";
import "../../../../../../after-B1IIdH3Y.mjs";
import "../../../../../../object-cache-Bok5j2ae.mjs";
import "../../../../../../content-DhgEth4k.mjs";
import "../../../../../../base64-CmWvODNW.mjs";
import "../../../../../../types-CuW0zWDA.mjs";
import "../../../../../../media-BrXIaMFN.mjs";
import "../../../../../../user-BjAOvU3Z.mjs";
import "../../../../../../taxonomy-BxGxZZdu.mjs";
import "../../../../../../comment-CzeatwBV.mjs";
import "../../../../../../options-DTTML-Tx.mjs";
import "../../../../../../menus-Cfivc2Ad.mjs";
import "../../../../../../redirect-IyfNirgS.mjs";
import "../../../../../../request-cache-KCNHp_RJ.mjs";
import "../../../../../../byline-registry-BOjqDOim.mjs";
import "../../../../../../field-defs-cache-WUI88LX5.mjs";
import "../../../../../../byline-Bm8ZgUP9.mjs";
import "../../../../../../seo-DXu2UQnC.mjs";
import { n as handleRegistryInstall } from "../../../../../../api-BFURZn1i.mjs";
import "../../../../../../dashboard-EvEKF5en.mjs";
import "../../../../../../content-refresh-CA4uKwzH.mjs";
import "../../../../../../fts-manager-BbFaF_V8.mjs";
import "../../../../../../registry-B65mUtPa.mjs";
import "../../../../../../loader-CB6YHeul.mjs";
import "../../../../../../schema-Blhoxp3P.mjs";
import "../../../../../../zod-generator-CacrR-i8.mjs";
import "../../../../../../seo-89-53DDs.mjs";
import "../../../../../../sections-CpF5SaGr.mjs";
import "../../../../../../settings-zhmdKyJQ.mjs";
import "../../../../../../settings-rZyHEcNT.mjs";
import "../../../../../../resolve-DR3wqMTy.mjs";
import "../../../../../../taxonomies-Cnb4F-Pb.mjs";
import "../../../../../../taxonomies-BGBd16Eu.mjs";
import "../../../../../../manifest-schema-BHlgZM1W.mjs";
import "../../../../../../types-DROWRxYV.mjs";
import "../../../../../../ssrf-1g7usGBg.mjs";
import { a as unwrapResult, r as handleError, t as apiError } from "../../../../../../error-CkAseSqZ.mjs";
import { n as parseBody, t as isParseError } from "../../../../../../parse-GmjecGct.mjs";
import "../../../../../../redirects-DXEXBAII.mjs";
import "../../../../../../byline-fields-2Lp5Hbgf.mjs";
import "../../../../../../status-CqJQX1QN.mjs";
import { n as VERSION } from "../../../../../../version-CaccU7DM.mjs";
import { n as requirePerm } from "../../../../../../authorize-Dz88F_Fw.mjs";
import { z } from "zod";
import { hostEnvFromVersions } from "@emdash-cms/registry-client/env";

//#region src/astro/routes/api/admin/plugins/registry/install.ts
/**
* Registry plugin install endpoint
*
* POST /_emdash/api/admin/plugins/registry/install
*
* Installs a plugin from the experimental decentralized plugin registry
* (see RFC 0001). The browser resolves `(handle, slug) → (did, slug)`
* via the aggregator before posting and sends the publisher DID
* directly; the server skips the resolvePackage round-trip and looks
* up the package by DID. Sending DID rather than handle means installs
* work for publishers whose handle the aggregator couldn't resolve at
* view time (handle is best-effort per the lexicon).
*/
const prerender = false;
const installBodySchema = z.object({
	did: z.string().min(1).max(2048).regex(/^did:[a-z]+:/, "Invalid DID"),
	slug: z.string().min(1).max(64).regex(/^[a-zA-Z][a-zA-Z0-9_-]*$/, "Invalid slug"),
	version: z.string().min(1).max(64).optional(),
	acknowledgedDeclaredAccess: z.unknown().optional()
});
const POST = async ({ request, locals }) => {
	try {
		const { emdash, user } = locals;
		if (!emdash?.db) return apiError("NOT_CONFIGURED", "EmDash is not initialized", 500);
		const denied = requirePerm(user, "plugins:manage");
		if (denied) return denied;
		const body = await parseBody(request, installBodySchema);
		if (isParseError(body)) return body;
		const reservedPluginIds = new Set([...emdash.configuredPlugins.map((p) => p.id), ...(emdash.config.sandboxed ?? []).map((p) => p.id)]);
		const result = await handleRegistryInstall(emdash.db, emdash.storage, emdash.getSandboxRunner(), emdash.config.experimental?.registry, {
			did: body.did,
			slug: body.slug,
			version: body.version,
			acknowledgedDeclaredAccess: body.acknowledgedDeclaredAccess
		}, {
			configuredPluginIds: reservedPluginIds,
			hostEnv: hostEnvFromVersions(VERSION, emdash.config.astroVersion)
		});
		if (!result.success) return unwrapResult(result);
		await emdash.syncRegistryPlugins();
		return unwrapResult(result, 201);
	} catch (error) {
		console.error("[registry-install] Unhandled error:", error);
		return handleError(error, "Failed to install plugin from registry", "INSTALL_FAILED");
	}
};

//#endregion
export { POST, prerender };
//# sourceMappingURL=install.mjs.map