{"version":3,"file":"ApiPlatformClientService.mjs","sourceRoot":"","sources":["../src/ApiPlatformClientService.ts"],"names":[],"mappings":";;;;;;;;;;;;AAEA,OAAO,EAAE,iBAAiB,EAAE,wBAAc;AAI1C,kBAAkB;AAElB;;;GAGG;AACH,MAAM,CAAC,MAAM,4BAA4B,GAAG,0BAA0B,CAAC;AAEvE,oBAAoB;AAEpB,MAAM,yBAAyB,GAAG,CAAC,sBAAsB,CAAU,CAAC;AA2CpE,6BAA6B;AAE7B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,MAAM,OAAO,wBAAwB;IAOnC,YAAY,EACV,SAAS,EACT,GAAG,aAAa,EACgB;QAPzB,sDAA8C;QAE9C,mDAA2B;QAMlC,IAAI,CAAC,IAAI,GAAG,4BAA4B,CAAC;QACzC,uBAAA,IAAI,uCAAc,SAAS,MAAA,CAAC;QAC5B,uBAAA,IAAI,oCAAW,IAAI,iBAAiB,CAAC,aAAa,CAAC,MAAA,CAAC;QAEpD,uBAAA,IAAI,2CAAW,CAAC,4BAA4B,CAC1C,IAAI,EACJ,yBAAyB,CAC1B,CAAC;IACJ,CAAC;IAED;;;;;;OAMG;IACH,oBAAoB;QAClB,OAAO,uBAAA,IAAI,wCAAQ,CAAC;IACtB,CAAC;CACF","sourcesContent":["import type { Messenger } from '@metamask/messenger';\n\nimport { ApiPlatformClient } from './api';\nimport type { ApiPlatformClientOptions } from './api';\nimport type { ApiPlatformClientServiceMethodActions } from './ApiPlatformClientService-method-action-types';\n\n// === GENERAL ===\n\n/**\n * The name of the {@link ApiPlatformClientService}, used to namespace the\n * service's actions and events.\n */\nexport const apiPlatformClientServiceName = 'ApiPlatformClientService';\n\n// === MESSENGER ===\n\nconst MESSENGER_EXPOSED_METHODS = ['getApiPlatformClient'] as const;\n\n/**\n * Actions that {@link ApiPlatformClientService} exposes to other consumers.\n */\nexport type ApiPlatformClientServiceActions =\n  ApiPlatformClientServiceMethodActions;\n\n/**\n * Actions from other messengers that {@link ApiPlatformClientServiceMessenger} calls.\n */\ntype AllowedActions = never;\n\n/**\n * Events that {@link ApiPlatformClientService} exposes to other consumers.\n */\nexport type ApiPlatformClientServiceEvents = never;\n\n/**\n * Events from other messengers that {@link ApiPlatformClientService} subscribes to.\n */\ntype AllowedEvents = never;\n\n/**\n * The messenger which is restricted to actions and events accessed by\n * {@link ApiPlatformClientService}.\n */\nexport type ApiPlatformClientServiceMessenger = Messenger<\n  typeof apiPlatformClientServiceName,\n  ApiPlatformClientServiceActions | AllowedActions,\n  ApiPlatformClientServiceEvents | AllowedEvents\n>;\n\n// === SERVICE OPTIONS ===\n\n/**\n * Options for constructing {@link ApiPlatformClientService}.\n */\nexport type ApiPlatformClientServiceOptions = {\n  /** The messenger suited for this service. */\n  messenger: ApiPlatformClientServiceMessenger;\n} & ApiPlatformClientOptions;\n\n// === SERVICE DEFINITION ===\n\n/**\n * Service that provides access to {@link ApiPlatformClient} via the messenger.\n *\n * Consumers obtain the client by calling the `ApiPlatformClientService:getApiPlatformClient`\n * action, then use it for accounts, prices, token, and tokens API calls.\n *\n * @example\n *\n * ```ts\n * import { Messenger } from '@metamask/messenger';\n * import {\n *   ApiPlatformClientService,\n *   type ApiPlatformClientServiceActions,\n *   type ApiPlatformClientServiceEvents,\n * } from '@metamask/core-backend';\n *\n * const rootMessenger = new Messenger<'Root', ApiPlatformClientServiceActions, ApiPlatformClientServiceEvents>({ namespace: 'Root' });\n * const serviceMessenger = new Messenger<\n *   'ApiPlatformClientService',\n *   ApiPlatformClientServiceActions,\n *   ApiPlatformClientServiceEvents,\n *   typeof rootMessenger\n * >({ namespace: 'ApiPlatformClientService', parent: rootMessenger });\n *\n * new ApiPlatformClientService({\n *   messenger: serviceMessenger,\n *   clientProduct: 'metamask-extension',\n *   getBearerToken: async () => token,\n * });\n *\n * const client = rootMessenger.call('ApiPlatformClientService:getApiPlatformClient');\n * const balances = await client.accounts.fetchV5MultiAccountBalances(accountIds);\n * ```\n */\nexport class ApiPlatformClientService {\n  readonly name: typeof apiPlatformClientServiceName;\n\n  readonly #messenger: ApiPlatformClientServiceMessenger;\n\n  readonly #client: ApiPlatformClient;\n\n  constructor({\n    messenger,\n    ...clientOptions\n  }: ApiPlatformClientServiceOptions) {\n    this.name = apiPlatformClientServiceName;\n    this.#messenger = messenger;\n    this.#client = new ApiPlatformClient(clientOptions);\n\n    this.#messenger.registerMethodActionHandlers(\n      this,\n      MESSENGER_EXPOSED_METHODS,\n    );\n  }\n\n  /**\n   * Returns the shared ApiPlatformClient instance.\n   *\n   * Use this via the messenger: `messenger.call('ApiPlatformClientService:getApiPlatformClient')`.\n   *\n   * @returns The ApiPlatformClient instance (accounts, prices, token, tokens).\n   */\n  getApiPlatformClient(): ApiPlatformClient {\n    return this.#client;\n  }\n}\n"]}