import { channelRoutes } from '@d8d/allin-channel-module';
import { rpcClient } from '@d8d/shared-ui-components/utils/hc'

export class ChannelClientManager {
  private static instance: ChannelClientManager;
  private client: ReturnType<typeof rpcClient<typeof channelRoutes>> | null = null;

  private constructor() {}

  public static getInstance(): ChannelClientManager {
    if (!ChannelClientManager.instance) {
      ChannelClientManager.instance = new ChannelClientManager();
    }
    return ChannelClientManager.instance;
  }

  // 初始化客户端
  public init(baseUrl: string = '/'): ReturnType<typeof rpcClient<typeof channelRoutes>> {
    return this.client = rpcClient<typeof channelRoutes>(baseUrl);
  }

  // 获取客户端实例
  public get(): ReturnType<typeof rpcClient<typeof channelRoutes>> {
    if (!this.client) {
      return this.init()
    }
    return this.client;
  }

  // 重置客户端（用于测试或重新初始化）
  public reset(): void {
    this.client = null;
  }
}

// 导出单例实例
const channelClientManager = ChannelClientManager.getInstance();

// 导出默认客户端实例（延迟初始化）
export const channelClient = channelClientManager.get()

export {
  channelClientManager
}