import { useCallback } from 'react'; import { useHttpService } from './HttpService.tsx'; import { environment } from '../environments/environment.ts'; const API_URL = `${environment.fhirBaseApi}`; export type AddressAvailability = 'available' | 'taken' | 'reserved' | 'invalid' | 'unreachable'; const SERVER_STATES: ReadonlyArray = ['available', 'taken', 'reserved', 'invalid']; export const usePracticeAddressApi = () => { const { get } = useHttpService(); const recommendAddress = useCallback(async (practiceName: string): Promise => { if (!practiceName.trim()) return undefined; try { const body = await get<{ label?: string | null }>( `${API_URL}/practice-hostname-suggestion?name=${encodeURIComponent(practiceName.trim())}`, { bypassAuth: true, bypassOrganization: true }, ); return typeof body?.label === 'string' && body.label ? body.label : undefined; } catch { return undefined; } }, [get]); const checkAddress = useCallback(async (label: string): Promise => { if (!label.trim()) return 'invalid'; try { const body = await get<{ availability?: string }>( `${API_URL}/practice-hostname-availability/${encodeURIComponent(label.trim())}`, { bypassAuth: true, bypassOrganization: true }, ); return typeof body?.availability === 'string' && SERVER_STATES.includes(body.availability) ? body.availability as AddressAvailability : 'unreachable'; } catch { return 'unreachable'; } }, [get]); return { recommendAddress, checkAddress }; };