import { unregisterCurrentDevice } from '../features/devices/device-service' import { detachPushRegistrationForLogout } from '../features/notifications/notification-service' import { getMobileRuntimeConfig } from './native-config' import { signOutWithLocalFallback, type LocalLogoutResult } from './logout' export interface AccountLogoutInput { userId: string | null deviceId: string | null purgeLocalSession: () => Promise } export interface AccountLogoutOperations { detachPush: (userId: string, deviceId: string | null) => Promise unregisterDevice: (userId: string, installationId: string) => Promise installationId: () => string signOut: (purgeLocalSession: () => Promise) => Promise } const accountLogoutOperations: AccountLogoutOperations = { detachPush: detachPushRegistrationForLogout, unregisterDevice: unregisterCurrentDevice, installationId: () => getMobileRuntimeConfig().installationId, signOut: signOutWithLocalFallback, } /** Keeps authenticated server detach operations ahead of local session loss. */ export async function performAccountLogout( input: AccountLogoutInput, operations: AccountLogoutOperations = accountLogoutOperations, ): Promise { if (input.userId !== null) { await operations.detachPush(input.userId, input.deviceId) try { await operations.unregisterDevice(input.userId, operations.installationId()) } catch { // Device unregister is best effort after push detach; local privacy and // refresh-token deletion must still proceed while offline. } } return operations.signOut(input.purgeLocalSession) }