"use client"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Trash, PlusCircle } from "lucide-react"; interface PricingTiersProps { pricing: any[]; handleTierChange: ( e: React.ChangeEvent, index: number ) => void; handleRemoveTier: (index: number) => void; handleAddTier: () => void; } export const PricingTiers = ({ pricing = [], handleTierChange, handleRemoveTier, handleAddTier, }: PricingTiersProps) => { const formatNumber = (num: number) => { if (isNaN(num) || num === null || num === undefined) return ""; // Return the number as-is without any formatting to prevent precision issues return num.toString(); }; const formatTotal = (num: number) => { return num.toFixed(2); }; const calculateTotal = (quantity: number, pricePerUnit: number) => { return formatTotal(quantity * pricePerUnit); }; const handleTotalChange = ( e: React.ChangeEvent, index: number, minQuantity: number ) => { if (!handleTierChange || !e || !e.target) return; const totalPrice = Number(e.target.value); const pricePerUnit = minQuantity > 0 ? totalPrice / minQuantity : 0; // Create a simple synthetic event with the raw number const syntheticEvent = { target: { name: 'pricePerUnit', value: pricePerUnit.toString() } } as React.ChangeEvent; try { handleTierChange(syntheticEvent, index); } catch (error) { console.error('Error in handleTotalChange:', error); } }; return (

Tiered Pricing

{pricing?.length > 0 ? ( <>
Quantity
Price Per Unit
Total Price
{[...pricing] .sort((a, b) => (a?.minQuantity || 0) - (b?.minQuantity || 0)) .map((tier, sortedIndex) => { if (!tier) return null; // Prefer stable identifiers for mapping back to the original array const keyId = tier._id || tier.tempId; const originalIndex = pricing.findIndex((p) => (p?._id || p?.tempId) === keyId); return (
handleTierChange?.(e, originalIndex)} className="h-8 text-sm px-2" /> handleTierChange?.(e, originalIndex)} className="h-8 text-sm px-2" /> handleTotalChange(e, originalIndex, tier?.minQuantity || 0)} className="h-8 text-sm px-2" />
); })} ) : (

No pricing tiers added.

)}
); };