import type { SearchEntry, SettingsGroup } from '@/types/settings.types' // One entry per field. `text` holds every searchable part in lower case. export const buildSearchIndex = (groups: SettingsGroup[]): SearchEntry[] => { const entries: SearchEntry[] = [] groups.forEach(group => { group.pages.forEach(page => { page.sections.forEach(section => { section.fields.forEach(field => { const path = [group.label, page.label, section.label].filter(part => part !== '').join(' › ') entries.push({ fieldId: field.id, pageId: page.id, path, label: field.label, text: [page.label, section.label, field.label, field.description, field.key].join(' ').toLowerCase(), locked: page.locked || field.flags.includes('locked') }) }) }) }) }) return entries } export const searchIndex = (entries: SearchEntry[], query: string, limit = 20): SearchEntry[] => { const terms = query.toLowerCase().split(/\s+/).filter(term => term !== '') if (terms.length === 0) { return [] } return entries.filter(entry => terms.every(term => entry.text.includes(term))).slice(0, limit) }