import type { Price } from "../types/price" type RegularPrice = { pricePerStay: number regularPricePerStay?: number } // Helper function to calculate regular/strikethrough price export function calculateRegularPrice({ total, useMemberRate, regularMemberPrice, regularPublicPrice, additionalCost = 0, }: { total: Price useMemberRate: boolean regularMemberPrice: RegularPrice | undefined regularPublicPrice: RegularPrice | undefined additionalCost?: number }) { if ( !total || (!useMemberRate && !regularPublicPrice) || (useMemberRate && !regularMemberPrice) ) { return total } let basePrice = 0 // Legend: // - total.local.price = Total Price = Black price, what the user pays // - total.local.regularPrice = Regular Price = Strikethrough price (could potentially be none) // - total.requested.price = Requested Price = EUR approx price // We sometimes don't get all the required data to calculate the correct strikethrough total. // Therefore we try these different approach to get a number that is close // enough to the real number if all data would've been present. if (useMemberRate && regularMemberPrice) { if (regularPublicPrice) { // #1 Member price uses public price as strikethrough basePrice = regularPublicPrice.pricePerStay } else if (regularMemberPrice.regularPricePerStay) { // #2 Member price uses member regular price as strikethrough basePrice = regularMemberPrice.regularPricePerStay } else { // #3 Member price uses member price as strikethrough basePrice = regularMemberPrice.pricePerStay } } else if (regularPublicPrice) { if (regularPublicPrice.regularPricePerStay) { // #1 Public price uses public regular price as strikethrough basePrice = regularPublicPrice.regularPricePerStay } else { // #2 Public price uses public price as strikethrough basePrice = regularPublicPrice.pricePerStay } } total.local.regularPrice = add( total.local.regularPrice, basePrice, additionalCost ) return total } //copied from enter-details/helpers.ts function add(...nums: (number | string | undefined)[]) { return nums.reduce((total: number, num) => { if (typeof num === "undefined") { num = 0 } total = total + parseInt(`${num}`) return total }, 0) }