import { n as loadPluginManifestRegistry } from "./manifest-registry-C9qOP1PN.js";
import { a as loadConfig } from "./io-B_JNqEVl.js";
import { g as shouldPreferNativeJiti, h as resolvePluginSdkScopedAliasMap, m as resolvePluginSdkAliasFile, n as buildPluginLoaderJitiOptions } from "./sdk-alias-Czt6OgXf.js";
import "./config-BR67jvKt.js";
import { n as loadWebMediaRaw$1, o as getDefaultLocalRoots$1, r as optimizeImageToJpeg$1, t as loadWebMedia$1 } from "./web-media-C2olOd0h.js";
import fs from "node:fs";
import path from "node:path";
import { createJiti } from "jiti";
//#region src/plugins/runtime/runtime-plugin-boundary.ts
function readPluginBoundaryConfigSafely() {
	try {
		return loadConfig();
	} catch {
		return {};
	}
}
function resolvePluginRuntimeRecordByEntryBaseNames(entryBaseNames, onMissing) {
	const matches = loadPluginManifestRegistry({
		config: readPluginBoundaryConfigSafely(),
		cache: true
	}).plugins.filter((plugin) => {
		if (!plugin?.source) return false;
		const record = {
			rootDir: plugin.rootDir,
			source: plugin.source
		};
		return entryBaseNames.every((entryBaseName) => resolvePluginRuntimeModulePath(record, entryBaseName) !== null);
	});
	if (matches.length === 0) {
		if (onMissing) onMissing();
		return null;
	}
	if (matches.length > 1) {
		const pluginIds = matches.map((plugin) => plugin.id).join(", ");
		throw new Error(`plugin runtime boundary is ambiguous for entries [${entryBaseNames.join(", ")}]: ${pluginIds}`);
	}
	const record = matches[0];
	return {
		...record.origin ? { origin: record.origin } : {},
		rootDir: record.rootDir,
		source: record.source
	};
}
function resolvePluginRuntimeModulePath(record, entryBaseName, onMissing) {
	const candidates = [
		path.join(path.dirname(record.source), `${entryBaseName}.js`),
		path.join(path.dirname(record.source), `${entryBaseName}.ts`),
		...record.rootDir ? [path.join(record.rootDir, `${entryBaseName}.js`), path.join(record.rootDir, `${entryBaseName}.ts`)] : []
	];
	for (const candidate of candidates) if (fs.existsSync(candidate)) return candidate;
	if (onMissing) onMissing();
	return null;
}
function getPluginBoundaryJiti(modulePath, loaders) {
	const tryNative = shouldPreferNativeJiti(modulePath);
	const cached = loaders.get(tryNative);
	if (cached) return cached;
	const pluginSdkAlias = resolvePluginSdkAliasFile({
		srcFile: "root-alias.cjs",
		distFile: "root-alias.cjs",
		modulePath
	});
	const aliasMap = {
		...pluginSdkAlias ? {
			"kaijibot/plugin-sdk": pluginSdkAlias,
			"@kaijibot/plugin-sdk": pluginSdkAlias
		} : {},
		...resolvePluginSdkScopedAliasMap({ modulePath })
	};
	const loader = createJiti(import.meta.url, {
		...buildPluginLoaderJitiOptions(aliasMap),
		tryNative
	});
	loaders.set(tryNative, loader);
	return loader;
}
function loadPluginBoundaryModuleWithJiti(modulePath, loaders) {
	return getPluginBoundaryJiti(modulePath, loaders)(modulePath);
}
//#endregion
//#region src/plugins/runtime/runtime-web-channel-plugin.ts
let cachedHeavyModulePath = null;
let cachedHeavyModule = null;
let cachedLightModulePath = null;
let cachedLightModule = null;
const jitiLoaders = /* @__PURE__ */ new Map();
function resolveWebChannelPluginRecord() {
	return resolvePluginRuntimeRecordByEntryBaseNames(["light-runtime-api", "runtime-api"], () => {
		throw new Error("web channel plugin runtime is unavailable: missing plugin that provides light-runtime-api and runtime-api");
	});
}
function resolveWebChannelRuntimeModulePath(record, entryBaseName) {
	const modulePath = resolvePluginRuntimeModulePath(record, entryBaseName, () => {
		throw new Error(`web channel plugin runtime is unavailable: missing ${entryBaseName}`);
	});
	if (!modulePath) throw new Error(`web channel plugin runtime is unavailable: missing ${entryBaseName}`);
	return modulePath;
}
function loadCurrentHeavyModuleSync() {
	return loadPluginBoundaryModuleWithJiti(resolveWebChannelRuntimeModulePath(resolveWebChannelPluginRecord(), "runtime-api"), jitiLoaders);
}
function loadWebChannelLightModule() {
	const modulePath = resolveWebChannelRuntimeModulePath(resolveWebChannelPluginRecord(), "light-runtime-api");
	if (cachedLightModule && cachedLightModulePath === modulePath) return cachedLightModule;
	const loaded = loadPluginBoundaryModuleWithJiti(modulePath, jitiLoaders);
	cachedLightModulePath = modulePath;
	cachedLightModule = loaded;
	return loaded;
}
async function loadWebChannelHeavyModule() {
	const modulePath = resolveWebChannelRuntimeModulePath(resolveWebChannelPluginRecord(), "runtime-api");
	if (cachedHeavyModule && cachedHeavyModulePath === modulePath) return cachedHeavyModule;
	const loaded = loadPluginBoundaryModuleWithJiti(modulePath, jitiLoaders);
	cachedHeavyModulePath = modulePath;
	cachedHeavyModule = loaded;
	return loaded;
}
function getLightExport(exportName) {
	const value = loadWebChannelLightModule()[exportName];
	if (value == null) throw new Error(`web channel plugin runtime is missing export '${String(exportName)}'`);
	return value;
}
async function getHeavyExport(exportName) {
	const value = (await loadWebChannelHeavyModule())[exportName];
	if (value == null) throw new Error(`web channel plugin runtime is missing export '${String(exportName)}'`);
	return value;
}
function getActiveWebListener(...args) {
	return getLightExport("getActiveWebListener")(...args);
}
function getWebAuthAgeMs(...args) {
	return getLightExport("getWebAuthAgeMs")(...args);
}
function logWebSelfId(...args) {
	return getLightExport("logWebSelfId")(...args);
}
function loginWeb(...args) {
	return loadWebChannelHeavyModule().then((loaded) => loaded.loginWeb(...args));
}
function logoutWeb(...args) {
	return getLightExport("logoutWeb")(...args);
}
function readWebSelfId(...args) {
	return getLightExport("readWebSelfId")(...args);
}
function webAuthExists(...args) {
	return getLightExport("webAuthExists")(...args);
}
function sendWebChannelMessage(...args) {
	return loadWebChannelHeavyModule().then((loaded) => loaded.sendMessageWhatsApp(...args));
}
function sendWebChannelPoll(...args) {
	return loadWebChannelHeavyModule().then((loaded) => loaded.sendPollWhatsApp(...args));
}
function sendWebChannelReaction(...args) {
	return loadWebChannelHeavyModule().then((loaded) => loaded.sendReactionWhatsApp(...args));
}
function createRuntimeWebChannelLoginTool(...args) {
	return getLightExport("createWhatsAppLoginTool")(...args);
}
function createWebChannelSocket(...args) {
	return loadWebChannelHeavyModule().then((loaded) => loaded.createWaSocket(...args));
}
function formatError(...args) {
	return getLightExport("formatError")(...args);
}
function getStatusCode(...args) {
	return getLightExport("getStatusCode")(...args);
}
function pickWebChannel(...args) {
	return getLightExport("pickWebChannel")(...args);
}
function resolveWebChannelAuthDir() {
	return getLightExport("WA_WEB_AUTH_DIR");
}
async function handleWebChannelAction(...args) {
	return (await getHeavyExport("handleWhatsAppAction"))(...args);
}
async function loadWebMedia(...args) {
	return await loadWebMedia$1(...args);
}
async function loadWebMediaRaw(...args) {
	return await loadWebMediaRaw$1(...args);
}
function monitorWebChannel(...args) {
	return loadWebChannelHeavyModule().then((loaded) => loaded.monitorWebChannel(...args));
}
async function monitorWebInbox(...args) {
	return (await getHeavyExport("monitorWebInbox"))(...args);
}
async function optimizeImageToJpeg(...args) {
	return await optimizeImageToJpeg$1(...args);
}
async function runWebHeartbeatOnce(...args) {
	return (await getHeavyExport("runWebHeartbeatOnce"))(...args);
}
async function startWebLoginWithQr(...args) {
	return (await getHeavyExport("startWebLoginWithQr"))(...args);
}
async function waitForWebChannelConnection(...args) {
	return (await getHeavyExport("waitForWaConnection"))(...args);
}
async function waitForWebLogin(...args) {
	return (await getHeavyExport("waitForWebLogin"))(...args);
}
const extractMediaPlaceholder = (...args) => loadCurrentHeavyModuleSync().extractMediaPlaceholder(...args);
const extractText = (...args) => loadCurrentHeavyModuleSync().extractText(...args);
function getDefaultLocalRoots(...args) {
	return getDefaultLocalRoots$1(...args);
}
function resolveHeartbeatRecipients(...args) {
	return loadCurrentHeavyModuleSync().resolveHeartbeatRecipients(...args);
}
//#endregion
export { createRuntimeWebChannelLoginTool, createWebChannelSocket, extractMediaPlaceholder, extractText, formatError, getActiveWebListener, getDefaultLocalRoots, getStatusCode, getWebAuthAgeMs, handleWebChannelAction, loadWebMedia, loadWebMediaRaw, logWebSelfId, loginWeb, logoutWeb, monitorWebChannel, monitorWebInbox, optimizeImageToJpeg, pickWebChannel, readWebSelfId, resolveHeartbeatRecipients, resolveWebChannelAuthDir, runWebHeartbeatOnce, sendWebChannelMessage, sendWebChannelPoll, sendWebChannelReaction, startWebLoginWithQr, waitForWebChannelConnection, waitForWebLogin, webAuthExists };
