import crypto from "node:crypto";
import fs from "node:fs";
import { sseEmitter } from "./sse/emitter.js";
import { guessMimeType } from "./http/handlers/files.js";
const DISCOVERY = {
    actions: ["send", "channel-info", "channel-list"],
    capabilities: ["text", "media"],
};
const CHANNEL_INFO_RESPONSE = {
    ok: true,
    channels: [{ id: "friday-next", name: "Friday Next", transport: "http+sse" }],
};
export function describeMessageActions() {
    return DISCOVERY;
}
function pickString(params, keys) {
    for (const k of keys) {
        const v = params[k];
        if (typeof v === "string" && v.trim())
            return v.trim();
    }
    return "";
}
async function readMediaFile(mediaPath, ctx) {
    if (ctx.mediaReadFile) {
        try {
            const buffer = await ctx.mediaReadFile(mediaPath);
            if (buffer?.length) {
                return { buffer, mimeType: guessMimeType(mediaPath) };
            }
        }
        catch { /* fall through */ }
    }
    try {
        const buffer = fs.readFileSync(mediaPath);
        return { buffer, mimeType: guessMimeType(mediaPath) };
    }
    catch {
        return null;
    }
}
async function handleSend(ctx) {
    const to = pickString(ctx.params, ["to", "target"]).toUpperCase();
    const text = pickString(ctx.params, ["message", "text", "content"]);
    const mediaPath = pickString(ctx.params, ["media", "path", "filePath", "fileUrl"]);
    const caption = pickString(ctx.params, ["caption"]);
    if (!to) {
        return { ok: false, error: "Missing required param: to" };
    }
    const runId = crypto.randomUUID();
    const sessionKey = ctx.sessionKey ?? undefined;
    // Send text via SSE outbound
    if (text) {
        sseEmitter.broadcast({
            type: "outbound",
            data: {
                op: "text",
                ts: Date.now(),
                runId,
                deviceId: to,
                sessionKey,
                ctx: { text, to },
            },
        }, to, true);
    }
    // Send media via SSE outbound
    if (mediaPath) {
        const result = await readMediaFile(mediaPath, ctx);
        if (result) {
            const { saveMediaBuffer } = await import("openclaw/plugin-sdk/media-store");
            const saved = await saveMediaBuffer(result.buffer, result.mimeType, "inbound");
            if (saved.id) {
                const publicUrl = `/friday-next/files/${encodeURIComponent(saved.id)}`;
                sseEmitter.broadcast({
                    type: "outbound",
                    data: {
                        op: "media",
                        ts: Date.now(),
                        runId,
                        deviceId: to,
                        sessionKey,
                        audioAsVoice: false,
                        caption: caption || text,
                        mediaUrl: publicUrl,
                        ctx: { to, text: caption || text, originalMediaUrl: mediaPath },
                    },
                }, to, true);
            }
        }
    }
    return { ok: true, runId, to };
}
export async function handleMessageAction(ctx) {
    if (ctx.action === "channel-info" || ctx.action === "channel-list") {
        return CHANNEL_INFO_RESPONSE;
    }
    if (ctx.action === "send") {
        return handleSend(ctx);
    }
    return null;
}
