import type { fileRoutesMt as fileRoutes } from '@d8d/file-module-mt';
import { rpcClient } from '@d8d/shared-ui-components/utils/hc';

class FileClientManager {
  private static instance: FileClientManager;
  private client: ReturnType<typeof rpcClient<typeof fileRoutes>> | null = null;

  private constructor() {}

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

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

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

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

// 导出单例实例
const fileClientManager = FileClientManager.getInstance();

// 导出类型定义（用于类型检查）
export const fileClient: ReturnType<typeof rpcClient<typeof fileRoutes>> = fileClientManager.get()

export {
  fileClientManager
}