import { createRequire as __cr } from 'node:module'; const require = __cr(import.meta.url);

// src/index/fts.ts
function initFts(index) {
  const db = index.getDb();
  db.exec(`
    CREATE VIRTUAL TABLE IF NOT EXISTS notes_fts USING fts5(
      path UNINDEXED,
      type UNINDEXED,
      id UNINDEXED,
      title,
      content,
      content='notes',
      content_rowid='rowid'
    );

    CREATE TRIGGER IF NOT EXISTS notes_ai AFTER INSERT ON notes BEGIN
      INSERT INTO notes_fts(rowid, path, type, id, title, content)
      VALUES (new.rowid, new.path, new.type, new.id, new.title, new.content);
    END;

    CREATE TRIGGER IF NOT EXISTS notes_ad AFTER DELETE ON notes BEGIN
      INSERT INTO notes_fts(notes_fts, rowid, path, type, id, title, content)
      VALUES ('delete', old.rowid, old.path, old.type, old.id, old.title, old.content);
    END;

    CREATE TRIGGER IF NOT EXISTS notes_au AFTER UPDATE ON notes BEGIN
      INSERT INTO notes_fts(notes_fts, rowid, path, type, id, title, content)
      VALUES ('delete', old.rowid, old.path, old.type, old.id, old.title, old.content);
      INSERT INTO notes_fts(rowid, path, type, id, title, content)
      VALUES (new.rowid, new.path, new.type, new.id, new.title, new.content);
    END;
  `);
}
function searchFts(index, query, options = {}) {
  const db = index.getDb();
  const limit = options.limit ?? 20;
  const params = [query];
  let typeFilter = "";
  if (options.type) {
    typeFilter = " AND type = ?";
    params.push(options.type);
  }
  params.push(limit);
  const sql = `
    SELECT path, type, id, title,
           snippet(notes_fts, 4, '<mark>', '</mark>', '...', 40) AS snippet,
           rank
    FROM notes_fts
    WHERE notes_fts MATCH ?${typeFilter}
    ORDER BY rank
    LIMIT ?
  `;
  return db.prepare(sql).all(...params);
}

export {
  initFts,
  searchFts
};
//# sourceMappingURL=chunk-6FQISQNA.js.map