// Re-export stream types from the shared lightweight module.
// This allows the example package to import stream schemas without pulling the full types.ts dependency graph.
export { UI_STREAM_HOST_CONTEXT_KEY, UI_STREAM_REQUEST_META_KEY, UI_STREAM_RESULT_PATCH_METHOD, SERVER_STREAM_RESULT_PATCH_METHOD, UI_STREAM_STRUCTURED_CONTENT_KEY, uiStreamModeSchema, visualizationStreamPhaseSchema, visualizationStreamFrameTypeSchema, visualizationStreamStatusSchema, uiStreamHostContextSchema, visualizationStreamEnvelopeSchema, uiStreamCallToolResultSchema, uiStreamResultPatchNotificationSchema, serverStreamResultPatchNotificationSchema, getUiStreamHostContext, getVisualizationStreamEnvelope, } from "./ui-stream-types.js";
/**
 * Extract prompt text from either legacy MCP UI message shapes or native AppBridge user messages.
 */
export function extractUiPromptText(params) {
    if (params.type === "prompt" || params.prompt) {
        const prompt = params.prompt ?? String(params.message ?? "");
        return prompt || undefined;
    }
    if (params.role === "user" && Array.isArray(params.content)) {
        const text = params.content
            .map((block) => (block && typeof block === "object" && "text" in block ? String(block.text ?? "") : ""))
            .filter(Boolean)
            .join("\n\n");
        return text || undefined;
    }
    return undefined;
}
/**
 * Parse a canonical named UI handoff encoded as `intent\n{json}`.
 */
export function parseUiPromptHandoff(prompt) {
    const newlineIndex = prompt.indexOf("\n");
    if (newlineIndex <= 0) {
        return undefined;
    }
    const intent = prompt.slice(0, newlineIndex).trim();
    const payloadText = prompt.slice(newlineIndex + 1).trim();
    if (!intent || !payloadText) {
        return undefined;
    }
    if (!/^[A-Za-z][A-Za-z0-9_-]*$/.test(intent)) {
        return undefined;
    }
    try {
        const parsed = JSON.parse(payloadText);
        if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
            return undefined;
        }
        return {
            intent,
            params: parsed,
            raw: prompt,
        };
    }
    catch {
        return undefined;
    }
}
/**
 * Get server prefix based on tool prefix mode.
 */
export function getServerPrefix(serverName, mode) {
    if (mode === "none")
        return "";
    if (mode === "short") {
        let short = serverName.replace(/-?mcp$/i, "").replace(/-/g, "_");
        if (!short)
            short = "mcp";
        return short;
    }
    return serverName.replace(/-/g, "_");
}
/**
 * Format a tool name with server prefix.
 */
export function formatToolName(toolName, serverName, prefix) {
    const p = getServerPrefix(serverName, prefix);
    return p ? `${p}_${toolName}` : toolName;
}
function normalizeToolName(value) {
    return value.replace(/-/g, "_");
}
export function isToolExcluded(toolName, serverName, prefix, excludeTools) {
    if (!Array.isArray(excludeTools) || excludeTools.length === 0)
        return false;
    const candidates = new Set([
        normalizeToolName(toolName),
        normalizeToolName(formatToolName(toolName, serverName, prefix)),
        normalizeToolName(formatToolName(toolName, serverName, "server")),
        normalizeToolName(formatToolName(toolName, serverName, "short")),
    ]);
    for (const excluded of excludeTools) {
        if (typeof excluded !== "string")
            continue;
        if (candidates.has(normalizeToolName(excluded))) {
            return true;
        }
    }
    return false;
}
