Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

61.2 KB
Raw Blame
/**
 * Notes Page JavaScript
 * Handles notes listing, creation, and management.
 */

// Global state
let notes = [];
let collections = [];
let currentFilter = {
    search: '',
    collection_id: ''
};
// Search mode — mirrors the library/history/news pages. HYBRID is the
// default and merges keyword + semantic results via the shared
// window.SemanticSearch.buildTieredResults (same tiering as those pages).
const SEARCH_MODES = Object.freeze({ HYBRID: 'hybrid', TEXT: 'text', SEMANTIC: 'semantic' });
let searchMode = SEARCH_MODES.HYBRID;
let searchAbortController = null;
// Monotonic id so a slow hybrid response can't overwrite a newer search.
let hybridSearchId = 0;
// Keyword-listing pagination. The list endpoint pages at NOTES_PAGE_SIZE;
// without a pager, notes past the first page were silently unreachable
// from the UI (the server returns `total` exactly so we can render this).
const NOTES_PAGE_SIZE = 100;
let notesTotal = 0;
// RAW count of rows the server has returned for the current keyword listing,
// used as the pagination cursor. This is deliberately SEPARATE from
// notes.length: id-dedup (below) drops re-served rows from the displayed
// list, so notes.length lags the server's true offset. Using notes.length as
// the offset made every subsequent window overlap the last, and a page that
// was entirely duplicates froze notes.length while leaving the pager open —
// wedging "Load more" forever. The raw cursor always advances by a full page.
let notesFetchedCount = 0;
// Whether the last keyword page the server returned was FULL (== page size).
// A short/empty page means the server is exhausted — the definitive
// termination signal. Needed because an offset shift can SKIP rows, so the
// raw cursor may never reach notesTotal; without this the pager would wedge
// open chasing rows the server will never return.
let notesLastPageFull = false;
let _loadMoreInProgress = false;
// Monotonic id bumped whenever the visible result set is about to be
// replaced (fresh load or mode change). A "Load more" append captures it
// before its fetch and discards the response if it changed meanwhile —
// otherwise a filter/search/mode change in flight would concatenate a
// stale unfiltered page onto the new result set. (Load-more runs outside
// loadNotes()'s AbortController, so it can't be cancelled the usual way.)
let notesListGeneration = 0;

// Multi-select / synthesis state
let selectMode = false;
let selectedNoteIds = new Set();
// Remember the last-used badge state so select-mode interactions, which
// re-render the same result set, don't drop the "% match" badges shown after a
// semantic/hybrid search.
let lastShowSimilarity = false;
let synthesizeModal;
const SYNTH_MIN = 2;
const SYNTH_MAX = 5;
// Guards the create-note POST against double-submit.
let _createNoteInProgress = false;
// Guards the "Ask your notes" run against double-submit. The Enter-key handler
// invokes askNotes() directly and the input is never disabled, so disabling the
// button alone can't prevent two quick Enter presses launching duplicate runs.
let _askInProgress = false;
// Guards the synthesis preview/create calls against double-submit. Both fire a
// multi-second LLM call; disabling one button alone wouldn't stop the user from
// clicking the other while a call is in flight.
let _synthesizeInProgress = false;

// DOM Elements
let notesGrid;
let notesEmpty;
let searchInput;
let collectionFilter;
let noteModal;
let searchModeBtn;
let searchModeMenu;
let searchModeLabel;
let searchNotice;

/**
 * Initialize the notes page
 */
document.addEventListener('DOMContentLoaded', function() {
    notesGrid = document.getElementById('ldr-notes-grid');
    notesEmpty = document.getElementById('ldr-notes-empty');
    searchInput = document.getElementById('ldr-notes-search');
    collectionFilter = document.getElementById('ldr-collection-filter');
    searchModeBtn = document.getElementById('notes-search-mode-btn');
    searchModeMenu = document.getElementById('notes-search-mode-menu');
    searchModeLabel = document.getElementById('notes-search-mode-label');
    searchNotice = document.getElementById('ldr-notes-search-notice');

    // Initialize Bootstrap modals
    noteModal = new bootstrap.Modal(document.getElementById('noteModal'));
    const synthModalEl = document.getElementById('synthesizeModal');
    if (synthModalEl) {
        synthesizeModal = new bootstrap.Modal(synthModalEl);
    }

    // Load data
    loadNotes();
    loadCollections();

    // Setup event listeners
    setupEventListeners();
});

/**
 * Setup event listeners
 */
// Map of data-action values → handler. Used by the delegated click
// dispatcher below; mirrors the pattern in note-detail.js so notes.html
// no longer carries inline onclick handlers (script-src CSP friendly).
// Note-card click handler: in select mode, intercept the anchor
// navigation and toggle selection instead. Outside select mode, fall
// through so the native <a href="/notes/{id}"> navigation runs — that's
// what gives the card keyboard support (Tab + Enter) and
// middle/ctrl-click "open in new tab" for free.
function handleNoteCardAction(el, event) {
    if (selectMode) {
        if (event) event.preventDefault();
        const noteId = el.dataset.noteId;
        if (noteId) toggleNoteSelection(noteId);
    }
    // else: let the native <a href> navigate
}

const ACTIONS = {
    'toggle-select-mode': toggleSelectMode,
    'create-new-note': createNewNote,
    'retry-load-notes': () => loadNotes(),
    'clear-selection': clearSelection,
    'open-synthesize-modal': openSynthesizeModal,
    'save-note': saveNote,
    'preview-synthesis': previewSynthesis,
    'create-synthesized-note': createSynthesizedNote,
    'open-note': handleNoteCardAction,
    'ask-notes': askNotes,
    'clear-collection-filter': clearCollectionFilter,
    'load-more-notes': loadMoreNotes,
};

/**
 * Clear an active collection filter and reload the full notes list.
 * Wired from the "No notes in this collection" empty state.
 */
function clearCollectionFilter() {
    currentFilter.collection_id = '';
    if (collectionFilter) collectionFilter.value = '';
    loadNotes();
}

function setupEventListeners() {
    // Search input with debounce
    let searchTimeout;
    searchInput.addEventListener('input', function() {
        clearTimeout(searchTimeout);
        searchTimeout = setTimeout(() => {
            currentFilter.search = this.value;
            loadNotes();
        // Text mode is a single server call; the AI modes add an embedding
        // round-trip, so debounce a little longer to avoid spamming it.
        }, searchMode === SEARCH_MODES.TEXT ? 300 : 500);
    });

    // Collection filter
    collectionFilter.addEventListener('change', function() {
        currentFilter.collection_id = this.value;
        loadNotes();
    });

    // "Ask your notes" — Enter submits the question.
    const askInput = document.getElementById('ldr-notes-ask-input');
    if (askInput) {
        askInput.addEventListener('keydown', (e) => {
            if (e.key === 'Enter') {
                e.preventDefault();
                askNotes();
            }
        });
    }

    // Search mode selector (Text / AI Hybrid / AI only)
    if (searchModeMenu) {
        searchModeMenu.addEventListener('click', function(e) {
            const item = e.target.closest('[data-mode]');
            if (!item) return;
            e.preventDefault();
            const mode = item.dataset.mode;
            if (!mode || mode === searchMode) return;
            setSearchMode(mode);
        });
    }

    // Delegated data-action dispatcher
    document.addEventListener('click', (e) => {
        const el = e.target.closest('[data-action]');
        if (!el) return;
        const fn = ACTIONS[el.dataset.action];
        if (fn) fn(el, e);
    });
}

// Per-mode UI strings for the selector button and the search placeholder.
const SEARCH_MODE_UI = Object.freeze({
    hybrid: { label: 'AI Hybrid', icon: 'fa-brain', placeholder: 'AI Hybrid: keywords + meaning...' },
    text: { label: 'Text Only', icon: 'fa-font', placeholder: 'Search notes by keyword...' },
    semantic: { label: 'AI Only', icon: 'fa-brain', placeholder: 'Search notes by meaning...' },
});

/**
 * Switch the active search mode (hybrid / text / semantic), update the
 * selector chrome, and re-run the current query in the new mode.
 */
function setSearchMode(mode) {
    if (!SEARCH_MODE_UI[mode]) return;
    searchMode = mode;
    const ui = SEARCH_MODE_UI[mode];

    // Mark the active dropdown item.
    if (searchModeMenu) {
        searchModeMenu.querySelectorAll('.dropdown-item').forEach((i) => {
            i.classList.toggle('active', i.dataset.mode === mode);
        });
    }
    // Update the button label + icon.
    if (searchModeLabel) searchModeLabel.textContent = ui.label;
    if (searchModeBtn) {
        const icon = searchModeBtn.querySelector('i');
        if (icon) icon.className = 'fas ' + ui.icon;
    }
    // Update the input placeholder.
    if (searchInput) searchInput.placeholder = ui.placeholder;
    // A mode change invalidates any in-flight hybrid merge.
    hybridSearchId++;
    clearSearchNotice();

    // Re-run so the visible results reflect the new mode immediately.
    loadNotes();
}

/** Show a small inline notice under the filters (e.g. AI unavailable). */
function showSearchNotice(message) {
    if (!searchNotice) return;
    searchNotice.textContent = message;
    searchNotice.style.display = 'block';
}

/** Hide the inline search notice. */
function clearSearchNotice() {
    if (!searchNotice) return;
    searchNotice.textContent = '';
    searchNotice.style.display = 'none';
}

/** Render the centered "searching…" spinner inside the grid. */
function showSearchLoading(message) {
    if (!notesGrid) return;
    notesGrid.replaceChildren();
    const wrap = document.createElement('div');
    wrap.className = 'ldr-semantic-search-loading';
    const spinner = document.createElement('div');
    spinner.className = 'ldr-spinner';
    const text = document.createElement('p');
    text.textContent = message || 'Searching...';
    wrap.append(spinner, text);
    notesGrid.appendChild(wrap);
    notesGrid.style.display = 'block';
    if (notesEmpty) notesEmpty.style.display = 'none';
}

/**
 * Render a load-failure state into the notes area, with a retry button.
 * Unlike a toast (which disappears), this replaces the grid content so a
 * failed load can't leave the initial page-load spinner spinning forever
 * or masquerade as the "No matching notes found" empty state.
 */
function showNotesLoadError(message) {
    if (!notesGrid || !notesEmpty) return;
    notesGrid.replaceChildren();
    notesGrid.style.display = 'none';
    const icon = document.createElement('i');
    icon.className = 'fas fa-exclamation-triangle ldr-notes-empty-icon';
    const heading = document.createElement('h3');
    heading.textContent = "Couldn't load notes";
    const text = document.createElement('p');
    text.textContent = message;
    const retryIcon = document.createElement('i');
    retryIcon.className = 'fas fa-rotate-right';
    const retryBtn = document.createElement('button');
    retryBtn.className = 'ldr-create-note-btn';
    retryBtn.dataset.action = 'retry-load-notes';
    retryBtn.append(retryIcon, ' Try again');
    // audited 2026-07-22: all children are createElement/textContent-built Nodes — replaceChildren() never parses HTML
    // bearer:disable javascript_lang_dangerous_insert_html
    notesEmpty.replaceChildren(icon, heading, text, retryBtn);
    notesEmpty.style.display = 'block';
}

/** Show / clear the inline notice under the "Ask your notes" box. */
function showAskNotice(message) {
    const el = document.getElementById('ldr-notes-ask-notice');
    if (!el) return;
    el.textContent = message;
    el.style.display = 'block';
}
function clearAskNotice() {
    const el = document.getElementById('ldr-notes-ask-notice');
    if (!el) return;
    el.textContent = '';
    el.style.display = 'none';
}

/**
 * "Ask your notes": start a normal research run pinned to the Notes
 * collection (search_engine=collection_<id>) and open the results page.
 * Pre-flight checks warn when there's nothing to search (no notes, or none
 * indexed yet) rather than launching a run that can't find anything.
 */
async function askNotes() {
    const input = document.getElementById('ldr-notes-ask-input');
    const btn = document.getElementById('ldr-notes-ask-btn');
    const query = (input && input.value || '').trim();
    if (!query) return;
    if (_askInProgress) return;
    clearAskNotice();
    _askInProgress = true;
    if (btn) btn.disabled = true;

    try {
        const ctxResp = await safeFetchWithAuth('/notes/api/notes/ask-context', {
            credentials: 'same-origin',
        });
        const ctx = await ctxResp.json();
        if (!ctx.success || !ctx.collection_id) {
            showAskNotice('Could not start chat with your notes.');
            return;
        }
        if (!ctx.note_count) {
            showAskNotice('You have no notes yet — create one first.');
            return;
        }
        if (!ctx.indexed_count) {
            showAskNotice('Your notes aren\'t indexed for AI search yet. Open a note and choose "Index for Search" first, then ask.');
            return;
        }

        // Start a normal research run, but restrict the search source to the
        // Notes collection. Model/mode/strategy come from the user's settings.
        const resp = await safeFetchWithAuth('/api/start_research', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json', 'X-CSRFToken': getCSRFToken() },
            body: JSON.stringify({
                query,
                search_engine: `collection_${ctx.collection_id}`,
                metadata: { source_type: 'notes_chat' },
            }),
            credentials: 'same-origin',
        });
        const data = await resp.json();
        // A start at the concurrent-research limit returns
        // {status: 'queued', research_id, ...} and the queue processor runs
        // it — that's a successful start, and the results page renders the
        // queued state (same contract as subscriptions.js / news.js).
        if ((data.status === 'success' || data.status === window.RESEARCH_STATUS.QUEUED) && data.research_id) {
            URLValidator.safeAssign(window.location, 'href', `/results/${data.research_id}`);
        } else {
            // /api/start_research reports failures as {status, message}.
            showAskNotice(data.message || data.error || 'Could not start research. Check that an LLM and a search engine are configured.');
        }
    } catch (error) {
        SafeLogger.error('Error asking notes:', error);
        showAskNotice('Could not start chat with your notes.');
    } finally {
        // eslint-disable-next-line require-atomic-updates -- single-writer guard cleared in finally
        _askInProgress = false;
        if (btn) btn.disabled = false;
    }
}

/**
 * Load notes from API
 */
async function loadNotes() {
    // Cancel any in-flight search request
    if (searchAbortController) {
        searchAbortController.abort();
    }
    const controller = new AbortController();
    searchAbortController = controller;
    // Invalidate any in-flight "Load more" append: it targeted the
    // result set we're about to replace.
    notesListGeneration++;

    try {
        // The AI modes need a query of at least 2 chars; below that (or in
        // TEXT mode) fall through to the plain keyword listing, which also
        // serves the "show everything" empty-query case.
        const hasQuery = currentFilter.search && currentFilter.search.length >= window.SemanticSearch.MIN_QUERY_LENGTH;
        if (searchMode === SEARCH_MODES.SEMANTIC && hasQuery) {
            await loadNotesSemanticSearch();
        } else if (searchMode === SEARCH_MODES.HYBRID && hasQuery) {
            await loadNotesHybridSearch();
        } else {
            await loadNotesKeywordSearch();
        }
    } catch (error) {
        if (error.name !== 'AbortError') {
            SafeLogger.error('Error in loadNotes:', error);
        }
    } finally {
        // Only clear the module-level controller if it is still ours. When
        // this call was superseded, a newer loadNotes() already installed
        // its own controller — nulling that here would break the next
        // call's cancellation and let a stale response overwrite newer
        // results.
        if (searchAbortController === controller) {
            searchAbortController = null;
        }
    }
}

/**
 * Load notes using keyword search (default)
 * @param {boolean} append - true for a "Load more" page: the fetched page
 *     is appended to the current list instead of replacing it.
 */
async function loadNotesKeywordSearch(append = false) {
    clearSearchNotice();
    // Capture the generation so a stale append (a filter/search/mode
    // change landed while this page was in flight) can be discarded.
    const myGen = notesListGeneration;
    try {
        const params = new URLSearchParams();
        if (currentFilter.search) {
            params.append('search', currentFilter.search);
        }
        if (currentFilter.collection_id) {
            params.append('collection_id', currentFilter.collection_id);
        }
        params.append('limit', String(NOTES_PAGE_SIZE));
        if (append) {
            // Raw server cursor, NOT notes.length (see notesFetchedCount).
            params.append('offset', String(notesFetchedCount));
        }

        const response = await safeFetchWithAuth(`/notes/api/notes?${params.toString()}`, {
            credentials: 'same-origin',
            signal: searchAbortController ? searchAbortController.signal : undefined,
        });

        const data = await response.json();

        // A newer load replaced (or is replacing) the result set this
        // append targeted — dropping it prevents concatenating a stale
        // unfiltered page onto the new list and re-showing the pager.
        if (append && myGen !== notesListGeneration) return;

        if (data.success) {
            // Offset pagination can re-serve a row the list already holds
            // when the ordering shifts mid-pagination (a note edited,
            // pinned, or created in another tab moves between offset
            // windows) — drop duplicates by id instead of rendering the
            // same card twice. Skipped rows are unavoidable with plain
            // offsets; duplicates are not.
            const rawPage = data.notes || [];
            const existingIds = append ? new Set(notes.map(n => n.id)) : null;
            const page = existingIds
                ? rawPage.filter(n => !existingIds.has(n.id))
                : rawPage;
            // eslint-disable-next-line require-atomic-updates -- appends are serialized by the _loadMoreInProgress guard + generation check; fresh loads replace wholesale
            notes = append ? notes.concat(page) : page;
            // Advance the cursor by the RAW page size (pre-dedup) so the next
            // offset is where the server actually left off; a fresh load resets
            // it. This is what makes the pager terminate: notesFetchedCount
            // reaches notesTotal even when dedup/skips keep notes.length lower.
            // eslint-disable-next-line require-atomic-updates -- serialized by the _loadMoreInProgress guard + generation check, like the sibling `notes` write
            notesFetchedCount = append
                ? notesFetchedCount + rawPage.length
                : rawPage.length;
            // A short page means the server has no more rows for this filter.
            notesLastPageFull = rawPage.length >= NOTES_PAGE_SIZE;
            // Fall back to the RAW cursor, not notes.length: if the server
            // ever omits `total`, keying the completion check off the deduped
            // notes.length would reintroduce the stuck/prematurely-closed
            // pager this cursor was introduced to prevent. notesFetchedCount
            // makes hasMore=false (nothing more known), closing cleanly.
            notesTotal = typeof data.total === 'number' ? data.total : notesFetchedCount;
            renderNotes(false);
            updateLoadMoreControl(true);
        } else if (append) {
            // A failed APPEND must keep the pages already on screen — the
            // in-memory list and grid are still valid. Toast + leave the
            // pager so the user can retry in place.
            SafeLogger.error('Failed to load more notes:', data.error);
            showNotesError('Failed to load more notes');
            updateLoadMoreControl(true);
        } else {
            SafeLogger.error('Failed to load notes:', data.error);
            showNotesError('Failed to load notes');
            // Replace the grid content too — on first page load it still
            // holds the template's hard-coded spinner, which would spin
            // forever once the toast fades (the semantic/hybrid siblings
            // already clear theirs in their catch blocks).
            showNotesLoadError('Check your connection and try again.');
            updateLoadMoreControl(false);
        }
    } catch (error) {
        if (error.name === 'AbortError') return;
        if (append && myGen !== notesListGeneration) return;
        SafeLogger.error('Error loading notes:', error);
        if (append) {
            // Same as above — never blank an already-loaded list on a
            // failed append.
            showNotesError('Failed to load more notes');
            updateLoadMoreControl(true);
            return;
        }
        showNotesError('Failed to load notes');
        showNotesLoadError('Check your connection and try again.');
        updateLoadMoreControl(false);
    }
}

/**
 * "Load more" pager for the keyword listing. Fetches the next page and
 * appends it. Only the keyword path paginates — the AI modes cap their
 * result sets by relevance instead.
 */
async function loadMoreNotes() {
    if (_loadMoreInProgress) return;
    // Refuse while a fresh load is in flight: it has already bumped the
    // generation, so an append started now would capture the NEW gen but
    // compute its offset from the OLD, not-yet-replaced list — passing the
    // guard later and concatenating a wrong-offset page. searchAbortController
    // is non-null exactly for the duration of a fresh loadNotes().
    if (searchAbortController) return;
    _loadMoreInProgress = true;
    const btn = document.querySelector('#ldr-notes-load-more button');
    if (btn) btn.disabled = true;
    try {
        await loadNotesKeywordSearch(true);
    } finally {
        // eslint-disable-next-line require-atomic-updates -- single-writer guard cleared in finally
        _loadMoreInProgress = false;
        if (btn) btn.disabled = false;
    }
}

/**
 * Toggle the load-more control. Visible only for the keyword listing and
 * only while more pages exist; the AI modes always hide it.
 */
function updateLoadMoreControl(keywordMode) {
    const container = document.getElementById('ldr-notes-load-more');
    if (!container) return;
    // Release any Load-more lock / disabled state still held by an abandoned,
    // uncancellable append from a PRIOR generation. This runs at the end of
    // every settled load, so by here the current generation's list is final
    // and the button must be clickable again. Without this, a fresh load that
    // supersedes an in-flight append (and resolves first) shows a valid pager
    // whose button stays stuck-disabled until that discarded network request
    // eventually completes. Safe for the normal loadMoreNotes append path too:
    // no await occurs between this call and loadMoreNotes's own finally, so it
    // cannot open a re-entrancy window.
    _loadMoreInProgress = false;
    const loadMoreBtn = container.querySelector('button');
    if (loadMoreBtn) loadMoreBtn.disabled = false;
    // Terminate on TWO signals: a short last page (server exhausted — closes
    // even when an offset shift skipped rows so the cursor never reaches
    // total), AND the raw cursor still short of total (closes the all-
    // duplicates case where full pages keep coming but every row is a dup).
    // Neither notes.length nor notesTotal alone is sufficient: dedup/skips
    // decouple notes.length from the cursor, and a shift can strand the
    // cursor below total.
    const hasMore =
        keywordMode && notesLastPageFull && notesFetchedCount < notesTotal;
    container.style.display = hasMore ? 'flex' : 'none';
    if (hasMore) {
        const label = document.getElementById('ldr-notes-load-more-label');
        if (label) {
            label.textContent = `Load more (showing ${notes.length} of ${notesTotal})`;
        }
    }
}

/**
 * Load notes using semantic search
 */
async function loadNotesSemanticSearch() {
    clearSearchNotice();
    updateLoadMoreControl(false);
    try {
        showSearchLoading('Searching by meaning...');

        const data = await fetchSemanticNotes(currentFilter.search);

        if (data.success) {
            notes = data.results || [];
            renderNotes(true); // Show similarity scores
        } else {
            // AI-only mode can't fall back silently — surface the failure.
            // An explicit error state (not the "No matching notes found"
            // empty state, which reads as a benign zero-match) plus the
            // toast, so the failure is visible after the toast fades.
            SafeLogger.error('Semantic search failed:', data.error);
            showNotesError('AI search failed — try Text or AI Hybrid mode');
            notes = [];
            showNotesLoadError('AI search failed — try Text or AI Hybrid mode.');
        }
    } catch (error) {
        if (error.name === 'AbortError') return;
        SafeLogger.error('Error in semantic search:', error);
        showNotesError('AI search failed — try Text or AI Hybrid mode');
        notes = [];
        showNotesLoadError('AI search failed — try Text or AI Hybrid mode.');
    }
}

/**
 * Fetch raw semantic-search results for a query. Shared by the AI-only
 * and AI-hybrid modes. Returns the parsed JSON envelope ({success, results}).
 */
async function fetchSemanticNotes(query) {
    const params = new URLSearchParams();
    params.append('q', query);
    params.append('limit', '20');
    params.append('min_similarity', String(window.SemanticSearch.MIN_SIMILARITY));
    // Honor the active collection filter: semantic + hybrid search
    // previously ignored it and returned notes from all collections.
    if (currentFilter.collection_id) {
        params.append('collection_id', currentFilter.collection_id);
    }

    const response = await safeFetchWithAuth(`/notes/api/notes/semantic-search?${params.toString()}`, {
        credentials: 'same-origin',
        // Share the keyword controller so a fresh search (or mode change)
        // cancels an in-flight request and its late response can't win.
        signal: searchAbortController ? searchAbortController.signal : undefined,
    });
    return response.json();
}

/**
 * AI Hybrid search: run keyword + semantic in parallel and merge them via
 * the shared window.SemanticSearch.buildTieredResults — the exact tiering
 * the library, history and news pages use. Tier 1 = matched by both
 * (top, ranked by similarity), Tier 2 = keyword-only (recency), Tier 3 =
 * semantic-only (ranked by similarity). Keyword is the base: if the AI
 * call fails or returns nothing, hybrid degrades to the keyword results
 * (by design, like the library — not a bug-masking fallback).
 */
async function loadNotesHybridSearch() {
    const myId = ++hybridSearchId;
    clearSearchNotice();
    updateLoadMoreControl(false);
    showSearchLoading('Searching keywords + meaning...');

    const signal = searchAbortController ? searchAbortController.signal : undefined;

    // Keyword leg — the base result set. Best-effort like the semantic
    // leg: a failure is recorded (not silently mapped to "no matches")
    // so the user can tell a broken text search from a zero-match one.
    let kwUnavailable = false;
    const kwParams = new URLSearchParams();
    kwParams.append('search', currentFilter.search);
    if (currentFilter.collection_id) kwParams.append('collection_id', currentFilter.collection_id);
    const kwPromise = safeFetchWithAuth(`/notes/api/notes?${kwParams.toString()}`, {
        credentials: 'same-origin',
        signal,
    })
        .then((r) => r.json())
        .then((d) => {
            if (d && d.success) return d.notes || [];
            kwUnavailable = true;
            return [];
        })
        .catch((err) => {
            if (err && err.name === 'AbortError') throw err;
            SafeLogger.error('Hybrid keyword leg failed:', err);
            kwUnavailable = true;
            return [];
        });

    // Semantic leg — best-effort. Swallow non-abort failures so the keyword
    // base still renders; record that AI was unavailable for the notice.
    let aiUnavailable = false;
    const semPromise = fetchSemanticNotes(currentFilter.search)
        .then((d) => {
            if (d && d.success) return d.results || [];
            aiUnavailable = true;
            return [];
        })
        .catch((err) => {
            if (err && err.name === 'AbortError') throw err;
            SafeLogger.error('Hybrid semantic leg failed:', err);
            aiUnavailable = true;
            return [];
        });

    let textResults;
    let semanticResults;
    try {
        [textResults, semanticResults] = await Promise.all([kwPromise, semPromise]);
    } catch (error) {
        if (error.name === 'AbortError') return;
        SafeLogger.error('Error in hybrid search:', error);
        // A newer search owns the grid now — don't clobber its state.
        if (myId !== hybridSearchId) return;
        showNotesError('Search failed');
        // Clear the "Searching keywords + meaning..." spinner left by
        // showSearchLoading — otherwise it spins forever (the semantic
        // sibling does the same in its catch).
        notes = [];
        renderNotes(true);
        return;
    }

    // A newer search (typing or a mode change) superseded this one.
    if (myId !== hybridSearchId) return;

    // Both legs down: there is nothing meaningful to render — show the
    // explicit error state (with retry) instead of a fake empty result.
    if (kwUnavailable && aiUnavailable) {
        notes = [];
        showNotesLoadError('Search failed — check your connection and try again.');
        return;
    }

    const merge = window.SemanticSearch && window.SemanticSearch.buildTieredResults;
    if (!merge) {
        // Shared component missing (load-order/dependency issue) — show the
        // keyword base rather than nothing, and log it loudly.
        SafeLogger.error('window.SemanticSearch.buildTieredResults unavailable; showing keyword results only');
        notes = textResults;
        renderNotes(false);
        return;
    }

    // The notes semantic endpoint returns its snippet as `content_preview`;
    // buildTieredResults reads `.snippet`, so alias it for Tier-1 enrichment.
    const normalizedSemantic = semanticResults.map(
        (r) => ({ ...r, snippet: r.content_preview || '' }),
    );

    const tiered = merge(textResults, normalizedSemantic, {
        textIdKey: 'id',
        semanticIdKey: 'id',
    });
    notes = mergeTieredNotes(tiered);

    if (aiUnavailable) {
        // Surface the AI failure whenever the semantic leg was unavailable —
        // including when the keyword leg also matched nothing. Pre-fix the
        // notice was gated on textResults.length > 0, so a failed AI search
        // with no keyword matches looked identical to "you have no matching
        // notes", silently hiding the failure.
        showSearchNotice(
            textResults.length > 0
                ? 'AI search unavailable — showing keyword matches only.'
                : 'AI search unavailable — no keyword matches found.'
        );
    } else if (kwUnavailable) {
        // Mirror image: keyword backend failed while AI succeeded. Without
        // this notice the render was indistinguishable from "your keywords
        // matched nothing" (semantic-only tiering), hiding the failure.
        showSearchNotice(
            notes.length > 0
                ? 'Text search unavailable — showing AI matches only.'
                : 'Text search unavailable — no AI matches found.'
        );
    }
    renderNotes(true);
}

/**
 * Flatten tiered hybrid results into a single ordered notes array.
 * Thin page-local wrapper over the shared
 * window.SemanticSearch.flattenTieredResults (the notes and
 * unified-search copies of this logic had drifted apart); the caller
 * already sits behind the buildTieredResults availability guard, which
 * ships in the same component file.
 */
function mergeTieredNotes(tiered) {
    return window.SemanticSearch.flattenTieredResults(tiered);
}

/**
 * Load collections for filter dropdown
 */
async function loadCollections() {
    try {
        const response = await safeFetchWithAuth('/library/api/collections', {
            credentials: 'same-origin'
        });

        const data = await response.json();

        if (data.success) {
            collections = data.collections || [];
            renderCollectionFilter();
        } else {
            // Don't silently leave the filter empty as if no collections exist.
            window.ui?.showMessage?.('Could not load collections — filtering by collection is unavailable.', 'warning');
        }
    } catch (error) {
        SafeLogger.error('Error loading collections:', error);
        // Surface the degraded feature instead of swallowing the failure.
        window.ui?.showMessage?.('Could not load collections — filtering by collection is unavailable.', 'warning');
    }
}

/**
 * Render collection filter dropdown
 */
function renderCollectionFilter() {
    const options = ['<option value="">All Collections</option>'];

    collections.forEach(coll => {
        options.push(`<option value="${escapeHtml(coll.id)}">${escapeHtml(coll.name)}</option>`);
    });

    // eslint-disable-next-line no-unsanitized/property -- audited 2026-03-29: all interpolations use escapeHtml
    collectionFilter.innerHTML = options.join('');
}

/**
 * Render notes grid
 * @param {boolean} showSimilarity - Whether to show similarity scores
 */
function renderNotes(showSimilarity = false) {
    lastShowSimilarity = showSimilarity;
    // Keep the selection scoped to the visible result set so the synthesis
    // preview and the submitted note_ids can't diverge from what the user sees
    // (and a now-hidden selection can't get stuck un-deselectable).
    if (selectMode && selectedNoteIds.size) {
        const visible = new Set(notes.map((n) => n.id));
        let pruned = false;
        for (const id of [...selectedNoteIds]) {
            if (!visible.has(id)) {
                selectedNoteIds.delete(id);
                pruned = true;
            }
        }
        if (pruned) updateSelectActions();
    }
    if (notes.length === 0) {
        // eslint-disable-next-line no-unsanitized/property -- audited 2026-03-29: pure hardcoded HTML, no user data
        notesGrid.innerHTML = '';
        notesGrid.style.display = 'none';

        // Show appropriate empty state
        if (currentFilter.search) {
            const hint = searchMode === SEARCH_MODES.TEXT
                ? 'Try a different search or switch to AI Hybrid mode'
                : 'Try a different search or switch to Text mode';
            // eslint-disable-next-line no-unsanitized/property -- audited 2026-05-25: hint is one of two hardcoded strings, no user data
            notesEmpty.innerHTML = `
                <i class="fas fa-search ldr-notes-empty-icon"></i>
                <h3>No matching notes found</h3>
                <p>${hint}</p>
            `;
        } else if (currentFilter.collection_id) {
            // A collection filter is active but the collection has no notes.
            // Don't show the first-run "Create your first note" copy — it's
            // misleading (the user has notes) and a note created from there
            // would not join the selected collection.
            // eslint-disable-next-line no-unsanitized/property -- audited 2026-06-10: pure hardcoded HTML, no user data
            notesEmpty.innerHTML = `
                <i class="fas fa-folder-open ldr-notes-empty-icon"></i>
                <h3>No notes in this collection</h3>
                <p>This collection doesn't contain any notes yet</p>
                <button class="ldr-create-note-btn" data-action="clear-collection-filter">
                    <i class="fas fa-times"></i> Clear filter
                </button>
            `;
        } else {
            // eslint-disable-next-line no-unsanitized/property -- audited 2026-03-29: pure hardcoded HTML, no user data
            notesEmpty.innerHTML = `
                <i class="fas fa-sticky-note ldr-notes-empty-icon"></i>
                <h3>No notes yet</h3>
                <p>Start capturing your ideas and research notes</p>
                <button class="ldr-create-note-btn" data-action="create-new-note">
                    <i class="fas fa-plus"></i> Create your first note
                </button>
            `;
        }
        notesEmpty.style.display = 'block';
        return;
    }

    notesGrid.style.display = 'grid';
    notesEmpty.style.display = 'none';

    const html = notes.map(note => createNoteCard(note, showSimilarity)).join('');
    // bearer:disable javascript_lang_dangerous_insert_html
    // eslint-disable-next-line no-unsanitized/property -- audited 2026-03-29: all interpolations use escapeHtml/numeric
    notesGrid.innerHTML = html;
}

/**
 * Create a note card HTML
 * @param {Object} note - Note data
 * @param {boolean} showSimilarity - Whether to show similarity score
 */
/**
 * Reduce markdown source to plain text for the card previews. Delegates
 * to the shared window.formatting.stripMarkdownToText — kept as a
 * page-local wrapper so the call sites and the __notesTest hook keep a
 * stable name without reintroducing the deferred shared-script
 * global-shadowing bug.
 */
function stripMarkdownForPreview(text) {
    return window.formatting.stripMarkdownToText(text);
}

function createNoteCard(note, showSimilarity = false) {
    // Use content_preview if available (semantic search), otherwise truncate content
    const rawPreview = note.content_preview || (note.content ? note.content.substring(0, 150) + (note.content.length > 150 ? '...' : '') : '');
    const preview = stripMarkdownForPreview(rawPreview);
    const tagsHtml = (note.tags || []).slice(0, 3).map(tag =>
        `<span class="ldr-note-tag">${escapeHtml(tag)}</span>`
    ).join('');

    const pinnedClass = note.pinned ? 'ldr-pinned' : '';
    const date = note.updated_at || note.created_at ? formatNoteDate(note.updated_at || note.created_at) : '';

    // Format similarity score as percentage (shared badge; '' when absent).
    const similarityHtml = showSimilarity
        ? window.formatting.similarityBadge(note.similarity)
        : '';

    const isSelected = selectedNoteIds.has(note.id);
    const selectedClass = isSelected ? 'ldr-note-card-selected' : '';
    const checkboxHtml = selectMode
        ? `<span class="ldr-note-card-checkbox"><i class="fas ${isSelected ? 'fa-check-square' : 'fa-square'}"></i></span>`
        : '';

    // Real <a href> for the card — keyboard users get focus + Enter
    // navigation natively, screen readers announce it as a link, and
    // middle-click / ctrl-click opens in a new tab. Click handler is
    // delegated below in setupCardClickDelegation so the anchor can be
    // intercepted in select-mode (toggle selection) or fall through to
    // native navigation otherwise.
    return `
        <a class="ldr-note-card ${pinnedClass} ${selectedClass}" data-note-id="${escapeHtml(note.id)}" href="/notes/${encodeURIComponent(note.id)}" data-action="open-note">
            ${checkboxHtml}
            <div class="ldr-note-card-header">
                <h3 class="ldr-note-card-title">${escapeHtml(note.title)}</h3>
                ${similarityHtml}
            </div>
            <p class="ldr-note-card-preview">${escapeHtml(preview)}</p>
            <div class="ldr-note-card-footer">
                <div class="ldr-note-card-tags">${tagsHtml}</div>
                <div class="ldr-note-card-stats">
                    ${note.research_count > 0 ? `<span class="ldr-note-stat"><i class="fas fa-microscope"></i> ${note.research_count}</span>` : ''}
                    ${note.is_indexed ? `<span class="ldr-note-stat ldr-indexed"><i class="fas fa-check-circle"></i></span>` : ''}
                    <span class="ldr-note-stat"><i class="far fa-clock"></i> ${date}</span>
                </div>
            </div>
        </a>
    `;
}

/**
 * Toggle multi-select mode on/off.
 */
function toggleSelectMode() {
    selectMode = !selectMode;
    const btn = document.getElementById('ldr-select-mode-btn');
    const label = document.getElementById('ldr-select-mode-label');
    const actionBar = document.getElementById('ldr-select-actions');
    if (btn) btn.classList.toggle('active', selectMode);
    if (label) label.textContent = selectMode ? 'Cancel' : 'Select';
    if (actionBar) actionBar.style.display = selectMode ? 'flex' : 'none';
    if (!selectMode) selectedNoteIds.clear();
    updateSelectActions();
    renderNotes(lastShowSimilarity);
}

/**
 * Toggle selection state of a single note.
 */
function toggleNoteSelection(noteId) {
    if (selectedNoteIds.has(noteId)) {
        selectedNoteIds.delete(noteId);
    } else {
        if (selectedNoteIds.size >= SYNTH_MAX) {
            window.ui?.showMessage?.(`You can select at most ${SYNTH_MAX} notes`, 'warning');
            return;
        }
        selectedNoteIds.add(noteId);
    }
    updateSelectActions();
    renderNotes(lastShowSimilarity);
}

/**
 * Clear all selected notes.
 */
function clearSelection() {
    selectedNoteIds.clear();
    updateSelectActions();
    renderNotes(lastShowSimilarity);
}

function updateSelectActions() {
    const count = selectedNoteIds.size;
    const countEl = document.getElementById('ldr-select-count');
    const synthBtn = document.getElementById('ldr-synthesize-btn');
    if (countEl) countEl.textContent = `${count} selected`;
    if (synthBtn) synthBtn.disabled = count < SYNTH_MIN || count > SYNTH_MAX;
}

/**
 * Open the synthesis modal with the currently selected notes.
 */
function openSynthesizeModal() {
    if (selectedNoteIds.size < SYNTH_MIN || selectedNoteIds.size > SYNTH_MAX) {
        window.ui?.showMessage?.(
            `Select ${SYNTH_MIN}-${SYNTH_MAX} notes first`, 'warning'
        );
        return;
    }
    const list = document.getElementById('synthesis-selected-list');
    if (list) {
        const selectedNotes = notes.filter(n => selectedNoteIds.has(n.id));
        // eslint-disable-next-line no-unsanitized/property -- audited 2026-04-15: escapeHtml'd
        list.innerHTML = selectedNotes
            .map(n => `<div class="ldr-synthesis-selected-item">${escapeHtml(n.title)}</div>`)
            .join('');
    }
    const preview = document.getElementById('synthesis-preview');
    if (preview) preview.style.display = 'none';
    if (synthesizeModal) synthesizeModal.show();
}

async function _callSynthesize(endpoint, extraBody) {
    const payload = {
        note_ids: Array.from(selectedNoteIds),
        synthesis_type: document.getElementById('synthesis-type').value,
        ...extraBody,
    };
    const response = await safeFetchWithAuth(endpoint, {
        method: 'POST',
        credentials: 'same-origin',
        headers: {
            'Content-Type': 'application/json',
            'X-CSRFToken': getCSRFToken(),
        },
        body: JSON.stringify(payload),
    });
    return response.json();
}

/**
 * Put a synthesis button into a busy state (spinner + label) and return a
 * function that restores its original markup. Mirrors the spinner feedback the
 * search/FAB paths give for slow operations.
 */
function setSynthesizeButtonBusy(btn, label) {
    if (!btn) return () => {};
    const originalChildren = Array.from(btn.childNodes);
    btn.disabled = true;
    const spinnerIcon = document.createElement('i');
    spinnerIcon.className = 'fas fa-spinner fa-spin';
    // audited 2026-07-22: label is inserted via createTextNode, never parsed as HTML
    // bearer:disable javascript_lang_dangerous_insert_html
    btn.replaceChildren(spinnerIcon, document.createTextNode(` ${label}`));
    return () => {
        btn.disabled = false;
        // audited 2026-07-22: restores the button's own captured child Nodes — no HTML parsing
        // bearer:disable javascript_lang_dangerous_insert_html
        btn.replaceChildren(...originalChildren);
    };
}

/**
 * Preview the synthesized content without persisting.
 */
async function previewSynthesis() {
    if (_synthesizeInProgress) return;
    const btn = document.getElementById('synthesis-preview-btn');
    const createBtn = document.getElementById('synthesis-create-btn');
    _synthesizeInProgress = true;
    const restoreBtn = setSynthesizeButtonBusy(btn, 'Generating…');
    if (createBtn) createBtn.disabled = true;
    // Show an in-place placeholder so the modal doesn't look frozen during
    // the multi-second LLM call.
    const container = document.getElementById('synthesis-preview');
    const title = document.getElementById('synthesis-preview-title');
    const content = document.getElementById('synthesis-preview-content');
    if (title) title.textContent = 'Preview';
    if (content) {
        // eslint-disable-next-line no-unsanitized/property -- audited 2026-06-10: pure hardcoded HTML, no user data
        content.innerHTML = '<i class="fas fa-spinner fa-spin"></i> Generating preview…';
    }
    if (container) container.style.display = 'block';
    try {
        const data = await _callSynthesize(
            '/notes/api/notes/synthesize/preview',
            { create_note: false }
        );
        if (!data.success) {
            window.ui?.showMessage?.(data.error || 'Preview failed', 'error');
            if (container) container.style.display = 'none';
            return;
        }
        const result = data.result || {};
        if (title) {
            title.textContent = result.suggested_title
                ? `Preview — "${result.suggested_title}"`
                : 'Preview';
        }
        if (content) {
            const raw = result.content || '';
            // bearer:disable javascript_lang_dangerous_insert_html
            // eslint-disable-next-line no-unsanitized/property -- audited 2026-04-15: basicMarkdownRender escapes input
            content.innerHTML = window.formatting.basicMarkdownRender(raw);
        }
        if (container) container.style.display = 'block';

        // Surface a warning when the LLM only saw a slice of the source
        // notes. Without it, users assume the full content was used.
        if (result.truncated_sources) {
            window.ui?.showMessage?.(
                'One or more notes were too long; only the first part was used in the synthesis.',
                'warning',
            );
        }
    } catch (error) {
        SafeLogger.error('Error previewing synthesis:', error);
        window.ui?.showMessage?.('Preview failed', 'error');
        if (container) container.style.display = 'none';
    } finally {
        // eslint-disable-next-line require-atomic-updates -- single-writer guard cleared in finally
        _synthesizeInProgress = false;
        restoreBtn();
        if (createBtn) createBtn.disabled = false;
    }
}

/**
 * Create a new synthesized note and navigate to it.
 */
async function createSynthesizedNote() {
    if (_synthesizeInProgress) return;
    const btn = document.getElementById('synthesis-create-btn');
    const previewBtn = document.getElementById('synthesis-preview-btn');
    _synthesizeInProgress = true;
    const restoreBtn = setSynthesizeButtonBusy(btn, 'Creating…');
    if (previewBtn) previewBtn.disabled = true;
    // Track whether we navigate away on success — if so, leave the busy state
    // in place (the page is unloading) rather than flashing the button back.
    let navigating = false;
    try {
        const data = await _callSynthesize(
            '/notes/api/notes/synthesize',
            { create_note: true }
        );
        if (!data.success) {
            window.ui?.showMessage?.(data.error || 'Synthesis failed', 'error');
            return;
        }
        const result = data.result || {};
        const newNoteId = result.note_id;
        if (newNoteId) {
            navigating = true;
            URLValidator.safeAssign(
                window.location, 'href', `/notes/${newNoteId}`
            );
        } else {
            window.ui?.showMessage?.('Synthesis created but no note id returned', 'warning');
        }
    } catch (error) {
        SafeLogger.error('Error creating synthesized note:', error);
        window.ui?.showMessage?.('Synthesis failed', 'error');
    } finally {
        // eslint-disable-next-line require-atomic-updates -- single-writer guard cleared in finally
        _synthesizeInProgress = false;
        if (!navigating) {
            restoreBtn();
            if (previewBtn) previewBtn.disabled = false;
        }
    }
}

/**
 * Create a new note (open modal)
 */
function createNewNote() {
    // Reset form
    document.getElementById('note-title').value = '';
    document.getElementById('note-content').value = '';
    document.getElementById('ldr-note-tags').value = '';

    // The modal heading is static ("New Note" + icon in notes.html); no
    // JS update needed — rewriting textContent here would wipe the icon.

    // Reset to write mode and initialize toolbar
    resetModalToWriteMode();

    // Show modal
    noteModal.show();

    // Initialize toolbar after modal is shown
    setTimeout(() => {
        initModalMarkdownToolbar();
    }, 100);
}

/**
 * Save note from modal
 */
async function saveNote() {
    // In-flight guard: the Save button isn't disabled otherwise, so a
    // double-click would POST twice and create two notes.
    if (_createNoteInProgress) return;

    const title = document.getElementById('note-title').value.trim();
    const content = document.getElementById('note-content').value.trim();
    const tagsInput = document.getElementById('ldr-note-tags').value.trim();

    if (!title) {
        showNotesError('Title is required');
        return;
    }

    if (!content) {
        showNotesError('Content is required');
        return;
    }

    // Parse tags
    const tags = tagsInput
        ? tagsInput.split(',').map(t => t.trim()).filter(t => t)
        : [];

    const saveBtn = document.querySelector('[data-action="save-note"]');
    _createNoteInProgress = true;
    if (saveBtn) saveBtn.disabled = true;
    try {
        const response = await safeFetchWithAuth('/notes/api/notes', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'X-CSRFToken': getCSRFToken()
            },
            body: JSON.stringify({ title, content, tags }),
            credentials: 'same-origin'
        });

        const data = await response.json();

        if (data.success) {
            noteModal.hide();
            // Navigate to the new note
            URLValidator.safeAssign(window.location, 'href', `/notes/${data.id}`);
        } else {
            showNotesError(data.error || 'Failed to create note');
        }
    } catch (error) {
        SafeLogger.error('Error creating note:', error);
        showNotesError('Failed to create note');
    } finally {
        // eslint-disable-next-line require-atomic-updates -- single-writer guard cleared in finally
        _createNoteInProgress = false;
        if (saveBtn) saveBtn.disabled = false;
    }
}

/**
 * Get CSRF token — delegates to global api service
 */
function getCSRFToken() {
    // Delegates to the shared helper (meta-tag fallback when window.api
    // hasn't loaded — the previous inline version sent an empty token then).
    return window.NotesShared.csrfToken();
}

// escapeHtml is provided globally by xss-protection.js (window.escapeHtml)

/**
 * Format date for display
 */
function formatNoteDate(dateStr) {
    if (!dateStr) return '';
    try {
        const date = new Date(dateStr);
        const now = new Date();
        const diffDays = Math.floor((now - date) / (1000 * 60 * 60 * 24));

        if (diffDays === 0) {
            return 'Today';
        }
        if (diffDays === 1) {
            return 'Yesterday';
        }
        if (diffDays < 7) {
            return `${diffDays}d ago`;
        }
        return date.toLocaleDateString();
    } catch (_e) {
        return '';
    }
}

/**
 * Show error message
 */
function showNotesError(message) {
    if (window.ui && typeof window.ui.showMessage === 'function') {
        window.ui.showMessage(message, 'error');
    } else {
        // Never swallow an error silently — a missing ui helper otherwise
        // makes failed actions look like "the button did nothing" (mirrors
        // note-detail.js's showNoteError).
        window.alert(message);
    }
}

// ============================================================================
// Modal Markdown Editor Toolbar
// ============================================================================

/**
 * Initialize the modal markdown toolbar functionality
 */
function initModalMarkdownToolbar() {
    const toolbar = document.getElementById('modal-markdown-toolbar');
    const textarea = document.getElementById('note-content');
    const preview = document.getElementById('modal-note-preview');
    const container = document.querySelector('#noteModal .ldr-markdown-editor-container');

    if (!toolbar || !textarea || !preview || !container) {
        SafeLogger.warn('Modal markdown toolbar elements not found');
        return;
    }

    // Get tabs within the modal
    const tabs = container.querySelectorAll('.ldr-editor-tab');

    // Tab switching
    tabs.forEach(tab => {
        // Remove any existing listeners by cloning
        const newTab = tab.cloneNode(true);
        tab.parentNode.replaceChild(newTab, tab);

        newTab.addEventListener('click', () => {
            const mode = newTab.dataset.mode;
            // Deactivate every tab once (the previous code wrapped this in a
            // redundant outer tabs.forEach whose loop variable was unused).
            const parentTabs = container.querySelectorAll('.ldr-editor-tab');
            parentTabs.forEach(pt => {
                pt.classList.remove('active');
                pt.style.borderBottomColor = 'transparent';
                pt.style.color = 'var(--text-secondary)';
            });

            // Update clicked tab
            parentTabs.forEach(pt => {
                if (pt.dataset.mode === mode) {
                    pt.classList.add('active');
                    pt.style.borderBottomColor = 'var(--accent-primary)';
                    pt.style.color = 'var(--accent-primary)';
                }
            });

            if (mode === 'preview') {
                textarea.style.display = 'none';
                toolbar.style.display = 'none';
                preview.style.display = 'block';
                // Use renderMarkdown from ui.js if available
                if (typeof renderMarkdown === 'function') {
                    // bearer:disable javascript_lang_dangerous_insert_html
                    // eslint-disable-next-line no-unsanitized/property -- audited 2026-03-29: renderMarkdown sanitizes output via DOMPurify
                    preview.innerHTML = renderMarkdown(textarea.value);
                } else {
                    // bearer:disable javascript_lang_dangerous_insert_html
                    // eslint-disable-next-line no-unsanitized/property -- audited 2026-03-29: basicModalMarkdownRender escapes user input before rendering
                    preview.innerHTML = basicModalMarkdownRender(textarea.value);
                }
            } else {
                textarea.style.display = 'block';
                toolbar.style.display = 'flex';
                preview.style.display = 'none';
            }
        });
    });

    // Toolbar button clicks - remove existing listeners first
    toolbar.querySelectorAll('.ldr-toolbar-btn').forEach(btn => {
        const newBtn = btn.cloneNode(true);
        btn.parentNode.replaceChild(newBtn, btn);

        newBtn.addEventListener('click', (e) => {
            e.preventDefault();
            const format = newBtn.dataset.format;
            applyModalMarkdownFormat(textarea, format);
        });
    });

    // Keyboard shortcuts for textarea
    textarea.removeEventListener('keydown', handleModalKeydown);
    textarea.addEventListener('keydown', handleModalKeydown);
}

/**
 * Handle keydown events in modal textarea
 */
function handleModalKeydown(e) {
    const textarea = document.getElementById('note-content');
    if (!textarea) return;

    if (e.ctrlKey || e.metaKey) {
        if (e.key === 'b') {
            e.preventDefault();
            applyModalMarkdownFormat(textarea, 'bold');
        }
        if (e.key === 'i') {
            e.preventDefault();
            applyModalMarkdownFormat(textarea, 'italic');
        }
        if (e.key === 'k') {
            e.preventDefault();
            applyModalMarkdownFormat(textarea, 'link');
        }
    }
}

/**
 * Apply markdown formatting to selected text in modal (delegates to shared formatting service)
 */
function applyModalMarkdownFormat(textarea, format) {
    window.formatting.applyMarkdownFormat(textarea, format);
}

/**
 * Basic markdown rendering for modal (delegates to shared formatting service)
 */
function basicModalMarkdownRender(text) {
    return window.formatting.basicMarkdownRender(text);
}

/**
 * Reset modal to write mode
 */
function resetModalToWriteMode() {
    const textarea = document.getElementById('note-content');
    const preview = document.getElementById('modal-note-preview');
    const toolbar = document.getElementById('modal-markdown-toolbar');
    const container = document.querySelector('#noteModal .ldr-markdown-editor-container');

    if (textarea) textarea.style.display = 'block';
    if (preview) preview.style.display = 'none';
    if (toolbar) toolbar.style.display = 'flex';

    // Reset tabs
    if (container) {
        const tabs = container.querySelectorAll('.ldr-editor-tab');
        tabs.forEach(tab => {
            if (tab.dataset.mode === 'write') {
                tab.classList.add('active');
                tab.style.borderBottomColor = 'var(--accent-primary)';
                tab.style.color = 'var(--accent-primary)';
            } else {
                tab.classList.remove('active');
                tab.style.borderBottomColor = 'transparent';
                tab.style.color = 'var(--text-secondary)';
            }
        });
    }
}

// Production-inert test hook (mirrors note-detail.js's __noteDetailTest).
// Exposed only when a vitest harness sets window.__VITEST_TEST__ before import,
// so it never ships to the browser. Lets tests drive search functions that
// would otherwise need the full DOMContentLoaded page bootstrap.
if (typeof window !== 'undefined' && window.__VITEST_TEST__) {
    window.__notesTest = {
        loadNotesHybridSearch,
        stripMarkdownForPreview,
        getNotes: () => notes,
        setSearchState: ({ search = '', collectionId = null } = {}) => {
            currentFilter = {
                ...currentFilter,
                search,
                collection_id: collectionId,
            };
            hybridSearchId = 0;
        },
        setDomRefs: ({ grid, empty, notice }) => {
            notesGrid = grid;
            notesEmpty = empty;
            searchNotice = notice;
        },
        // Select-mode selection-pruning (renderNotes keeps the selection
        // scoped to the visible result set).
        renderNotes,
        setNotes: (n) => { notes = n; },
        setSelectState: ({ mode = false, ids = [] } = {}) => {
            selectMode = mode;
            selectedNoteIds = new Set(ids);
        },
        getSelectedIds: () => [...selectedNoteIds],
        // Pure-logic helpers
        mergeTieredNotes,
        formatNoteDate,
        // Keyword / semantic search loaders (fetch -> notes -> render)
        loadNotesKeywordSearch,
        loadNotesSemanticSearch,
        // Keyword-listing pager
        loadMoreNotes,
        // Create-note modal save (validation, tag parse, in-flight guard)
        saveNote,
        setNoteModal: (m) => { noteModal = m; },
        // Synthesis: preview + create selected notes into a new note
        previewSynthesis,
        createSynthesizedNote,
        // "Ask your notes": pre-flight gating + start research
        askNotes,
        // Note card renderer (badges, pinned, select-mode checkbox, stats)
        createNoteCard,
        // Central search dispatch (mode + query length -> loader)
        loadNotes,
        setMode: (m) => { searchMode = m; },
        // Select -> synthesize gating (SYNTH_MIN/MAX caps)
        toggleNoteSelection,
        openSynthesizeModal,
        setSynthesizeModal: (m) => { synthesizeModal = m; },
        // Card click delegation (select mode intercepts; else native nav)
        handleNoteCardAction,
        // Collection filter (load -> render dropdown; clear -> reset + reload)
        loadCollections,
        clearCollectionFilter,
        getCurrentFilter: () => ({ ...currentFilter }),
        setCollectionFilter: (el) => { collectionFilter = el; },
        // Event wiring (search-input debounce, collection-change)
        setupEventListeners,
        setSearchInput: (el) => { searchInput = el; },
        // Modal editor markdown keyboard shortcuts (Ctrl+B/I/K)
        handleModalKeydown,
    };
}