{"version":3,"file":"Df8Zmhwz.js","sources":["../../../../src/@types/product-subscription.ts","../../../../src/components/page/product/components/buyBox/components/options/subscription/useSubscriptionBox.ts"],"sourcesContent":["export enum EDeliveryIntervalValues {\n TWO_WEEKS = 'twoWeeks',\n THREE_WEEKS = 'threeWeeks',\n MONTHLY = 'fourWeeks',\n FIVE_WEEKS = 'fiveWeeks',\n SIX_WEEKS = 'sixWeeks',\n TWO_MONTHS = 'twoMonths',\n QUARTERLY = 'quarterly',\n HALFYEARLY = 'halfYearly',\n NINE_MONTHS = 'nineMonths',\n}\n\nexport enum EDeliveryDaysValues {\n MONDAY = '1',\n TUESDAY = '2',\n WEDNESDAY = '3',\n THURSDAY = '4',\n FRIDAY = '5',\n}\n\nexport interface IDeliveryInterval {\n label: string;\n value: string;\n}\n\nexport interface IDeliveryDay {\n label: string;\n value: string;\n}\n","import { useCartStore } from '~/stores/useCart';\nimport {\n EDeliveryIntervalValues,\n EDeliveryDaysValues,\n type IDeliveryInterval,\n type IDeliveryDay,\n} from '@/@types/product-subscription';\nimport type { IListItem } from '~/@types/bullet-list';\nimport {\n useDialogStore,\n DialogIdent,\n DialogResponses,\n} from '~/stores/useDialogStore';\nimport type {\n ProductSubscriptionItem,\n ProductSubscriptionOrderDate,\n ProductSubscriptionOrderDates,\n} from '~/server/transformers/shop/productSubscriptionListTransformer';\nimport type { Product } from '~/server/transformers/shop/product/types';\nimport { useSessionGet } from '~/composables/dataFetching/genericFetchers';\nimport { handleLoadingError } from '~/utils/handleLoadingError';\n\nexport function useSubscriptionBox(props: { product: Product }) {\n /** default values */\n const aboItemCount = ref(\n props.product?.productSubscription?.lowerLimit ??\n props.product?.minPurchase ??\n 0,\n );\n const selectedAboInterval = ref(EDeliveryIntervalValues.MONTHLY);\n const selectedDeliveryDay = ref(EDeliveryDaysValues.WEDNESDAY);\n\n const currentVariant = ref(props.product);\n const { t } = useTrans();\n const { addProductSubscription } = useCartStore();\n\n const deliveryIntervals: IDeliveryInterval[] = [\n {\n label: t('shop.orderAlarm.label.weeks', {\n interval: '2',\n }),\n value: EDeliveryIntervalValues.TWO_WEEKS,\n },\n {\n label: t('shop.orderAlarm.label.weeks', {\n interval: '3',\n }),\n value: EDeliveryIntervalValues.THREE_WEEKS,\n },\n {\n label: t('shop.orderAlarm.label.weeks', {\n interval: '4',\n }),\n value: EDeliveryIntervalValues.MONTHLY,\n },\n {\n label: t('shop.orderAlarm.label.weeks', {\n interval: '5',\n }),\n value: EDeliveryIntervalValues.FIVE_WEEKS,\n },\n {\n label: t('shop.orderAlarm.label.weeks', {\n interval: '6',\n }),\n value: EDeliveryIntervalValues.SIX_WEEKS,\n },\n {\n label: t('shop.orderAlarm.label.months', {\n interval: '2',\n }),\n value: EDeliveryIntervalValues.TWO_MONTHS,\n },\n {\n label: t('shop.orderAlarm.label.months', {\n interval: '3',\n }),\n value: EDeliveryIntervalValues.QUARTERLY,\n },\n {\n label: t('shop.orderAlarm.label.months', {\n interval: '6',\n }),\n value: EDeliveryIntervalValues.HALFYEARLY,\n },\n {\n label: t('shop.orderAlarm.label.months', {\n interval: '9',\n }),\n value: EDeliveryIntervalValues.NINE_MONTHS,\n },\n ];\n\n const deliveryDays: IDeliveryDay[] = [\n {\n label: t('globalWidget.monday'),\n value: EDeliveryDaysValues.MONDAY,\n },\n {\n label: t('globalWidget.tuesday'),\n value: EDeliveryDaysValues.TUESDAY,\n },\n {\n label: t('globalWidget.wednesday'),\n value: EDeliveryDaysValues.WEDNESDAY,\n },\n {\n label: t('globalWidget.thursday'),\n value: EDeliveryDaysValues.THURSDAY,\n },\n {\n label: t('globalWidget.friday'),\n value: EDeliveryDaysValues.FRIDAY,\n },\n ];\n\n const subscriptionListItems = ref([\n {\n text: t('shop.aboBox.productSubscription.listItem1'),\n },\n {\n text: t('shop.aboBox.productSubscription.listItem2'),\n },\n {\n text: t('shop.aboBox.productSubscription.listItem3'),\n },\n ] as IListItem[]);\n\n function onAddSubscriptionToCart(deliveryDay: string) {\n const deliverDayNumber: number = +deliveryDay;\n addProductSubscription(\n currentVariant.value.id,\n aboItemCount.value,\n deliverDayNumber,\n selectedAboInterval.value,\n );\n }\n\n async function getSubscriptionByProduct(\n productNumber: string,\n ): Promise {\n try {\n const site = useSiteIdent();\n const result = await useSessionGet(\n `/api/${site}/shop/product/productSubscription/productNumber/${productNumber}`,\n );\n if (result?.id) {\n return result;\n }\n return null;\n } catch (error) {\n handleLoadingError(error);\n }\n }\n\n return {\n aboItemCount,\n selectedAboInterval,\n deliveryIntervals,\n selectedDeliveryDay,\n deliveryDays,\n subscriptionListItems,\n onAddSubscriptionToCart,\n getSubscriptionByProduct,\n };\n}\n\nexport function useSubscriptionDeliveryNow(\n subscription: ProductSubscriptionItem,\n) {\n const dialogStore = useDialogStore();\n\n const nextOrderDate = ref(formatNextOrderDate(subscription.orderDates));\n\n const disabled = ref(\n checkDateDiff(subscription.notedDeliveryDate) ||\n subscription?.tempNotAvailable,\n );\n const triggerReload = ref(false);\n\n async function openDeliveryDialog(\n subscription: ProductSubscriptionItem,\n product: Product,\n ) {\n await dialogStore.openDialog(DialogIdent.SUBSCRIPTION_DELIVERY_NOW, {\n subscription: subscription,\n product: product,\n });\n triggerReload.value = true;\n }\n\n async function openPauseDialog(\n subscription: ProductSubscriptionItem,\n product: Product,\n disabled: boolean,\n ) {\n const response = await dialogStore.openDialog(\n DialogIdent.SUBSCRIPTION_PAUSE,\n {\n subscription: subscription,\n product: product,\n disabled: disabled,\n },\n );\n if (response == DialogResponses.SUCCESS) {\n triggerReload.value = true;\n }\n }\n\n async function openEditDialog(\n subscription: ProductSubscriptionItem,\n product: Product,\n ) {\n const response = await dialogStore.openDialog(\n DialogIdent.SUBSCRIPTION_EDIT,\n {\n subscription: subscription,\n product: product,\n triggerReload: triggerReload,\n },\n );\n if (response == DialogResponses.SUCCESS) {\n triggerReload.value = true;\n }\n }\n\n async function openCancelDialog(\n subscription: ProductSubscriptionItem,\n product: Product,\n ) {\n const sub = ref(subscription);\n const response = await dialogStore.openDialog(\n DialogIdent.SUBSCRIPTION_CANCEL,\n {\n subscription: sub,\n product: product,\n triggerReload: triggerReload,\n },\n );\n if (response == DialogResponses.SUCCESS) {\n triggerReload.value = true;\n }\n }\n\n function formatNextOrderDate(orderDates: ProductSubscriptionOrderDates) {\n const nextActiveOrderDate: ProductSubscriptionOrderDate = Object.values(\n orderDates,\n ).find((date) => {\n return date.status === true;\n });\n return new Date(nextActiveOrderDate.date).toLocaleDateString('de-DE', {\n day: 'numeric',\n month: 'long',\n year: 'numeric',\n weekday: 'short',\n });\n }\n\n // checks whether the date on which the order is placed is today or earlier\n function checkDateDiff(date: string) {\n // gets today's date and sets the time to 0 to compare it\n const dateNow = new Date();\n dateNow.setHours(0, 0, 0, 0);\n const dateDelivery = new Date(date);\n const diffTime = dateDelivery.getTime() - dateNow.getTime();\n const diffDays = Math.round(diffTime / (1000 * 3600 * 24));\n return diffDays <= 2;\n }\n\n return {\n nextOrderDate,\n disabled,\n triggerReload,\n openDeliveryDialog,\n openPauseDialog,\n formatNextOrderDate,\n checkDateDiff,\n openEditDialog,\n openCancelDialog,\n };\n}\n"],"names":["EDeliveryIntervalValues","EDeliveryDaysValues","useSubscriptionBox","props","aboItemCount","ref","_b","_a","_c","selectedAboInterval","selectedDeliveryDay","currentVariant","useTrans","addProductSubscription","useCartStore","deliveryIntervals","deliveryDays","subscriptionListItems","onAddSubscriptionToCart","deliveryDay","deliverDayNumber","getSubscriptionByProduct","productNumber","site","useSiteIdent","result","useSessionGet","error","handleLoadingError","useSubscriptionDeliveryNow","subscription","dialogStore","useDialogStore","nextOrderDate","formatNextOrderDate","disabled","checkDateDiff","triggerReload","openDeliveryDialog","product","DialogIdent","openPauseDialog","DialogResponses","openEditDialog","openCancelDialog","sub","orderDates","nextActiveOrderDate","date","dateNow","diffTime"],"mappings":"uHAAY,IAAAA,GAAAA,IACVA,EAAA,UAAY,WACZA,EAAA,YAAc,aACdA,EAAA,QAAU,YACVA,EAAA,WAAa,YACbA,EAAA,UA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