/**
 * Session title utilities.
 *
 * Pure helpers for title sanitization and default-title detection.
 * Title generation now happens via the backend GraphQL mutation
 * `generateSessionTitle`, called from the landing frontend after the
 * first assistant turn completes. The CLI no longer has its own LLM
 * pipeline for titles — the landing→backend→CLI REST PATCH flow
 * handles both the rename and the cross-tab meta publish.
 */
const DEFAULT_TITLES = new Set(["New conversation", "", "Untitled"]);
export const SESSION_TITLE_DEFAULT = "New conversation";
/** True if `title` looks like the auto-generated default and is fair game. */
export function isDefaultTitle(title) {
    if (title == null)
        return true;
    return DEFAULT_TITLES.has(title.trim());
}
/**
 * Sanitize raw LLM output into a usable session title.
 * Returns `null` if nothing usable remains.
 */
export function sanitizeTitle(raw) {
    if (!raw)
        return null;
    // First non-empty line.
    const firstLine = raw
        .split(/\r?\n/)
        .map((l) => l.trim())
        .find((l) => l.length > 0);
    if (!firstLine)
        return null;
    let out = firstLine;
    // Strip surrounding quote pairs.
    const quotePairs = [
        ['"', '"'],
        ["'", "'"],
        ["\u201C", "\u201D"], // " "
        ["\u2018", "\u2019"], // ' '
        ["`", "`"],
    ];
    for (const [open, close] of quotePairs) {
        if (out.startsWith(open) && out.endsWith(close) && out.length >= 2) {
            out = out.slice(open.length, out.length - close.length).trim();
            break;
        }
    }
    // Drop trailing punctuation.
    out = out.replace(/[.,!?;:]+$/u, "").trim();
    if (!out || out.length > 80) {
        if (out.length > 80) {
            return out.slice(0, 80);
        }
        return null;
    }
    return out;
}
