import "../../../../../dialect-helpers-DRI5pyY3.mjs";
import "../../../../../after-B1IIdH3Y.mjs";
import "../../../../../object-cache-Bok5j2ae.mjs";
import "../../../../../base64-CmWvODNW.mjs";
import "../../../../../types-CuW0zWDA.mjs";
import "../../../../../media-BrXIaMFN.mjs";
import "../../../../../options-DTTML-Tx.mjs";
import "../../../../../request-cache-KCNHp_RJ.mjs";
import "../../../../../loader-CB6YHeul.mjs";
import "../../../../../settings-zhmdKyJQ.mjs";
import { n as resolveAndValidateExternalUrl, t as SsrfError } from "../../../../../ssrf-1g7usGBg.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 "../../../../../redirects-DXEXBAII.mjs";
import { m as wpPluginAnalyzeBody } from "../../../../../byline-fields-2Lp5Hbgf.mjs";
import "../../../../../status-CqJQX1QN.mjs";
import "../../../../../api/schemas/index.mjs";
import { p as getSource } from "../../../../../import-fYLf14Co.mjs";
import "../../../../../ssrf-CmuGgCq3.mjs";
import "../../../../../utils-BJXElxdw.mjs";
import { n as requirePerm } from "../../../../../authorize-Dz88F_Fw.mjs";
import { SchemaRegistry } from "emdash";

//#region src/astro/routes/api/import/wordpress-plugin/analyze.ts
const prerender = false;
const POST = async ({ request, locals }) => {
	const { emdash, user } = locals;
	const denied = requirePerm(user, "import:execute");
	if (denied) return denied;
	try {
		const body = await parseBody(request, wpPluginAnalyzeBody);
		if (isParseError(body)) return body;
		try {
			await resolveAndValidateExternalUrl(body.url);
		} catch (e) {
			return apiError("SSRF_BLOCKED", e instanceof SsrfError ? e.message : "Invalid URL", 400);
		}
		const source = getSource("wordpress-plugin");
		if (!source) return apiError("NOT_CONFIGURED", "WordPress plugin source not available", 500);
		const existingCollections = await fetchExistingCollections(emdash?.db);
		return apiSuccess({
			success: true,
			analysis: await source.analyze({
				type: "url",
				url: body.url,
				token: body.token
			}, {
				db: emdash?.db,
				getExistingCollections: async () => existingCollections
			})
		});
	} catch (error) {
		return handleError(error, "Failed to analyze WordPress site", "WP_PLUGIN_ANALYZE_ERROR");
	}
};
/** Fetch collections and their fields from schema registry */
async function fetchExistingCollections(db) {
	const result = /* @__PURE__ */ new Map();
	if (!db) return result;
	try {
		const registry = new SchemaRegistry(db);
		const collections = await registry.listCollections();
		for (const collection of collections) {
			const fields = await registry.listFields(collection.id);
			const fieldMap = /* @__PURE__ */ new Map();
			for (const field of fields) fieldMap.set(field.slug, { type: field.type });
			result.set(collection.slug, {
				slug: collection.slug,
				fields: fieldMap
			});
		}
	} catch (error) {
		console.warn("Could not fetch schema registry:", error);
	}
	return result;
}

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