// SPDX-FileCopyrightText: © 2026 LEDGER SAS
// SPDX-License-Identifier: Apache-2.0

import type { TezosCoinConfig } from '../config'
import { TezosToolkit } from '@taquito/taquito'

// Tezos client instance is cached to avoid costly rebuild at every request
// Watch out: cache key is the URL, coin module can be instantiated several times with different URLs
const servers = new Map<string, TezosToolkit>()
export function getTezosToolkit(config: TezosCoinConfig): TezosToolkit {
  const url = config.node.url
  let server = servers.get(url)
  if (server === undefined) {
    server = new TezosToolkit(url)
    servers.set(url, server)
  }
  return server
}
