import { d as normalizeOutboundReplyPayload } from "./reply-payload-CPcXnHho.js";
import { r as dispatchChannelInboundTurn } from "./kernel-FrSJWC7d.js";
import { t as buildChannelInboundEventContext } from "./context-Chu5Lu77.js";
import { t as createChannelReplyPipeline } from "./reply-pipeline-C1IgE883.js";
import { a as resolveInboundRouteEnvelopeBuilderWithRuntime, r as resolveChannelInboundRouteEnvelope } from "./envelope-BtG0YVnA.js";
import "./direct-dm-access-Ck7wkHfC.js";
import "./direct-dm-guard-policy-BpvVTFWc.js";
//#region src/channels/direct-dm.ts
function buildDirectDmContext(params, route, body) {
	const accountId = route.accountId ?? params.accountId;
	return buildChannelInboundEventContext({
		channel: params.channel,
		accountId,
		provider: params.provider,
		surface: params.surface,
		messageId: params.messageId,
		messageIdFull: params.messageId,
		timestamp: params.timestamp,
		from: params.senderAddress,
		sender: {
			id: params.senderId,
			name: params.conversationLabel
		},
		conversation: {
			kind: "direct",
			id: params.peer.id,
			label: params.conversationLabel
		},
		route: {
			agentId: route.agentId,
			accountId: route.accountId,
			routeSessionKey: route.sessionKey,
			dispatchSessionKey: route.sessionKey
		},
		reply: {
			to: params.recipientAddress,
			originatingTo: params.originatingTo ?? params.recipientAddress
		},
		message: {
			body,
			bodyForAgent: params.bodyForAgent ?? params.rawBody,
			rawBody: params.rawBody,
			commandBody: params.commandBody ?? params.rawBody
		},
		access: { commands: { authorized: params.commandAuthorized === true } },
		extra: {
			NativeDirectUserId: params.peer.id,
			OriginatingChannel: params.originatingChannel ?? params.channel,
			...params.extraContext
		}
	});
}
async function dispatchInboundDirectDm(params) {
	const { route, buildEnvelope } = resolveChannelInboundRouteEnvelope({
		cfg: params.cfg,
		channel: params.channel,
		accountId: params.accountId,
		peer: params.peer
	});
	const ctxPayload = buildDirectDmContext(params, route, buildEnvelope({
		channel: params.channelLabel,
		from: params.conversationLabel,
		body: params.rawBody,
		timestamp: params.timestamp
	}));
	await dispatchChannelInboundTurn(buildDirectDmTurnPlan(params, route, ctxPayload));
	return {
		route,
		ctxPayload
	};
}
function buildDirectDmTurnPlan(params, route, ctxPayload) {
	const { onModelSelected, ...replyPipeline } = createChannelReplyPipeline({
		cfg: params.cfg,
		agentId: route.agentId,
		channel: params.channel,
		accountId: route.accountId ?? params.accountId
	});
	return {
		cfg: params.cfg,
		channel: params.channel,
		accountId: route.accountId ?? params.accountId,
		route: {
			agentId: route.agentId,
			sessionKey: route.sessionKey
		},
		ctxPayload,
		record: { onRecordError: params.onRecordError },
		delivery: {
			deliver: async (payload) => await params.deliver(normalizeOutboundReplyPayload(payload)),
			onError: params.onDispatchError
		},
		replyPipeline,
		replyOptions: { onModelSelected }
	};
}
async function dispatchInboundDirectDmWithRuntime(params) {
	const { route, buildEnvelope } = resolveInboundRouteEnvelopeBuilderWithRuntime({
		cfg: params.cfg,
		channel: params.channel,
		accountId: params.accountId,
		peer: params.peer,
		runtime: params.runtime.channel,
		sessionStore: params.cfg.session?.store
	});
	const { storePath, body } = buildEnvelope({
		channel: params.channelLabel,
		from: params.conversationLabel,
		body: params.rawBody,
		timestamp: params.timestamp
	});
	const ctxPayload = params.runtime.channel.reply.finalizeInboundContext({
		Body: body,
		BodyForAgent: params.bodyForAgent ?? params.rawBody,
		RawBody: params.rawBody,
		CommandBody: params.commandBody ?? params.rawBody,
		From: params.senderAddress,
		To: params.recipientAddress,
		SessionKey: route.sessionKey,
		AccountId: route.accountId ?? params.accountId,
		ChatType: "direct",
		ConversationLabel: params.conversationLabel,
		SenderId: params.senderId,
		Provider: params.provider ?? params.channel,
		Surface: params.surface ?? params.channel,
		MessageSid: params.messageId,
		MessageSidFull: params.messageId,
		Timestamp: params.timestamp,
		CommandAuthorized: params.commandAuthorized,
		...params.inboundAccessAuthorized === true ? { InboundAccessAuthorized: true } : {},
		OriginatingChannel: params.originatingChannel ?? params.channel,
		OriginatingTo: params.originatingTo ?? params.recipientAddress,
		NativeDirectUserId: params.peer.id,
		...params.extraContext
	});
	const plan = buildDirectDmTurnPlan(params, route, ctxPayload);
	await params.runtime.channel.inbound.run({
		channel: params.channel,
		accountId: route.accountId ?? params.accountId,
		raw: ctxPayload,
		adapter: {
			ingest: () => ({
				id: params.messageId,
				timestamp: params.timestamp,
				rawText: params.rawBody,
				textForAgent: params.bodyForAgent,
				textForCommands: params.commandBody,
				raw: ctxPayload
			}),
			resolveTurn: () => plan
		}
	});
	return {
		route,
		storePath,
		ctxPayload
	};
}
//#endregion
export { dispatchInboundDirectDmWithRuntime as n, dispatchInboundDirectDm as t };
