import { Client, Connection } from '@temporalio/client'
import { env } from '../../config/env'
import { logger } from '../../config/logger'

let _client: Client | null = null

export async function getTemporalClient(): Promise<Client> {
  if (_client) return _client

  const connection = await Connection.connect({ address: env.TEMPORAL_ADDRESS })

  _client = new Client({
    connection,
    namespace: env.TEMPORAL_NAMESPACE,
  })

  logger.info({ address: env.TEMPORAL_ADDRESS }, 'Temporal Client conectado')
  return _client
}
