import { n as PLUGIN_CAPABILITIES, t as HOOK_NAMES } from "../manifest-schema-BsXINkQD.mjs";

//#region src/plugins/adapt-sandbox-entry.ts
/**
* Default hook configuration values
*/
const DEFAULT_PRIORITY = 100;
const DEFAULT_TIMEOUT = 5e3;
const DEFAULT_ERROR_POLICY = "abort";
/**
* Check if a standard hook entry is a config object (has a `handler` property)
*/
function isHookConfig(entry) {
	return typeof entry === "object" && entry !== null && "handler" in entry;
}
/**
* Resolve a single standard hook entry to a ResolvedHook.
*
* Standard-format hooks use the sandbox entry convention:
*   handler(event, ctx) -- two args
*
* The HookPipeline dispatch methods also call handlers with (event, ctx),
* so the handler is compatible as-is. We just need to wrap it for type safety.
*/
function resolveStandardHook(entry, pluginId) {
	if (isHookConfig(entry)) return {
		priority: entry.priority ?? DEFAULT_PRIORITY,
		timeout: entry.timeout ?? DEFAULT_TIMEOUT,
		dependencies: entry.dependencies ?? [],
		errorPolicy: entry.errorPolicy ?? DEFAULT_ERROR_POLICY,
		exclusive: entry.exclusive ?? false,
		handler: entry.handler,
		pluginId
	};
	return {
		priority: DEFAULT_PRIORITY,
		timeout: DEFAULT_TIMEOUT,
		dependencies: [],
		errorPolicy: DEFAULT_ERROR_POLICY,
		exclusive: false,
		handler: entry,
		pluginId
	};
}
const VALID_CAPABILITIES_SET = new Set(PLUGIN_CAPABILITIES);
const VALID_HOOK_NAMES_SET = new Set(HOOK_NAMES);
/**
* Adapt a standard-format plugin definition into a ResolvedPlugin.
*
* This is the core of the unified plugin format. It takes the `{ hooks, routes }`
* export from a standard plugin and produces a ResolvedPlugin that can enter the
* HookPipeline alongside native plugins.
*
* @param definition - The standard plugin definition (from definePlugin() or raw export)
* @param descriptor - The plugin descriptor with id, version, capabilities, etc.
* @returns A ResolvedPlugin compatible with HookPipeline
*/
function adaptSandboxEntry(definition, descriptor) {
	const pluginId = descriptor.id;
	const version = descriptor.version;
	const resolvedHooks = {};
	if (definition.hooks) for (const [hookName, entry] of Object.entries(definition.hooks)) {
		if (!VALID_HOOK_NAMES_SET.has(hookName)) throw new Error(`Plugin "${pluginId}" declares unknown hook "${hookName}". Valid hooks: ${[...VALID_HOOK_NAMES_SET].join(", ")}`);
		resolvedHooks[hookName] = resolveStandardHook(entry, pluginId);
	}
	const resolvedRoutes = {};
	if (definition.routes) for (const [routeName, routeEntry] of Object.entries(definition.routes)) {
		const standardHandler = routeEntry.handler;
		resolvedRoutes[routeName] = {
			input: routeEntry.input,
			public: routeEntry.public,
			handler: async (ctx) => {
				const routeCtx = {
					input: ctx.input,
					request: ctx.request,
					requestMeta: ctx.requestMeta
				};
				const { input: _, request: __, requestMeta: ___, ...pluginCtx } = ctx;
				return standardHandler(routeCtx, pluginCtx);
			}
		};
	}
	const rawCapabilities = descriptor.capabilities ?? [];
	for (const cap of rawCapabilities) if (!VALID_CAPABILITIES_SET.has(cap)) throw new Error(`Invalid capability "${cap}" in plugin "${pluginId}". Valid capabilities: ${[...VALID_CAPABILITIES_SET].join(", ")}`);
	const capabilities = [...rawCapabilities];
	const allowedHosts = descriptor.allowedHosts ?? [];
	if (capabilities.includes("write:content") && !capabilities.includes("read:content")) capabilities.push("read:content");
	if (capabilities.includes("write:media") && !capabilities.includes("read:media")) capabilities.push("read:media");
	if (capabilities.includes("network:fetch:any") && !capabilities.includes("network:fetch")) capabilities.push("network:fetch");
	const rawStorage = descriptor.storage ?? {};
	const storage = {};
	for (const [name, config] of Object.entries(rawStorage)) storage[name] = {
		indexes: config.indexes ?? [],
		uniqueIndexes: config.uniqueIndexes
	};
	const admin = {};
	if (descriptor.adminPages) admin.pages = descriptor.adminPages;
	if (descriptor.adminWidgets) admin.widgets = descriptor.adminWidgets;
	return {
		id: pluginId,
		version,
		capabilities,
		allowedHosts,
		storage,
		hooks: resolvedHooks,
		routes: resolvedRoutes,
		admin
	};
}

//#endregion
export { adaptSandboxEntry };
//# sourceMappingURL=adapt-sandbox-entry.mjs.map