"use client";
import { UserApi, userApi } from "@/data/services/api";
import {
  User,
  UserEntitlements,
} from "@/data/dto/user";
import { Result } from "@/utils";
import type { IUserRepository } from "@/data/repositories/interfaces";
import { createLazySingleton } from "./lazy_singleton";

export class UserRepository implements IUserRepository {
  constructor(private readonly api: UserApi) {}

  /** 获取当前登录用户信息。 */
  async getCurrentUser(): Promise<Result<User>> {
    return Result.wrap(() => this.api.getCurrentUser());
  }

  /** 获取当前用户权益快照。 */
  async getEntitlements(): Promise<Result<UserEntitlements>> {
    return Result.wrap(() => this.api.getEntitlements());
  }
}

/** 全局懒单例。 */
export const getUserRepository = createLazySingleton<IUserRepository>(
  () => new UserRepository(userApi),
);
