import { openReadonlyOrFail } from '../../../db/index.js';
import { buildFileConditionSQL } from '../../../db/query-builder.js';
import { normalizeSymbol } from '../../queries.js';
import { hasFtsIndex, sanitizeFtsQuery } from '../stores/fts5.js';
import { applyFilters } from './filters.js';
export function ftsSearchData(query, customDbPath, opts = {}) {
    const limit = opts.limit || 15;
    const db = openReadonlyOrFail(customDbPath);
    try {
        if (!hasFtsIndex(db)) {
            return null;
        }
        const ftsQuery = sanitizeFtsQuery(query);
        if (!ftsQuery) {
            return { results: [] };
        }
        let sql = `
      SELECT f.rowid AS node_id, rank AS bm25_score,
             n.name, n.kind, n.file, n.line, n.end_line, n.role
      FROM fts_index f
      JOIN nodes n ON f.rowid = n.id
      WHERE fts_index MATCH ?
    `;
        const params = [ftsQuery];
        if (opts.kind) {
            sql += ' AND n.kind = ?';
            params.push(opts.kind);
        }
        const fp = opts.filePattern;
        const fpArr = Array.isArray(fp) ? fp : fp ? [fp] : [];
        const isGlob = fpArr.length > 0 && fpArr.some((p) => /[*?[\]]/.test(p));
        if (fpArr.length > 0 && !isGlob) {
            const fc = buildFileConditionSQL(fpArr, 'n.file');
            sql += fc.sql;
            params.push(...fc.params);
        }
        sql += ' ORDER BY rank LIMIT ?';
        params.push(limit * 5);
        let rows;
        try {
            rows = db.prepare(sql).all(...params);
        }
        catch {
            return { results: [] };
        }
        rows = applyFilters(rows, opts);
        const hc = new Map();
        const results = rows.slice(0, limit).map((row) => ({
            ...normalizeSymbol(row, db, hc),
            bm25Score: -row.bm25_score,
        }));
        return { results };
    }
    finally {
        db.close();
    }
}
//# sourceMappingURL=keyword.js.map