export default function useOrderInfo({ activeCartId, activeCart, activeCartItems, addresses, getAddresses }) { const orderInfo = ref({ address: null, deliveryMethod: null, paymentMethod: null, }); watch(activeCartId, () => { // reset fields when tab is switched orderInfo.value.deliveryMethod = null; orderInfo.value.paymentMethod = null; }); watch(activeCart, async (newVal, oldVal) => { // set order info if (newVal?.customer) { await getAddresses(); if (!oldVal || !oldVal.customer || oldVal.customer !== newVal.customer) { const defaultAddress = addresses.value.find((add) => add.is_default); orderInfo.value.address = defaultAddress; } } else { addresses.value = null; orderInfo.value.address = null; } }); watch( () => orderInfo.value.deliveryMethod, (newVal) => { if (newVal?.code === "HOME_DELIVERY") { const defaultAddress = addresses.value.find((add) => add.is_default); orderInfo.value.address = defaultAddress; } }, ); const isOrderValid = computed(() => { if (activeCartItems.value?.length === 0) return false; if (!activeCart.value?.customer) return false; if (!orderInfo.value.deliveryMethod) return false; if (!orderInfo.value.paymentMethod) return false; if (orderInfo.value.deliveryMethod.code === "HOME_DELIVERY" && !orderInfo.value.address) return false; return true; }); return { orderInfo, isOrderValid }; }