// Command palette (PEP-52): item shape and ranking — pure, imported by the browser bundle too. export interface PaletteItem { kind: "project" | "story" | "runner" | "question" | "page"; /** Shown first, monospace: PEP, PEP-3, #12. */ key: string; title: string; /** Context line: project name, status. */ hint?: string; href: string; /** A backlog story can be moved to «В работе» from the palette. */ canStart?: boolean; } /** * Filters and ranks items for a query: every word must occur in "key title hint"; an exact key * wins, then a key prefix, then a word start in the title. Empty query: questions, then the rest as is. */ export function matchPalette(items: PaletteItem[], query: string, limit = 30): PaletteItem[] { const words = query.toLowerCase().split(/\s+/).filter(Boolean); if (!words.length) return items.slice(0, limit); const scored: Array<{ item: PaletteItem; score: number; i: number }> = []; items.forEach((item, i) => { const key = item.key.toLowerCase(); const title = item.title.toLowerCase(); const hay = `${key} ${title} ${(item.hint ?? "").toLowerCase()}`; if (!words.every((w) => hay.includes(w))) return; const q = words.join(" "); let score = 0; if (key === q) score += 100; else if (key.startsWith(q)) score += 50; if (title.startsWith(q)) score += 30; for (const w of words) if (new RegExp(`(^|[\\s«"(])${w.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}`).test(title)) score += 10; if (item.kind === "page") score -= 5; scored.push({ item, score, i }); }); return scored .sort((a, b) => b.score - a.score || a.i - b.i) .slice(0, limit) .map((x) => x.item); }