export type StoreCategoryLike = { id?: string; name?: string; parentId?: string | null; parent?: { name?: string } | null; }; export type StoreCategoryTreeNode = { id: string; name: string; children?: { id: string; name: string }[]; }; export function storeStarCount(rating?: number | string | null): number { const n = Number(rating); if (!Number.isFinite(n) || n <= 0) return 5; return Math.min(5, Math.max(1, Math.round(n))); } export function storeCategoryTags( store: { tags?: unknown; categoryId?: string | null; category?: StoreCategoryLike | null; }, tree: StoreCategoryTreeNode[] = [], ): string[] { const fromJson = Array.isArray(store.tags) ? store.tags.map((t) => String(t).trim()).filter(Boolean) : []; if (fromJson.length) return fromJson; const names: string[] = []; const childName = String(store.category?.name || '').trim(); const parentName = String(store.category?.parent?.name || '').trim(); if (parentName) names.push(parentName); if (childName && childName !== parentName) names.push(childName); const storeCatId = String(store.categoryId || store.category?.id || ''); const storeParentId = String(store.category?.parentId || ''); for (const root of tree) { if (root.id === storeParentId || root.id === storeCatId) { if (root.name && !names.includes(root.name)) names.unshift(root.name); } for (const child of root.children ?? []) { if (child.id === storeCatId) { if (root.name && !names.includes(root.name)) names.unshift(root.name); if (child.name && !names.includes(child.name)) names.push(child.name); } } } return names; }