import * as React from 'react';
import { clearTrustedDeviceTokenCache } from '../../bridge/trustedDevice.js';
import { Text } from '../../ink.js';
import { refreshGrowthBookAfterAuthChange } from '../../services/analytics/growthbook.js';
import { getGroveNoticeConfig, getGroveSettings } from '../../services/api/grove.js';
import { clearPolicyLimitsCache } from '../../services/policyLimits/index.js';
// flushTelemetry is loaded lazily to avoid pulling in ~1.1MB of OpenTelemetry at startup
import { clearRemoteManagedSettingsCache } from '../../services/remoteManagedSettings/index.js';
import { getClaudeAIOAuthTokens, removeApiKey } from '../../utils/auth.js';
import { clearBetasCaches } from '../../utils/betas.js';
import { saveGlobalConfig } from '../../utils/config.js';
import { gracefulShutdownSync } from '../../utils/gracefulShutdown.js';
import { getSecureStorage } from '../../utils/secureStorage/index.js';
import { clearToolSchemaCache } from '../../utils/toolSchemaCache.js';
import { resetUserCache } from '../../utils/user.js';
export async function performLogout({
  clearOnboarding = false
}): Promise<void> {
  // Flush telemetry BEFORE clearing credentials to prevent org data leakage
  const {
    flushTelemetry
  } = await import('../../utils/telemetry/instrumentation.js');
  await flushTelemetry();
  await removeApiKey();

  // Wipe all secure storage data on logout
  const secureStorage = getSecureStorage();
  secureStorage.delete();
  await clearAuthRelatedCaches();
  saveGlobalConfig(current => {
    const updated = {
      ...current
    };
    if (clearOnboarding) {
      updated.hasCompletedOnboarding = false;
      updated.subscriptionNoticeCount = 0;
      updated.hasAvailableSubscription = false;
      if (updated.customApiKeyResponses?.approved) {
        updated.customApiKeyResponses = {
          ...updated.customApiKeyResponses,
          approved: []
        };
      }
    }
    updated.oauthAccount = undefined;
    return updated;
  });
}

// clearing anything memoized that must be invalidated when user/session/auth changes
export async function clearAuthRelatedCaches(): Promise<void> {
  // Clear the OAuth token cache
  getClaudeAIOAuthTokens.cache?.clear?.();
  clearTrustedDeviceTokenCache();
  clearBetasCaches();
  clearToolSchemaCache();

  // Clear user data cache BEFORE GrowthBook refresh so it picks up fresh credentials
  resetUserCache();
  refreshGrowthBookAfterAuthChange();

  // Clear Grove config cache
  getGroveNoticeConfig.cache?.clear?.();
  getGroveSettings.cache?.clear?.();

  // Clear remotely managed settings cache
  await clearRemoteManagedSettingsCache();

  // Clear policy limits cache
  await clearPolicyLimitsCache();
}
export async function call(): Promise<React.ReactNode> {
  await performLogout({
    clearOnboarding: true
  });
  const message = <Text>Successfully logged out from your Anthropic account.</Text>;
  setTimeout(() => {
    gracefulShutdownSync(0, 'logout');
  }, 200);
  return message;
}
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJSZWFjdCIsImNsZWFyVHJ1c3RlZERldmljZVRva2VuQ2FjaGUiLCJUZXh0IiwicmVmcmVzaEdyb3d0aEJvb2tBZnRlckF1dGhDaGFuZ2UiLCJnZXRHcm92ZU5vdGljZUNvbmZpZyIsImdldEdyb3ZlU2V0dGluZ3MiLCJjbGVhclBvbGljeUxpbWl0c0NhY2hlIiwiY2xlYXJSZW1vdGVNYW5hZ2VkU2V0dGluZ3NDYWNoZSIsImdldENsYXVkZUFJT0F1dGhUb2tlbnMiLCJyZW1vdmVBcGlLZXkiLCJjbGVhckJldGFzQ2FjaGVzIiwic2F2ZUdsb2JhbENvbmZpZyIsImdyYWNlZnVsU2h1dGRvd25TeW5jIiwiZ2V0U2VjdXJlU3RvcmFnZSIsImNsZWFyVG9vbFNjaGVtYUNhY2hlIiwicmVzZXRVc2VyQ2FjaGUiLCJwZXJmb3JtTG9nb3V0IiwiY2xlYXJPbmJvYXJkaW5nIiwiUHJvbWlzZSIsImZsdXNoVGVsZW1ldHJ5Iiwic2VjdXJlU3RvcmFnZSIsImRlbGV0ZSIsImNsZWFyQXV0aFJlbGF0ZWRDYWNoZXMiLCJjdXJyZW50IiwidXBkYXRlZCIsImhhc0NvbXBsZXRlZE9uYm9hcmRpbmciLCJzdWJzY3JpcHRpb25Ob3RpY2VDb3VudCIsImhhc0F2YWlsYWJsZVN1YnNjcmlwdGlvbiIsImN1c3RvbUFwaUtleVJlc3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