import type { App } from 'vue' import { apiClient, portalApiClient, registerDefaultInterceptors, registerPortalTokenInterceptors, } from '@/lib/axios' import { useAuthStore } from '@/stores/useAuthStore' import { useImpersonationStore } from '@/stores/useImpersonationStore' import { useNotificationStore } from '@/stores/useNotificationStore' import { useOrganisationStore } from '@/stores/useOrganisationStore' // Numeric prefix `3.` runs after `2.pinia.ts`, so Pinia is active by // the time these store factories resolve. Stores are looked up lazily // inside each callback (not eagerly at plugin-init), which keeps the // seam tolerant of any future plugin-ordering changes. export default function (_: App): void { const sharedDeps = { notify: (message: string, level: 'error' | 'warning') => useNotificationStore().show(message, level), onAuthFail: () => { const authStore = useAuthStore() if (authStore.isInitialized) authStore.handleUnauthorized() }, onImpersonationRevoked: () => { useImpersonationStore().clearState() window.location.href = '/platform' }, } registerDefaultInterceptors(apiClient, { ...sharedDeps, getActiveOrgId: () => useOrganisationStore().activeOrganisationId, getImpersonationTargetUserId: () => useImpersonationStore().targetUserId, }) registerPortalTokenInterceptors(portalApiClient, { ...sharedDeps, getPortalToken: () => { const authStore = useAuthStore() as unknown as { portalToken?: string | null } return authStore.portalToken ?? null }, }) }