import { defaultCustomization, stylePresets, type BundleCustomization, } from "../config"; export function getBundlePresetValues( id: string, ): BundleCustomization | undefined { const preset = stylePresets.find((item) => item.id === id); return preset ? { ...defaultCustomization, ...preset.customization } : undefined; } export function getMatchingBundlePreset(values: BundleCustomization) { const normalize = (key: string, value: unknown) => { if (key === "fontFamily" && (!value || value === "inherit")) return ""; return typeof value === "string" && key.toLowerCase().includes("color") ? value.toLowerCase() : value; }; return stylePresets.find((preset) => { const expected = getBundlePresetValues(preset.id)!; return ( Object.keys(defaultCustomization) as (keyof BundleCustomization)[] ).every( (key) => normalize(key, values[key]) === normalize(key, expected[key]), ); }); } export const bundlePreviewItems = [ { id: 1, name: "Headphones", price: 79.99, discount: 15, image: "headphones.jpg", }, { id: 2, name: "Phone Case", price: 29.99, discount: 10, image: "phone-case.jpg", }, { id: 3, name: "USB-C Cable", price: 19.99, discount: 20, image: "usb-c-cable.jpg", }, ]; export function clampPreviewQuantity(value: number) { return Number.isFinite(value) ? Math.min(10, Math.max(1, Math.trunc(value))) : 1; } export function getBundlePreviewTotals(quantities: Record) { const original = bundlePreviewItems.reduce( (sum, item) => sum + item.price * clampPreviewQuantity(quantities[item.id] ?? 1), 0, ); const total = bundlePreviewItems.reduce( (sum, item) => sum + item.price * (1 - item.discount / 100) * clampPreviewQuantity(quantities[item.id] ?? 1), 0, ); const savings = original - total; return { total, savings, percent: Math.round((savings / original) * 100) }; }