export interface MenuGroupIconLibraryEntry { label: string; iconSvg: string; updatedAt: string; } export interface MenuGroupIconLibrary { version: 1; entries: Record; } const STORAGE_KEY = 'canteen.menu_group_icon_library'; function emptyLibrary(): MenuGroupIconLibrary { return { version: 1, entries: {} }; } export function normalizeMenuGroupIconLabel(label: string): string { return label.trim().replace(/\s+/g, ''); } function isValidIconSvg(iconSvg?: string | null): iconSvg is string { return Boolean(iconSvg && iconSvg.includes(' a.label.localeCompare(b.label, 'zh-CN') ); } export function seedLocalMenuGroupIconLibrary( groups: Array<{ label: string; iconSvg?: string }> ): void { for (const group of groups) { if (group.label.trim() && isValidIconSvg(group.iconSvg)) { saveLocalMenuGroupIcon(group.label, group.iconSvg); } } } export function mergeRemoteMenuGroupIconLibrary( entries: Record ): void { if (!entries || typeof entries !== 'object') return; const library = readLibrary(); for (const [key, entry] of Object.entries(entries)) { if (!isValidIconSvg(entry?.iconSvg)) continue; const normalizedKey = normalizeMenuGroupIconLabel(key) || key; const existing = library.entries[normalizedKey]; if ( existing && existing.updatedAt && entry.updatedAt && existing.updatedAt >= entry.updatedAt ) { continue; } library.entries[normalizedKey] = { label: entry.label || key, iconSvg: entry.iconSvg, updatedAt: entry.updatedAt || new Date().toISOString(), }; } writeLibrary(library); }