// Services and their durations — pure helpers shared by the admin editor, // the Shop block and the checkout. Which duration costs what is decided // (and charged) by the shop hooks in PocketBase; this only reads and shows it. import type { BundlePart, ItemDuration, ItemRecord, ShopSort } from './types'; /** A service's `durations` field (JSON, possibly hand-edited) → valid entries, shortest first, one per length. */ export function parseDurations(raw: unknown): ItemDuration[] { const list = Array.isArray(raw) ? raw : []; const seen = new Set(); return list .map((d) => ({ minutes: Math.floor(Number(d?.minutes)), price: Number(d?.price) })) .filter((d) => d.minutes > 0 && Number.isFinite(d.price) && d.price >= 0 && !seen.has(d.minutes) && seen.add(d.minutes)) .sort((a, b) => a.minutes - b.minutes); } /** `45` → "45 min", `60` → "1 hour", `120` → "2 hours", `90` → "1 h 30 min". */ export function formatDuration(minutes: number) { if (minutes < 60) return `${minutes} min`; const hours = Math.floor(minutes / 60); const rest = minutes % 60; if (!rest) return `${hours} ${hours === 1 ? 'hour' : 'hours'}`; return `${hours} h ${rest} min`; } /** The lowest price among a service's durations (what "from …" shows), or null without any. */ export function lowestPrice(durations: ItemDuration[]) { return durations.length ? Math.min(...durations.map((d) => d.price)) : null; } /** A stored code `ABCD2345` → `ABCD-2345`, as shown to people (the shop hooks format it the same way). */ export function formatCode(code: string) { return code.length === 8 ? `${code.slice(0, 4)}-${code.slice(4)}` : code; } /** * What a bundle's parts cost bought one by one (each service at the part's * length), to show next to the bundle price — or null when a part isn't on * sale (not in `items`, or a length the service no longer has). */ export function bundleListPrice(parts: BundlePart[], items: ItemRecord[]) { let total = 0; for (const part of parts) { const item = items.find((candidate) => candidate.id === part.item); if (!item || item.type === 'bundle') return null; const unit = item.type === 'item_with_time' ? item.durations.find((d) => d.minutes === part.minutes)?.price : item.price; if (unit == null) return null; total += unit * part.count; } return Math.round(total * 100) / 100; } /** The Shop block's sort choices, in menu order, with what visitors see. */ export const SHOP_SORTS: { value: ShopSort; label: string }[] = [ { value: 'recommended', label: 'Recommended' }, { value: 'popular', label: 'Most popular' }, { value: 'price_asc', label: 'Price: low to high' }, { value: 'price_desc', label: 'Price: high to low' }, { value: 'name', label: 'Name (A–Z)' } ]; const byName = (a: ItemRecord, b: ItemRecord) => a.name.localeCompare(b.name, 'en-GB', { sensitivity: 'base' }); const byPriority = (a: ItemRecord, b: ItemRecord) => b.priority - a.priority || byName(a, b); /** * `items` in the Shop block's `sort` order (a new array). Ties — and, for * "Most popular", items that haven't sold lately — fall back to Recommended, * so the admin's priorities still decide whatever the numbers don't. * Services sort by their lowest price (what "from …" shows). */ export function sortItems(items: ItemRecord[], sort: ShopSort = 'recommended') { const list = [...items]; switch (sort) { case 'popular': return list.sort((a, b) => (a.popularRank ?? Infinity) - (b.popularRank ?? Infinity) || byPriority(a, b)); case 'price_asc': return list.sort((a, b) => a.price - b.price || byPriority(a, b)); case 'price_desc': return list.sort((a, b) => b.price - a.price || byPriority(a, b)); case 'name': return list.sort(byName); default: return list.sort(byPriority); } }