import { normalizeTelegramDeliveryChatId } from "../../../extensions/telegram/src/delivery-chat-id.js";
import "../telegram/index.js";
import { normalizePayloadForPlugin } from "./normalize.js";
//#region src/channels/outbound/deliver.ts
async function deliverOutboundMessage(params) {
	const { cfg, plugin, processedMsg } = params;
	const outbound = plugin.outbound;
	if (!outbound) return void 0;
	const chatId = plugin.id === "telegram" && typeof processedMsg.chat_id === "string" ? normalizeTelegramDeliveryChatId(processedMsg.chat_id) : processedMsg.chat_id;
	const msg = {
		...processedMsg,
		chat_id: chatId
	};
	const normalizedPayload = normalizePayloadForPlugin(msg, plugin);
	const outboundMeta = msg.metadata && typeof msg.metadata === "object" ? msg.metadata : void 0;
	if (msg.type === "typing_on" || msg.type === "typing_off") {
		if (outbound.sendPayload) return outbound.sendPayload({
			cfg,
			to: chatId,
			text: msg.content ?? "",
			mediaUrl: msg.mediaUrl,
			threadId: msg.metadata?.threadId,
			replyToId: msg.replyToMessageId,
			accountId: msg.metadata?.accountId ?? void 0,
			silent: msg.silent,
			outboundMetadata: outboundMeta,
			payload: normalizedPayload
		});
		return;
	}
	const outboundCtx = {
		cfg,
		to: chatId,
		text: msg.content ?? "",
		mediaUrl: msg.mediaUrl,
		mediaType: msg.mediaType,
		threadId: msg.metadata?.threadId,
		replyToId: msg.replyToMessageId,
		accountId: msg.metadata?.accountId ?? void 0,
		silent: msg.silent,
		audioAsVoice: msg.audioAsVoice,
		outboundMetadata: outboundMeta
	};
	if (outbound.sendPayload) return outbound.sendPayload({
		...outboundCtx,
		payload: normalizedPayload
	});
	const hasMediaUrl = Boolean(msg.mediaUrl?.trim());
	if (!hasMediaUrl && outbound.sendText) return outbound.sendText(outboundCtx);
	if (hasMediaUrl && outbound.sendMedia) return outbound.sendMedia(outboundCtx);
	if (outbound.sendMedia) return outbound.sendMedia(outboundCtx);
	if (outbound.sendText) return outbound.sendText(outboundCtx);
	return {
		messageId: "",
		chatId,
		success: false,
		error: "No send method"
	};
}
//#endregion
export { deliverOutboundMessage };

//# sourceMappingURL=deliver.js.map