const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
export function isComposerUuid(id) {
    return !!id && UUID_RE.test(id);
}
export function isSyntheticTabId(id) {
    return !id || /^tab-\d+$/.test(id) || /^sidebar-\d+$/.test(id);
}
export function normalizeComposerTitle(title) {
    return title
        .trim()
        .replace(/\s+/g, ' ')
        .replace(/\d+\s*(?:s|m|h|d|w)\b/gi, '')
        .replace(/\d+[smhdw]$/i, '')
        .trim()
        .toLowerCase();
}
export function titlesAlign(a, b) {
    const na = normalizeComposerTitle(a);
    const nb = normalizeComposerTitle(b);
    if (!na || !nb)
        return false;
    if (na === nb)
        return true;
    if (na.length >= 8 && nb.length >= 8 && (na.includes(nb) || nb.includes(na)))
        return true;
    return false;
}
/** Learns sidebar title → composer UUID while user switches chats in Cursor. */
export class ComposerTitleIndex {
    byTitle = new Map();
    note(activeTabTitle, headerTitle, composerId) {
        if (!isComposerUuid(composerId))
            return;
        const tabT = activeTabTitle?.trim();
        const headT = headerTitle?.trim();
        if (tabT) {
            this.byTitle.set(normalizeComposerTitle(tabT), composerId);
        }
        if (tabT && headT && titlesAlign(tabT, headT)) {
            this.byTitle.set(normalizeComposerTitle(headT), composerId);
        }
    }
    lookup(title) {
        if (!title?.trim())
            return undefined;
        return this.byTitle.get(normalizeComposerTitle(title));
    }
    snapshot() {
        return Object.fromEntries(this.byTitle);
    }
}
