{"version":3,"file":"BAZat7-B.js","sources":["../../../../composables/useCommon.ts"],"sourcesContent":["import { API_ENDPOINT } from \"~/constants/api-endpoint\";\nimport { useRewardsStore } from \"~/stores/rewards-store\";\nimport type {\n ICheckoutResponseData,\n IGetApplicableCouponsResponse,\n} from \"~/types/checkout\";\nimport type {\n ICityResponse,\n IModuleCountApiResponse,\n IStateCity,\n IStateResponse,\n} from \"~/types/common\";\nimport type { IOrderSummary } from \"~/types/order\";\nimport type { IRewardResponse, RewardsPayload, Reward } from \"~/types/rewards\";\n\nexport const useCommon = () => {\n // Initialize variables\n const otp: Ref = ref(\"\");\n const isLoading: Ref = ref(false);\n const isSubmitting: Ref = ref(false);\n const isCityLoading: Ref = ref(false);\n const isStateLoading: Ref = ref(false);\n const errorMessage: Ref = ref(\"\");\n const initTimer: Ref = ref(false);\n const timerCountSec: Ref = ref(90);\n const isResendingOTP: Ref = ref(false);\n const isTimerShowing: Ref = ref(true);\n const rewards = ref([]);\n const rewardsUrl = API_ENDPOINT.REWARDS;\n let countdownTimeout: ReturnType | null = null;\n const isRewardsLoading = ref(false);\n\n // Initialize router, error handling, and snackbar\n const router = useRouter();\n const { handleErrorResponse } = useErrors();\n const { showSnackbar } = useSnackbar();\n\n // Initialize stores and computed properties\n const rewardsStore = useRewardsStore();\n const { setRewards, setPagination } = useRewardsStore();\n const orderStore = useOrderStore();\n const checkoutStore = useCheckoutStore();\n const orderId = computed(() => checkoutStore.getOrderId);\n\n // Location data state\n const states = ref([]);\n const cities = ref([]);\n const selectedCountryId = ref(\"1\");\n\n /**\n * Countdown timer function\n */\n const countDownTimer = (): void => {\n // Check if the timer is initialized\n if (initTimer.value) {\n // Decrement the timer count\n timerCountSec.value--;\n\n // If the timer is still running, set a timeout to decrement the timer again\n if (timerCountSec.value >= 0 && timerCountSec.value > 0) {\n // Set a timeout to decrement the timer again\n countdownTimeout = setTimeout(() => {\n countDownTimer();\n }, 1000);\n } else {\n // If the timer has reached 0, hide the timer\n isTimerShowing.value = false;\n }\n }\n };\n\n /**\n * Resets the countdown timer\n */\n const resetCountDown = (): void => {\n // Clear the timeout if it exists\n if (countdownTimeout) {\n clearTimeout(countdownTimeout);\n }\n\n // Reset the timer count and show the timer\n timerCountSec.value = 90;\n isTimerShowing.value = true;\n\n // Initialize the timer\n initTimer.value = true;\n\n // Start the timer\n countDownTimer();\n };\n\n /**\n * Resends the OTP\n */\n const resendOTP = async (): Promise => {\n // Set the resending OTP flag to true\n isResendingOTP.value = true;\n try {\n // Reset the resending OTP flag\n isResendingOTP.value = false;\n\n // Clear the OTP value\n otp.value = \"\";\n\n // Reset the timer\n resetCountDown();\n } catch (err) {\n // Set the resending OTP flag to false\n console.error(err);\n isResendingOTP.value = false;\n\n // Clear the OTP value\n otp.value = \"\";\n\n // Set the error message\n errorMessage.value = \"Failed to resend OTP\";\n }\n };\n\n /**\n * Fetches the rewards list based on the provided payload\n * @param payload - The payload containing pagination and filter data\n */\n const getRewardsList = async (payload: RewardsPayload) => {\n try {\n const { data, current_page, last_page } =\n await useApi().get(`${rewardsUrl}`, {\n params: {\n page: payload.page,\n per_page: payload.per_page,\n sort: payload.sort,\n order_by: payload.order_by,\n search: payload.search,\n filter: payload.filters,\n },\n });\n\n // If the data is available, set the rewards and pagination\n if (data) {\n // Set the rewards and pagination\n rewards.value = data;\n\n // Set the rewards and pagination\n setRewards(data);\n\n // Set the pagination\n setPagination({\n currentPage: current_page,\n lastPage: last_page,\n });\n }\n } catch (error: any) {\n // Handle error response\n errorMessage.value = handleErrorResponse(error.response).error;\n\n // Show the error message\n showSnackbar(errorMessage.value, \"error\");\n\n // Reset the rewards\n rewards.value = [];\n }\n };\n\n /**\n * Fetches the count of all modules\n */\n const getallmodulecount = async () => {\n try {\n // Fetch the module count from the API\n const response = await useApi().get(\n `${API_ENDPOINT.ALL_MODULE_COUNT}`\n );\n // Set the module count\n rewardsStore.setModuleCount(response.data);\n } catch (error: any) {\n // Handle error response\n errorMessage.value = handleErrorResponse(error.response).error;\n\n // Show the error message\n showSnackbar(errorMessage.value, \"error\");\n }\n };\n\n /**\n * Fetches states list based on country ID\n * Updates states reference data\n */\n const fetchStates = async () => {\n isStateLoading.value = true;\n try {\n // Fetch states list from the API\n const response: IStateResponse = await useApi().get(\"states\", {\n params: { country_id: selectedCountryId.value },\n });\n // Update states reference data\n states.value = response.data;\n } catch (error: any) {\n // Handle error response\n errorMessage.value = handleErrorResponse(error.response).error;\n\n // Show the error message\n showSnackbar(errorMessage.value, \"error\");\n } finally {\n // Reset loading state\n isStateLoading.value = false;\n }\n };\n\n /**\n * Fetches cities list based on selected state\n * @param stateId - ID of the selected state\n */\n const fetchCities = async (stateId: string) => {\n isCityLoading.value = true;\n try {\n // Fetch cities list from the API\n const response: ICityResponse = await useApi().get(\"cities\", {\n params: { state_id: stateId },\n });\n // Update cities reference data\n cities.value = response.data;\n } catch (error: any) {\n // Handle error response\n errorMessage.value = handleErrorResponse(error.response).error;\n\n // Clear the cities if the state is not found\n if (error.status === 404) {\n cities.value = [];\n }\n showSnackbar(errorMessage.value, \"error\");\n } finally {\n // Reset loading state\n isCityLoading.value = false;\n }\n };\n\n /**\n * Fetches the order summary based on the cart ID\n */\n const fetchOrderSummary = async () => {\n try {\n // Fetch the order summary from the API\n const response: IOrderSummary = await useApi().post(\n API_ENDPOINT.ORDER_SUMMARY,\n {\n order_id: orderId.value,\n },\n );\n\n // Set the order summary\n orderStore.setOrderSummary(response);\n } catch (error: any) {\n // Handle error response\n errorMessage.value = handleErrorResponse(error.response).error;\n\n // Show the error message\n showSnackbar(errorMessage.value, \"error\");\n }\n };\n\n /**\n * Fetches the checkout data\n */\n const checkOut = async () => {\n try {\n // Create checkout session\n const response = await useApi().post(\n API_ENDPOINT.CHECKOUT,\n {\n order_id: orderId.value,\n },\n );\n // Set checkout data\n checkoutStore.setCheckoutData(response.data);\n } catch (error: any) {\n // Handle error response\n errorMessage.value = handleErrorResponse(error.response).error;\n\n // Show the error message\n showSnackbar(errorMessage.value, \"error\");\n }\n };\n\n /**\n * Fetches the coupons\n */\n const fetchCoupons = async () => {\n try {\n // Fetch the coupons\n const response = await useApi().get(\n API_ENDPOINT.GET_APPLICABLE_COUPONS,\n );\n\n // Set the applicable coupons to the checkout store\n checkoutStore.setApplicableCoupons(response.data);\n } catch (error: any) {\n // Handle error response\n errorMessage.value = handleErrorResponse(error.response).error;\n\n // Show the error message\n showSnackbar(errorMessage.value, \"error\");\n }\n };\n\n /**\n * Handles the order summary and coupons fetch\n */\n const handleSummaryAndCouponFetch = async () => {\n // Set the loading state\n isLoading.value = true;\n\n // Fetch the coupons\n if (checkoutStore.hasCoupon) {\n await fetchCoupons();\n }\n\n // Fetch the order summary\n await fetchOrderSummary();\n\n // Reset the loading state\n isLoading.value = false;\n };\n\n return {\n isLoading,\n isSubmitting,\n isCityLoading,\n isStateLoading,\n states,\n cities,\n selectedCountryId,\n errorMessage,\n initTimer,\n otp,\n timerCountSec,\n isResendingOTP,\n isTimerShowing,\n router,\n countDownTimer,\n resetCountDown,\n resendOTP,\n rewards,\n getRewardsList,\n getallmodulecount,\n fetchStates,\n fetchCities,\n checkOut,\n handleSummaryAndCouponFetch,\n fetchOrderSummary,\n isRewardsLoading,\n };\n};\n"],"names":["useCommon","otp","ref","isLoading","isSubmitting","isCityLoading","isStateLoading","errorMessage","initTimer","timerCountSec","isResendingOTP","isTimerShowing","rewards","rewardsUrl","API_ENDPOINT","countdownTimeout","isRewardsLoading","router","useRouter","handleErrorResponse","useErrors","showSnackbar","useSnackbar","rewardsStore","useRewardsStore","setRewards","setPagination","orderStore","useOrderStore","checkoutStore","useCheckoutStore","orderId","computed","states","cities","selectedCountryId","countDownTimer","resetCountDown","resendOTP","err","getRewardsList","payload","data","current_page","last_page","useApi","error","getallmodulecount","response","fetchStates","fetchCities","stateId","fetchOrderSummary","checkOut","fetchCoupons"],"mappings":"wJAeO,MAAMA,GAAY,IAAM,CAEvB,MAAAC,EAAmBC,EAAI,EAAE,EACzBC,EAA0BD,EAAI,EAAK,E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