import { n as getPluginRuntimeGatewayRequestScope } from "../registry-DBOuapU8.js";
import { n as handleGatewayRequest } from "../server-methods-BYonpyRt.js";
//#region src/plugin-sdk/gateway-method-runtime.ts
/**
* Dispatch a Gateway control-plane method from an authenticated plugin request scope.
*/
async function dispatchGatewayMethod(method, params, options) {
	const scope = getPluginRuntimeGatewayRequestScope();
	if (!scope?.context) {
		const pluginLabel = scope?.pluginId ? ` for plugin "${scope.pluginId}"` : "";
		throw new Error(`Gateway method dispatch requires a plugin gateway request scope with context${pluginLabel}.`);
	}
	let result;
	await handleGatewayRequest({
		req: {
			type: "req",
			id: `plugin-gw-${method}`,
			method,
			params: params ?? {}
		},
		client: scope.client,
		isWebchatConnect: scope.isWebchatConnect ?? (() => false),
		respond: (ok, payload, error) => {
			if (!result) result = {
				ok,
				payload,
				error
			};
		},
		context: scope.context
	});
	if (!result) return {
		ok: false,
		error: {
			code: "no_response",
			message: `Gateway method "${method}" completed without a response.`
		}
	};
	return {
		ok: result.ok,
		payload: result.payload,
		error: result.error ? {
			code: result.error.code ?? "unknown",
			message: result.error.message ?? "Unknown error"
		} : void 0
	};
}
//#endregion
export { dispatchGatewayMethod };
