import { useCancelPromotion, useNadal, usePromotion } from '#lib/composables' import type { GetFastPayRequest, SenpayResponse } from '#lib/types' import { valueToNumber } from '#lib/utils' import { ref } from 'vue' /** * Provides methods and state related to Senpay transactions, including fetching promotion packages and requesting transfer information. * @returns {Object} An object containing state and methods for managing Senpay transactions. * @namespace */ export function useSenpay() { const { payment } = useNadal() const { isUsingPromotion, promotionPackages, fetchPromotionPackages } = usePromotion() const { openModalCancelPromotion } = useCancelPromotion() const isLoading = ref(false) /** * Requests transfer information for a Senpay transaction. * @async * @param {GetFastPayRequest} data - The request data for the Senpay transaction. * @returns {Promise} A promise that resolves to the response from the Senpay transaction request. */ const fetchTransferInfo = async ( data: GetFastPayRequest, ): Promise => { if (isUsingPromotion.value) { openModalCancelPromotion() return Promise.resolve(undefined) } const body = { ...data, amount: valueToNumber(data.amount.toString(), 1000), } return await payment.requestSenpayInvoice< GetFastPayRequest, SenpayResponse >(body) } /** * Fetches promotion packages and manages the loading state. * @returns {Promise} A promise that resolves when the promotion packages have been fetched. */ const fetchData = async (): Promise => { isLoading.value = true return fetchPromotionPackages().finally(() => { isLoading.value = false }) } return { isLoading, promotionPackages, fetchTransferInfo, fetchData, } }