import { t as DEFAULT_ACCOUNT_ID } from "./session-key-BSfKMcoD.js";
import { i as formatCliCommand } from "./env-CNGYrGXP.js";
import { r as markdownToIRWithMeta } from "./ir-DmyUuTXh.js";
import { t as renderMarkdownWithMarkers } from "./render-D8ijUhi5.js";

//#region src/markdown/tables.ts
const MARKDOWN_STYLE_MARKERS = {
	bold: {
		open: "**",
		close: "**"
	},
	italic: {
		open: "_",
		close: "_"
	},
	strikethrough: {
		open: "~~",
		close: "~~"
	},
	code: {
		open: "`",
		close: "`"
	},
	code_block: {
		open: "```\n",
		close: "```"
	}
};
function convertMarkdownTables(markdown, mode) {
	if (!markdown || mode === "off") return markdown;
	const { ir, hasTables } = markdownToIRWithMeta(markdown, {
		linkify: false,
		autolink: false,
		headingStyle: "none",
		blockquotePrefix: "",
		tableMode: mode
	});
	if (!hasTables) return markdown;
	return renderMarkdownWithMarkers(ir, {
		styleMarkers: MARKDOWN_STYLE_MARKERS,
		escapeText: (text) => text,
		buildLink: (link, text) => {
			const href = link.href.trim();
			if (!href) return null;
			if (!text.slice(link.start, link.end)) return null;
			return {
				start: link.start,
				end: link.end,
				open: "[",
				close: `](${href})`
			};
		}
	});
}

//#endregion
//#region src/web/active-listener.ts
const STATE_KEY = Symbol.for("minion.activeWebListeners");
const state = (() => {
	const g = globalThis;
	if (!g[STATE_KEY]) g[STATE_KEY] = {
		listeners: /* @__PURE__ */ new Map(),
		currentListener: null
	};
	return g[STATE_KEY];
})();
function resolveWebAccountId(accountId) {
	return (accountId ?? "").trim() || DEFAULT_ACCOUNT_ID;
}
function requireActiveWebListener(accountId) {
	const id = resolveWebAccountId(accountId);
	const listener = state.listeners.get(id) ?? null;
	if (!listener) throw new Error(`No active WhatsApp Web listener (account: ${id}). Start the gateway, then link WhatsApp with: ${formatCliCommand(`minion channels login --channel whatsapp --account ${id}`)}.`);
	return {
		accountId: id,
		listener
	};
}
function setActiveWebListener(accountIdOrListener, maybeListener) {
	const { accountId, listener } = typeof accountIdOrListener === "string" ? {
		accountId: accountIdOrListener,
		listener: maybeListener ?? null
	} : {
		accountId: DEFAULT_ACCOUNT_ID,
		listener: accountIdOrListener ?? null
	};
	const id = resolveWebAccountId(accountId);
	if (!listener) state.listeners.delete(id);
	else state.listeners.set(id, listener);
	if (id === DEFAULT_ACCOUNT_ID) state.currentListener = listener;
}
function getActiveWebListener(accountId) {
	const id = resolveWebAccountId(accountId);
	return state.listeners.get(id) ?? null;
}

//#endregion
export { convertMarkdownTables as i, requireActiveWebListener as n, setActiveWebListener as r, getActiveWebListener as t };