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,IAAI,cAAc,CAAC,aAAa,CAAC,CAAC;QAC/C,IAAI,CAAC,MAAM,GAAG,IAAI,eAAe,CAAC,aAAa,CAAC,CAAC;IACnD,CAAC;IAED,6EAA6E;IAC7E,oDAAoD;IACpD,6EAA6E;IAE7E;;;;OAIG;IACH,IAAI,WAAW;QACb,OAAO,uBAAA,IAAI,4CAAmB,CAAC;IACjC,CAAC;IAED;;;;;OAKG;IACH,aAAa,CAAa,QAAkB;QAC1C,OAAO,uBAAA,IAAI,4CAAmB,CAAC,YAAY,CAAa,QAAQ,CAAC,CAAC;IACpE,CAAC;IAED;;;;;OAKG;IACH,aAAa,CAAa,QAAkB,EAAE,IAAgB;QAC5D,uBAAA,IAAI,4CAAmB,CAAC,YAAY,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC;IACvD,CAAC;IAED;;;;;OAKG;IACH,UAAU,CAAC,QAAkB;QAC3B,OAAO,uBAAA,IAAI,4CAAmB,CAAC,UAAU,CAAC,EAAE,QAAQ,EAAE,CAAC,GAAG,CAAC,CAAC;IAC9D,CAAC;IAED;;OAEG;IACH,KAAK;QACH,uBAAA,IAAI,4CAAmB,CAAC,KAAK,EAAE,CAAC;IAClC,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,aAAa;QACjB,MAAM,uBAAA,IAAI,4CAAmB,CAAC,iBAAiB,EAAE,CAAC;IACpD,CAAC;IAED;;;;;;OAMG;IACH,KAAK,CAAC,mBAAmB;QACvB,MAAM,uBAAA,IAAI,4CAAmB,CAAC,YAAY,CAAC;YACzC,QAAQ,EAAE,aAAa,CAAC,WAAW,EAAE;SACtC,CAAC,CAAC;IACL,CAAC;CACF;;AAED;;;;;GAKG;AACH,MAAM,UAAU,uBAAuB,CACrC,OAAiC;IAEjC,OAAO,IAAI,iBAAiB,CAAC,OAAO,CAAC,CAAC;AACxC,CAAC","sourcesContent":["/**\n * ApiPlatformClient - MetaMask API Platform Client\n *\n * A comprehensive API client that uses @tanstack/query-core directly for:\n * - Automatic request deduplication\n * - Intelligent caching\n * - Automatic retries with exponential backoff\n *\n * Provides unified access to all MetaMask backend APIs:\n * - Accounts API (accounts.api.cx.metamask.io)\n * - Price API (price.api.cx.metamask.io)\n * - Token API (token.api.cx.metamask.io)\n * - Tokens API (tokens.api.cx.metamask.io)\n *\n * @example\n * ```typescript\n * const client = new ApiPlatformClient({\n *   clientProduct: 'metamask-extension',\n *   getBearerToken: async () => token,\n * });\n *\n * // Access API methods through sub-clients\n * const networks = await client.accounts.fetchV2SupportedNetworks();\n * const balances = await client.accounts.fetchV5MultiAccountBalances(accountIds);\n * const prices = await client.prices.fetchV3SpotPrices(assetIds);\n * const tokenList = await client.token.fetchTokenList(1);\n * const assets = await client.tokens.fetchV3Assets(assetIds);\n *\n * // Cache management\n * await client.invalidateAll();           // Invalidate all caches\n * await client.invalidateAuthToken();     // Invalidate auth token\n * await client.accounts.invalidateBalances(); // Domain-specific via sub-client\n * await client.prices.invalidatePrices();     // Domain-specific via sub-client\n * ```\n */\n\nimport { QueryClient } from '@tanstack/query-core';\nimport type { QueryKey } from '@tanstack/query-core';\n\n// Import API clients from subfolders\nimport { AccountsApiClient } from './accounts';\nimport { authQueryKeys } from './base-client';\nimport { PricesApiClient } from './prices';\nimport {\n  STALE_TIMES,\n  GC_TIMES,\n  shouldRetry,\n  calculateRetryDelay,\n} from './shared-types';\nimport type { ApiPlatformClientOptions } from './shared-types';\nimport { TokenApiClient } from './token';\nimport { TokensApiClient } from './tokens';\n\n// ============================================================================\n// UNIFIED API CLIENT\n// ============================================================================\n\n/**\n * MetaMask API Platform Client with TanStack Query caching.\n * Provides cached access to all MetaMask backend APIs through a unified interface.\n *\n * Access API methods through the sub-clients:\n * - `client.accounts` - Accounts API (balances, transactions, NFTs, etc.)\n * - `client.prices` - Prices API (spot prices, exchange rates, historical prices)\n * - `client.token` - Token API (token metadata, trending, top gainers)\n * - `client.tokens` - Tokens API (bulk asset operations, supported networks)\n */\nexport class ApiPlatformClient {\n  /**\n   * Accounts API client.\n   * Provides methods for balances, transactions, relationships, NFTs, and token discovery.\n   */\n  readonly accounts: AccountsApiClient;\n\n  /**\n   * Prices API client.\n   * Provides methods for spot prices, exchange rates, and historical prices.\n   */\n  readonly prices: PricesApiClient;\n\n  /**\n   * Token API client.\n   * Provides methods for token metadata, networks, trending tokens, and top assets.\n   */\n  readonly token: TokenApiClient;\n\n  /**\n   * Tokens API client.\n   * Provides methods for bulk asset operations and supported networks.\n   */\n  readonly tokens: TokensApiClient;\n\n  /**\n   * Shared QueryClient instance used by all sub-clients.\n   */\n  readonly #sharedQueryClient: QueryClient;\n\n  constructor(options: ApiPlatformClientOptions) {\n    // Create or use provided QueryClient - shared by all sub-clients\n    this.#sharedQueryClient =\n      options.queryClient ??\n      new QueryClient({\n        defaultOptions: {\n          queries: {\n            staleTime: STALE_TIMES.DEFAULT,\n            gcTime: GC_TIMES.DEFAULT,\n            retry: shouldRetry,\n            retryDelay: calculateRetryDelay,\n            refetchOnWindowFocus: false,\n            networkMode: 'always',\n          },\n        },\n      });\n\n    // Pass the shared QueryClient to all sub-clients\n    const sharedOptions: ApiPlatformClientOptions = {\n      ...options,\n      queryClient: this.#sharedQueryClient,\n    };\n\n    this.accounts = new AccountsApiClient(sharedOptions);\n    this.prices = new PricesApiClient(sharedOptions);\n    this.token = new TokenApiClient(sharedOptions);\n    this.tokens = new TokensApiClient(sharedOptions);\n  }\n\n  // ==========================================================================\n  // CACHE MANAGEMENT (operates on shared QueryClient)\n  // ==========================================================================\n\n  /**\n   * Get the underlying QueryClient (for advanced usage).\n   *\n   * @returns The underlying QueryClient instance.\n   */\n  get queryClient(): QueryClient {\n    return this.#sharedQueryClient;\n  }\n\n  /**\n   * Get cached data for a query key.\n   *\n   * @param queryKey - The query key to look up.\n   * @returns The cached data or undefined.\n   */\n  getCachedData<CachedData>(queryKey: QueryKey): CachedData | undefined {\n    return this.#sharedQueryClient.getQueryData<CachedData>(queryKey);\n  }\n\n  /**\n   * Set cached data for a query key.\n   *\n   * @param queryKey - The query key to set data for.\n   * @param data - The data to cache.\n   */\n  setCachedData<CachedData>(queryKey: QueryKey, data: CachedData): void {\n    this.#sharedQueryClient.setQueryData(queryKey, data);\n  }\n\n  /**\n   * Check if a query is currently fetching.\n   *\n   * @param queryKey - The query key to check.\n   * @returns True if the query is currently fetching.\n   */\n  isFetching(queryKey: QueryKey): boolean {\n    return this.#sharedQueryClient.isFetching({ queryKey }) > 0;\n  }\n\n  /**\n   * Clear all cached data across all sub-clients.\n   */\n  clear(): void {\n    this.#sharedQueryClient.clear();\n  }\n\n  /**\n   * Invalidate all queries across all sub-clients.\n   */\n  async invalidateAll(): Promise<void> {\n    await this.#sharedQueryClient.invalidateQueries();\n  }\n\n  /**\n   * Invalidate the cached auth token.\n   * Call this when the user logs out or the token expires.\n   *\n   * Uses resetQueries() instead of invalidateQueries() to completely remove\n   * the cached value, ensuring the next request fetches a fresh token immediately.\n   */\n  async invalidateAuthToken(): Promise<void> {\n    await this.#sharedQueryClient.resetQueries({\n      queryKey: authQueryKeys.bearerToken(),\n    });\n  }\n}\n\n/**\n * Factory function to create an ApiPlatformClient.\n *\n * @param options - Configuration options for the client.\n * @returns A new ApiPlatformClient instance.\n */\nexport function createApiPlatformClient(\n  options: ApiPlatformClientOptions,\n): ApiPlatformClient {\n  return new ApiPlatformClient(options);\n}\n"]}