import { CancellationRuleEnum } from "@/constants/booking" import type { Room } from "@/stores/my-stay/myStayRoomDetailsStore" interface ModificationConditions { canModify: boolean isNotPast: boolean isNotCancelled: boolean isNotPrePaid: boolean isNotRewardNight: boolean } interface GuaranteeConditions { isCancellableBefore6PM: boolean hasNoGuaranteeInfo: boolean isNotCancelled: boolean isNotPast: boolean } export function isDatetimePast(date: Date): boolean { return new Date(date) < new Date() } export function checkDateModifiable({ canChangeDate, datetimeIsInThePast, isCancelled, isPrePaid, isRewardNight, }: { canChangeDate: boolean datetimeIsInThePast: boolean isCancelled: boolean isPrePaid: boolean isRewardNight: boolean }): boolean { const conditions: ModificationConditions = { canModify: canChangeDate, isNotPast: !datetimeIsInThePast, isNotCancelled: !isCancelled, isNotPrePaid: !isPrePaid, isNotRewardNight: !isRewardNight, } return Object.values(conditions).every(Boolean) } export function checkCancelable({ bookedRoom, linkedReservationRooms, datetimeIsInThePast, }: { bookedRoom: Room linkedReservationRooms: Room[] datetimeIsInThePast: boolean }): boolean { const hasAnyCancelableRoom = bookedRoom.isCancelable || linkedReservationRooms.some((room) => room.isCancelable) return hasAnyCancelableRoom && !datetimeIsInThePast } export function checkGuaranteeable({ bookedRoom, datetimeIsInThePast, }: { bookedRoom: Room datetimeIsInThePast: boolean }): boolean { const conditions: GuaranteeConditions = { isCancellableBefore6PM: bookedRoom.rateDefinition.cancellationRule === CancellationRuleEnum.CancellableBefore6PM, hasNoGuaranteeInfo: !bookedRoom.guaranteeInfo, isNotCancelled: !bookedRoom.isCancelled, isNotPast: !datetimeIsInThePast, } return Object.values(conditions).every(Boolean) } export function checkCanDownloadInvoice({ isCancelled, isPrePaid, }: { isCancelled: boolean isPrePaid: boolean }): boolean { return !isCancelled && isPrePaid }