import { init_string_coerce, normalizeOptionalString } from "../utils/string-coerce.js";
import { z } from "zod";
//#region src/mcp/channel-shared.ts
init_string_coerce();
function normalizeMessageChannel(value) {
	return value?.trim() || void 0;
}
const ClaudePermissionRequestSchema = z.object({
	method: z.literal("notifications/claude/channel/permission_request"),
	params: z.object({
		request_id: z.string(),
		tool_name: z.string(),
		description: z.string(),
		input_preview: z.string()
	})
});
function resolveMessageId(entry) {
	return normalizeOptionalString(entry.id) ?? (entry.__openclaw && typeof entry.__openclaw === "object" ? normalizeOptionalString(entry.__openclaw.id) : void 0);
}
function summarizeResult(label, count) {
	return { content: [{
		type: "text",
		text: `${label}: ${count}`
	}] };
}
function resolveConversationChannel(row) {
	return normalizeMessageChannel(normalizeOptionalString(row.deliveryContext?.channel) ?? normalizeOptionalString(row.lastChannel) ?? normalizeOptionalString(row.channel) ?? normalizeOptionalString(row.origin?.provider));
}
function toConversation(row) {
	const channel = resolveConversationChannel(row);
	const to = normalizeOptionalString(row.deliveryContext?.to) ?? normalizeOptionalString(row.lastTo);
	if (!channel || !to) return null;
	return {
		sessionKey: row.key,
		channel,
		to,
		accountId: normalizeOptionalString(row.deliveryContext?.accountId) ?? normalizeOptionalString(row.lastAccountId) ?? normalizeOptionalString(row.origin?.accountId),
		threadId: row.deliveryContext?.threadId ?? row.lastThreadId ?? row.origin?.threadId,
		label: normalizeOptionalString(row.label),
		displayName: normalizeOptionalString(row.displayName),
		derivedTitle: normalizeOptionalString(row.derivedTitle),
		lastMessagePreview: normalizeOptionalString(row.lastMessagePreview),
		updatedAt: typeof row.updatedAt === "number" ? row.updatedAt : null
	};
}
function matchEventFilter(event, filter) {
	if (event.cursor <= filter.afterCursor) return false;
	if (!filter.sessionKey) return true;
	return "sessionKey" in event && event.sessionKey === filter.sessionKey;
}
function extractAttachmentsFromMessage(message) {
	if (!message || typeof message !== "object") return [];
	const content = message.content;
	if (!Array.isArray(content)) return [];
	return content.filter((entry) => {
		if (!entry || typeof entry !== "object") return false;
		return normalizeOptionalString(entry.type) !== "text";
	});
}
function normalizeApprovalId(value) {
	const id = normalizeOptionalString(value);
	return id ? id.trim() : void 0;
}
//#endregion
export { ClaudePermissionRequestSchema, extractAttachmentsFromMessage, matchEventFilter, normalizeApprovalId, resolveConversationChannel, resolveMessageId, summarizeResult, toConversation, normalizeOptionalString as toText };

//# sourceMappingURL=channel-shared.js.map