import { app } from 'electron' import { existsSync, readFileSync, writeFileSync } from 'fs' import { join } from 'path' function readJson(path: string, fallback: T): T { if (!existsSync(path)) return fallback try { return JSON.parse(readFileSync(path, 'utf-8')) as T } catch { return fallback } } function favPath(): string { return join(app.getPath('userData'), 'favorites.json') } function recentPath(): string { return join(app.getPath('userData'), 'recent.json') } export function getFavorites(): string[] { return readJson(favPath(), []) } export function toggleFavorite(id: string): string[] { const favs = getFavorites() const updated = favs.includes(id) ? favs.filter((f) => f !== id) : [...favs, id] writeFileSync(favPath(), JSON.stringify(updated), 'utf-8') return updated } export function getRecent(): string[] { return readJson(recentPath(), []) } export function addRecent(id: string): string[] { const recent = getRecent().filter((r) => r !== id) const updated = [id, ...recent].slice(0, 20) writeFileSync(recentPath(), JSON.stringify(updated), 'utf-8') return updated }