import { o as getActivePluginRegistry, t as getActivePluginChannelRegistry } from "./runtime-BaqZ-OEG.js";
import { n as getLoadedChannelPlugin, t as getChannelPlugin } from "./registry-D5RocGab.js";
import "./plugins-lWQny0c_.js";
import { i as normalizeMessageChannel, t as isDeliverableMessageChannel } from "./message-channel-normalize-DtCGllE7.js";
import "./message-channel-D6KA_-3u.js";
import { t as bootstrapOutboundChannelPlugin } from "./channel-bootstrap.runtime-Cj1zrBMS.js";
//#region src/infra/outbound/channel-resolution.ts
/** Normalizes a raw channel id and rejects non-deliverable/internal channels. */
function normalizeDeliverableOutboundChannel(raw) {
	const normalized = normalizeMessageChannel(raw);
	if (!normalized || !isDeliverableMessageChannel(normalized)) return;
	return normalized;
}
function maybeBootstrapChannelPlugin(params) {
	bootstrapOutboundChannelPlugin(params);
}
function resolveDirectFromRegistry(registry, channel) {
	if (!registry) return;
	for (const entry of registry.channels) {
		const plugin = entry?.plugin;
		if (plugin?.id === channel) return plugin;
	}
}
function messageAdapterCanSendText(message) {
	return typeof message?.send?.text === "function";
}
function resolveSendCapableMessageAdapter(plugin) {
	const message = plugin?.message;
	return messageAdapterCanSendText(message) ? message : void 0;
}
function channelPluginHasRuntimeOutboundSurface(plugin) {
	return Boolean(plugin?.outbound ?? resolveSendCapableMessageAdapter(plugin));
}
function resolveRuntimeOutboundPlugin(plugin) {
	return channelPluginHasRuntimeOutboundSurface(plugin) ? plugin : void 0;
}
function resolveRuntimeOutboundPluginCandidate(params) {
	if (channelPluginHasRuntimeOutboundSurface(params.loaded)) return params.loaded;
	if (params.runtime) return params.runtime;
	if (channelPluginHasRuntimeOutboundSurface(params.bundled)) return params.bundled;
	if (params.allowSetupShell) return params.loaded ?? params.setupFallback ?? params.bundled;
}
function resolveValueFromRuntimeRegistries(channel, resolveValue) {
	const channelRegistry = getActivePluginChannelRegistry();
	const channelPlugin = resolveDirectFromRegistry(channelRegistry, channel);
	if (channelPlugin) {
		const value = resolveValue(channelPlugin);
		if (value !== void 0) return value;
	}
	const activeRegistry = getActivePluginRegistry();
	if (activeRegistry && activeRegistry !== channelRegistry) {
		const activePlugin = resolveDirectFromRegistry(activeRegistry, channel);
		if (activePlugin) return resolveValue(activePlugin);
	}
}
function resolveDirectFromRuntimeRegistries(channel) {
	return resolveValueFromRuntimeRegistries(channel, (plugin) => plugin);
}
function resolveRuntimeOutboundPluginFromRuntimeRegistries(channel) {
	return resolveValueFromRuntimeRegistries(channel, resolveRuntimeOutboundPlugin);
}
/** Resolves a deliverable outbound channel plugin, optionally bootstrapping it. */
function resolveOutboundChannelPlugin(params) {
	const normalized = normalizeDeliverableOutboundChannel(params.channel);
	if (!normalized) return;
	const resolveLoaded = () => getLoadedChannelPlugin(normalized);
	const resolve = () => getChannelPlugin(normalized);
	const candidate = resolveRuntimeOutboundPluginCandidate({
		loaded: resolveLoaded(),
		runtime: resolveRuntimeOutboundPluginFromRuntimeRegistries(normalized),
		setupFallback: resolveDirectFromRuntimeRegistries(normalized),
		bundled: resolve(),
		allowSetupShell: params.allowBootstrap !== true
	});
	if (candidate) return candidate;
	if (params.allowBootstrap !== true) return;
	maybeBootstrapChannelPlugin({
		channel: normalized,
		cfg: params.cfg
	});
	return resolveRuntimeOutboundPluginCandidate({
		loaded: resolveLoaded(),
		runtime: resolveRuntimeOutboundPluginFromRuntimeRegistries(normalized),
		setupFallback: resolveDirectFromRuntimeRegistries(normalized),
		bundled: resolve()
	});
}
/** Resolves the message adapter for a deliverable outbound channel. */
function resolveOutboundChannelMessageAdapter(params) {
	const normalized = normalizeDeliverableOutboundChannel(params.channel);
	if (!normalized) return;
	const current = resolveSendCapableMessageAdapter(getLoadedChannelPlugin(normalized)) ?? resolveValueFromRuntimeRegistries(normalized, resolveSendCapableMessageAdapter) ?? resolveSendCapableMessageAdapter(getChannelPlugin(normalized));
	if (current || params.allowBootstrap !== true) return current;
	maybeBootstrapChannelPlugin({
		channel: normalized,
		cfg: params.cfg
	});
	return resolveSendCapableMessageAdapter(getLoadedChannelPlugin(normalized)) ?? resolveValueFromRuntimeRegistries(normalized, resolveSendCapableMessageAdapter) ?? resolveSendCapableMessageAdapter(getChannelPlugin(normalized));
}
//#endregion
export { resolveOutboundChannelMessageAdapter as n, resolveOutboundChannelPlugin as r, normalizeDeliverableOutboundChannel as t };
