import { normalizeAgentTitle } from './chat-sync.js';
const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
/** Immediate state after Editor `new_chat` — until DOM poll catches auxiliary tab. */
export function patchStateForEditorNewChat(state, agentId, title) {
    const id = agentId.trim();
    const chatTitle = title?.trim() || 'New Agent';
    const tabs = (state.tabs ?? []).map((t) => ({ ...t, active: false }));
    const idx = tabs.findIndex((t) => (t.composerId || t.id) === id);
    const row = {
        id,
        composerId: id,
        title: chatTitle,
        active: true,
    };
    if (idx >= 0)
        tabs[idx] = { ...tabs[idx], ...row };
    else
        tabs.push(row);
    const composerIdByTitle = { ...(state.composerIdByTitle ?? {}) };
    composerIdByTitle[normalizeAgentTitle(chatTitle)] = id;
    return {
        activeComposerId: id,
        activeChatTitle: chatTitle,
        tabs,
        composerIdByTitle,
    };
}
export function isComposerUuid(id) {
    return !!id && UUID_RE.test(id);
}
