import "./run-with-concurrency-D4u3H0yp.js";
import "./config-DdDYLh4x.js";
import "./logger-fRdp2gns.js";
import "./paths-Dy1WwvWU.js";
import { i as resolveWhatsAppAccount } from "./accounts-rQH70u1k.js";
import "./plugins-BO8YQttz.js";
import { f as readStringParam, l as readReactionParams, o as jsonResult, r as createActionGate, t as ToolAuthorizationError } from "./common-CD5Vi3Sl.js";
import { t as resolveWhatsAppOutboundTarget } from "./resolve-outbound-target-CJ-6AHLq.js";
import "./image-ops-BYhM4_gH.js";
import "./github-copilot-token-N9Yy7V1a.js";
import "./path-alias-guards-C8JwvQKY.js";
import "./fs-safe-D18qO9vm.js";
import "./proxy-env-BRmaQl-K.js";
import "./tool-images-BXXYUdz8.js";
import "./fetch-guard-Ds8bscMO.js";
import "./local-roots-epaKbkJx.js";
import "./ir-CRDGdRHE.js";
import "./render-BZdJrUxT.js";
import "./tables-Cna82h_q.js";
import { r as sendReactionWhatsApp } from "./outbound-CGXOEKy4.js";

//#region src/agents/tools/whatsapp-target-auth.ts
function resolveAuthorizedWhatsAppOutboundTarget(params) {
	const account = resolveWhatsAppAccount({
		cfg: params.cfg,
		accountId: params.accountId
	});
	const resolution = resolveWhatsAppOutboundTarget({
		to: params.chatJid,
		allowFrom: account.allowFrom ?? [],
		mode: "implicit"
	});
	if (!resolution.ok) throw new ToolAuthorizationError(`WhatsApp ${params.actionLabel} blocked: chatJid "${params.chatJid}" is not in the configured allowFrom list for account "${account.accountId}".`);
	return {
		to: resolution.to,
		accountId: account.accountId
	};
}

//#endregion
//#region src/agents/tools/whatsapp-actions.ts
async function handleWhatsAppAction(params, cfg) {
	const action = readStringParam(params, "action", { required: true });
	const isActionEnabled = createActionGate(cfg.channels?.whatsapp?.actions);
	if (action === "react") {
		if (!isActionEnabled("reactions")) throw new Error("WhatsApp reactions are disabled.");
		const chatJid = readStringParam(params, "chatJid", { required: true });
		const messageId = readStringParam(params, "messageId", { required: true });
		const { emoji, remove, isEmpty } = readReactionParams(params, { removeErrorMessage: "Emoji is required to remove a WhatsApp reaction." });
		const participant = readStringParam(params, "participant");
		const accountId = readStringParam(params, "accountId");
		const fromMeRaw = params.fromMe;
		const fromMe = typeof fromMeRaw === "boolean" ? fromMeRaw : void 0;
		const resolved = resolveAuthorizedWhatsAppOutboundTarget({
			cfg,
			chatJid,
			accountId,
			actionLabel: "reaction"
		});
		const resolvedEmoji = remove ? "" : emoji;
		await sendReactionWhatsApp(resolved.to, messageId, resolvedEmoji, {
			verbose: false,
			fromMe,
			participant: participant ?? void 0,
			accountId: resolved.accountId
		});
		if (!remove && !isEmpty) return jsonResult({
			ok: true,
			added: emoji
		});
		return jsonResult({
			ok: true,
			removed: true
		});
	}
	throw new Error(`Unsupported WhatsApp action: ${action}`);
}

//#endregion
export { handleWhatsAppAction };