import { i18n } from "@lingui/core";

//#region \0rolldown/runtime.js
var __defProp = Object.defineProperty;
var __exportAll = (all, no_symbols) => {
	let target = {};
	for (var name in all) {
		__defProp(target, name, {
			get: all[name],
			enumerable: true
		});
	}
	if (!no_symbols) {
		__defProp(target, Symbol.toStringTag, { value: "Module" });
	}
	return target;
};

//#endregion
//#region src/lib/api/client.ts
const API_BASE = "/_emdash/api";
/**
* Fetch wrapper that adds the X-EmDash-Request CSRF protection header
* to all requests. All API calls should use this instead of raw fetch().
*/
function apiFetch(input, init) {
	const headers = new Headers(init?.headers);
	headers.set("X-EmDash-Request", "1");
	return fetch(input, {
		...init,
		headers
	});
}
/**
* Throw an error with the message from the API response body if available,
* falling back to a generic message. All API error responses use the shape
* `{ error: { code, message } }`.
*/
async function throwResponseError(res, fallback) {
	const body = await res.json().catch(() => ({}));
	let message;
	if (typeof body === "object" && body !== null && "error" in body) {
		const { error } = body;
		if (typeof error === "object" && error !== null && "message" in error) {
			const { message: errorMessage } = error;
			if (typeof errorMessage === "string") message = errorMessage;
		}
	}
	throw new Error(message || `${fallback}: ${res.statusText}`);
}
/**
* Parse an API response with the { data: T } envelope.
*
* Handles error responses via throwResponseError, then unwraps the data envelope.
* Replaces both bare `response.json()` and field-unwrap patterns.
*/
async function parseApiResponse(response, fallbackMessage = i18n._({
	id: "/3O5R/",
	message: "Request failed"
})) {
	if (!response.ok) await throwResponseError(response, fallbackMessage);
	return (await response.json()).data;
}
/**
* Fetch admin manifest
*/
async function fetchManifest() {
	return parseApiResponse(await apiFetch(`${API_BASE}/manifest`), i18n._({
		id: "6B9mcK",
		message: "Failed to fetch manifest"
	}));
}
/**
* Fetch auth mode (public endpoint — works without authentication).
* Used by the login page to determine which login UI to render.
*/
async function fetchAuthMode() {
	return parseApiResponse(await apiFetch(`${API_BASE}/auth/mode`), i18n._({
		id: "HS8GaZ",
		message: "Failed to fetch auth mode"
	}));
}

//#endregion
//#region src/lib/api/plugins.ts
/**
* Plugin management APIs
*/
var plugins_exports = /* @__PURE__ */ __exportAll({
	disablePlugin: () => disablePlugin,
	enablePlugin: () => enablePlugin,
	fetchPlugin: () => fetchPlugin,
	fetchPlugins: () => fetchPlugins
});
/**
* Fetch all plugins
*/
async function fetchPlugins() {
	return (await parseApiResponse(await apiFetch(`${API_BASE}/admin/plugins`), i18n._({
		id: "lKhv/y",
		message: "Failed to fetch plugins"
	}))).items;
}
/**
* Fetch a single plugin
*/
async function fetchPlugin(pluginId) {
	const response = await apiFetch(`${API_BASE}/admin/plugins/${pluginId}`);
	if (!response.ok) {
		if (response.status === 404) throw new Error(i18n._({
			id: "/8h8wp",
			message: "Plugin \"{pluginId}\" not found",
			values: { pluginId }
		}));
		await throwResponseError(response, i18n._({
			id: "5Je0bM",
			message: "Failed to fetch plugin"
		}));
	}
	return (await parseApiResponse(response, i18n._({
		id: "5Je0bM",
		message: "Failed to fetch plugin"
	}))).item;
}
/**
* Enable a plugin
*/
async function enablePlugin(pluginId) {
	return (await parseApiResponse(await apiFetch(`${API_BASE}/admin/plugins/${pluginId}/enable`, { method: "POST" }), i18n._({
		id: "K4UCXw",
		message: "Failed to enable plugin"
	}))).item;
}
/**
* Disable a plugin
*/
async function disablePlugin(pluginId) {
	return (await parseApiResponse(await apiFetch(`${API_BASE}/admin/plugins/${pluginId}/disable`, { method: "POST" }), i18n._({
		id: "GWYJp8",
		message: "Failed to disable plugin"
	}))).item;
}

//#endregion
export { plugins_exports as a, fetchAuthMode as c, throwResponseError as d, fetchPlugins as i, fetchManifest as l, enablePlugin as n, API_BASE as o, fetchPlugin as r, apiFetch as s, disablePlugin as t, parseApiResponse as u };
//# sourceMappingURL=plugins-G9Z9NQ7n.js.map