import { keepPreviousData, useQuery } from '@tanstack/react-query' import { organizationKeys } from './keys' import type { CustomerAddress, CustomerTaxId } from './types' import { handleError, post } from '@/data/fetchers' import type { StudioError } from '@/lib/errors/studio-errors' import type { UseCustomQueryOptions } from '@/types' export type CreditTopUpPreviewVariables = { slug?: string amount?: number address?: CustomerAddress taxId?: CustomerTaxId } export async function previewCreditTopUp({ slug, amount, address, taxId, }: CreditTopUpPreviewVariables) { if (!slug) throw new Error('slug is required') if (!amount) throw new Error('amount is required') const { data, error } = await post(`/platform/organizations/{slug}/billing/credits/preview`, { params: { path: { slug } }, body: { amount, ...(address && { address }), ...(taxId && { tax_id: taxId }), }, }) if (error) handleError(error) return data } export type CreditTopUpPreviewData = Awaited> export const useCreditTopUpPreview = ( { slug, amount, address, taxId }: CreditTopUpPreviewVariables, { enabled = true, ...options }: UseCustomQueryOptions = {} ) => useQuery({ queryKey: organizationKeys.creditTopUpPreview(slug, { amount, address: address as Record | undefined, taxId: taxId as Record | undefined, }), queryFn: () => previewCreditTopUp({ slug, amount, address, taxId }), enabled: enabled && typeof slug !== 'undefined' && typeof amount !== 'undefined', placeholderData: keepPreviousData, ...options, })