"use client"; import React from "react"; import NameColumn from "./FooterNameColumn"; import PriceColumn from "./FooterPriceColumn"; import type { FormattedPeriod, Item } from "./generateTotal"; import generateTotal from "./generateTotal"; export interface Footer { name?: string; tooltip?: string; hideFirst?: boolean; } export interface Labels { [key: string]: string; remove: string; add: string; free: string; upfront: string; monthly: string; } export type FooterRow = { col1: React.ReactNode; col2: React.ReactNode; col3: React.ReactNode; }; function calculateTotal(items: Item[], type: "monthly" | "upfront"): number { return items.reduce((total, item) => { total += item?.price?.[type] ?? 0; total -= item?.discount?.[type] ?? 0; return total; }, 0); } const generateFooter = ( items: Item[], hide: string, footer: Footer, labels: Labels, size: "large", ): FooterRow[] => { const hasLimitedDiscounts = items.find( (item) => item?.monthsFree || item?.discount?.monthsCount, ); if (hasLimitedDiscounts) { const total = generateTotal(items, footer?.hideFirst) as FormattedPeriod[]; return total.map(({ name, price, discount }) => ({ col1: , col2: price && price.upfront !== undefined && price.upfront > 0 && hide !== "upfront" && ( ), col3: ( ), })); } return [ { col1: footer?.name && ( ), col2: hide !== "upfront" && ( ), col3: ( ), }, ]; }; export default generateFooter;