import { AlertTypeEnum } from "@scandic-hotels/common/constants/alert" import { BookingErrorCodeEnum } from "@scandic-hotels/trpc/enums/bookingErrorCode" import { BreakfastPackageEnum } from "@scandic-hotels/trpc/enums/breakfast" import type { Packages } from "@scandic-hotels/trpc/types/packages" import type { IntlShape } from "react-intl" import type { AncillaryErrorMessage } from "@/types/components/myPages/myStay/ancillaries" import type { BreakfastData } from "@/stores/my-stay/add-ancillary-flow" import type { AncillaryFormData } from "../schema" /** * This function calculates some breakfast data in the store. * It is used in various places in the add flow, but only needs * to be calculated once. */ export function calculateBreakfastData( isBreakfast: boolean, packages: Packages | null, nrOfAdults: number, childrenAges: number[], nrOfNights: number ): BreakfastData | null { if (!isBreakfast || !packages) { return null } const { nrOfPayingChildren, nrOfFreeChildren } = childrenAges.reduce( (total, childAge) => { if (childAge >= 4) { total.nrOfPayingChildren = total.nrOfPayingChildren + 1 } else { total.nrOfFreeChildren = total.nrOfFreeChildren + 1 } return total }, { nrOfPayingChildren: 0, nrOfFreeChildren: 0 } ) const adultPackage = packages.find( (p) => p.code === BreakfastPackageEnum.ANCILLARY_REGULAR_BREAKFAST ) const childPackage = packages.find( (p) => p.code === BreakfastPackageEnum.ANCILLARY_CHILD_PAYING_BREAKFAST ) const priceAdult = adultPackage?.localPrice.price const priceChild = childPackage?.localPrice.price const currency = adultPackage?.localPrice.currency ?? childPackage?.localPrice.currency if ( typeof priceAdult !== "number" || typeof priceChild !== "number" || typeof currency !== "string" ) { return null } else { return { nrOfAdults, nrOfPayingChildren, nrOfFreeChildren, nrOfNights, priceAdult, priceChild, currency, } } } export function buildBreakfastPackages( data: AncillaryFormData, breakfastData: BreakfastData ) { const packages = [ { code: BreakfastPackageEnum.ANCILLARY_REGULAR_BREAKFAST, quantity: breakfastData.nrOfAdults, comment: data.optionalText || undefined, }, { code: BreakfastPackageEnum.ANCILLARY_CHILD_PAYING_BREAKFAST, quantity: breakfastData.nrOfPayingChildren, comment: data.optionalText || undefined, }, { code: BreakfastPackageEnum.FREE_CHILD_BREAKFAST, quantity: breakfastData.nrOfFreeChildren, comment: data.optionalText || undefined, }, ] return packages.filter((pkg) => pkg.quantity > 0) } export function getErrorMessage( intl: IntlShape, errorCode: string | null ): AncillaryErrorMessage { switch (errorCode) { case BookingErrorCodeEnum.TransactionFailed: return { message: intl.formatMessage({ id: "guaranteePayment.failed", defaultMessage: "We had an issue guaranteeing your booking. Please try again.", }), type: AlertTypeEnum.Alarm, } case BookingErrorCodeEnum.TransactionCancelled: return { message: intl.formatMessage({ id: "guaranteePayment.cancelled", defaultMessage: "You have cancelled the payment. Your booking is not guaranteed.", }), type: AlertTypeEnum.Warning, } case "AncillaryFailed": return { message: intl.formatMessage({ id: "guaranteePayment.ancillaryFailed", defaultMessage: "The product could not be added. Your booking is guaranteed. Please try again.", }), type: AlertTypeEnum.Alarm, } default: return { message: intl.formatMessage({ id: "guaranteePayment.genericError", defaultMessage: "Something went wrong! Please try again later.", }), type: AlertTypeEnum.Alarm, } } }