import type { Message } from "@langchain/langgraph-sdk";

import type { AgentThread } from "./types";

export interface ThreadQueryIntentInput {
  pathThreadId?: string | null;
  queryThreadId?: string | null;
  isNewRoute?: boolean;
}

export interface ThreadQueryIntent {
  threadId: string | undefined;
  isNewThread: boolean;
  showWelcomeStyle: boolean;
}

export function pathOfThread(threadId: string) {
  return `/workspace/chats/${threadId}`;
}

function normalizeThreadId(value?: string | null): string | undefined {
  if (!value) {
    return undefined;
  }
  const trimmed = value.trim();
  if (!trimmed || trimmed === "new") {
    return undefined;
  }
  return trimmed;
}

export function textOfMessage(message: Message) {
  if (typeof message.content === "string") {
    return message.content;
  } else if (Array.isArray(message.content)) {
    for (const part of message.content) {
      if (part.type === "text") {
        return part.text;
      }
    }
  }
  return null;
}

export function titleOfThread(thread: AgentThread) {
  return thread.values?.title ?? "Untitled";
}
