// Normalized cart model shared by parsers, analysis and the solver. Money is integer paise. export type Store = 'instamart' | 'blinkit' | 'zepto'; export const STORES: readonly Store[] = ['instamart', 'blinkit', 'zepto']; export type Unit = 'g' | 'ml' | 'count'; export interface CartLine { store: Store; name: string; brand: string | null; sizeValue: number; sizeUnit: Unit; quantity: number; priceMinor: number; mrpMinor: number | null; } export type FeeKind = 'delivery' | 'handling' | 'small_cart' | 'surge' | 'platform' | 'membership' | 'tip' | 'other'; export interface Fee { label: string; amountMinor: number; kind: FeeKind; preselected: boolean; } export type ParsedCart = | { status: 'ok'; store: Store; lines: CartLine[]; fees: Fee[]; payableMinor: number; capturedAt: string } | { status: 'unsupported_layout'; store: Store; reason: string }; export type OkCart = Extract; /** Parse displayed rupees such as "₹1,234.50" or "Rs. 99" into paise; null when it is not a single amount. */ export function rupeesToMinor(text: string): number | null { const match = /^\s*(?:₹|rs\.?|inr)?\s*(\d{1,3}(?:,\d{2,3})*|\d+)(?:\.(\d{1,2}))?\s*$/i.exec(text); if (!match) return null; const rupees = Number(match[1]!.replace(/,/g, '')); const paise = Number((match[2] ?? '').padEnd(2, '0')); const value = rupees * 100 + paise; return Number.isSafeInteger(value) ? value : null; } /** Identity of one product on one store, used to compare the same product across pages. */ export function lineKey(line: Pick): string { return `${line.store}|${line.name.trim().toLowerCase().replace(/\s+/g, ' ')}|${line.sizeValue}${line.sizeUnit}`; } /** A product price the user saw on a store page, kept locally for 24 hours. */ export interface SeenPrice { store: Store; name: string; brand: string | null; sizeValue: number; sizeUnit: Unit; priceMinor: number; available: boolean; /** SHA-256 of the store's displayed delivery area; the area text itself is never stored. */ locationKey: string; seenAt: string; }