// src/auth/client/types.ts
var STORAGE_KEYS = {
  AUTH_TOKEN: "auth_token",
  USER_DATA: "user_data"
};
var API_ROUTES = {
  AUTH: {
    LOGIN: "/auth/login",
    REGISTER: "/auth/register",
    LOGOUT: "/auth/logout",
    ME: "/auth/me"
  }
};

// src/auth/client/base-api-client.ts
var BaseApiClient = class {
  constructor(storage, request, baseUrl) {
    this.storage = storage;
    this.request = request;
    this.baseUrl = baseUrl;
    this.token = null;
    this.user = null;
  }
  /**
   * 初始化 - 从存储中加载 token 和用户信息
   */
  async init() {
    try {
      this.token = await this.storage.getItem(STORAGE_KEYS.AUTH_TOKEN);
      const userData = await this.storage.getItem(STORAGE_KEYS.USER_DATA);
      if (userData) {
        this.user = JSON.parse(userData);
      }
    } catch (error) {
      console.error("Failed to load auth data:", error);
    }
  }
  /**
   * 设置认证 token
   */
  async setToken(token) {
    this.token = token;
    if (token) {
      await this.storage.setItem(STORAGE_KEYS.AUTH_TOKEN, token);
    } else {
      await this.storage.removeItem(STORAGE_KEYS.AUTH_TOKEN);
    }
  }
  /**
   * 设置用户信息
   */
  async setUser(user) {
    this.user = user;
    if (user) {
      await this.storage.setItem(STORAGE_KEYS.USER_DATA, JSON.stringify(user));
    } else {
      await this.storage.removeItem(STORAGE_KEYS.USER_DATA);
    }
  }
  /**
   * 获取当前 token
   */
  getToken() {
    return this.token;
  }
  /**
   * 获取当前用户
   */
  getUser() {
    return this.user;
  }
  /**
   * 检查是否已登录
   */
  async isAuthenticated() {
    return !!this.token;
  }
  /**
   * 清除用户数据
   */
  async clearUserData() {
    await this.storage.removeItem(STORAGE_KEYS.AUTH_TOKEN);
    await this.storage.removeItem(STORAGE_KEYS.USER_DATA);
    this.token = null;
    this.user = null;
  }
  /**
   * 发送请求的通用方法
   */
  async sendRequest(config) {
    try {
      const headers = {
        "Content-Type": "application/json",
        ...config.headers || {}
      };
      if (this.token) {
        headers["Authorization"] = "Bearer " + this.token;
      }
      const response = await this.request.request({
        ...config,
        url: this.baseUrl + config.url,
        headers
      });
      return response;
    } catch (error) {
      console.error("API request error:", error);
      return {
        success: false,
        error: error instanceof Error ? error.message : "\u7F51\u7EDC\u9519\u8BEF\uFF0C\u8BF7\u91CD\u8BD5"
      };
    }
  }
  // ==================== 认证相关 API ====================
  /**
   * 用户注册
   */
  async register(email, password, username) {
    const response = await this.sendRequest({
      url: API_ROUTES.AUTH.REGISTER,
      method: "POST",
      body: { email, password, username }
    });
    if (response.success && response.data) {
      await this.setToken(response.data.token);
      await this.setUser(response.data.user);
    }
    return response;
  }
  /**
   * 用户登录
   */
  async login(email, password) {
    const response = await this.sendRequest({
      url: API_ROUTES.AUTH.LOGIN,
      method: "POST",
      body: { email, password }
    });
    if (response.success && response.data) {
      await this.setToken(response.data.token);
      await this.setUser(response.data.user);
      if (typeof window !== "undefined") {
        window.dispatchEvent(
          new CustomEvent("user_login_success", {
            detail: {
              userId: response.data.user.id,
              email: response.data.user.email,
              role: response.data.user.role
            }
          })
        );
      }
    }
    return response;
  }
  /**
   * 用户退出登录
   */
  async logout() {
    await this.sendRequest({
      url: API_ROUTES.AUTH.LOGOUT,
      method: "POST"
    });
    await this.clearUserData();
  }
  /**
   * 获取当前用户信息
   */
  async getCurrentUser() {
    const response = await this.sendRequest({
      url: API_ROUTES.AUTH.ME,
      method: "GET"
    });
    if (response.success && response.data) {
      const userData = response.data.user || response.data;
      await this.setUser(userData);
      return {
        ...response,
        data: userData
      };
    }
    return response;
  }
  // ==================== 通用方法 ====================
  /**
   * 发送 GET 请求
   */
  async get(url, params) {
    return this.sendRequest({ url, method: "GET", params });
  }
  /**
   * 发送 POST 请求
   */
  async post(url, body) {
    return this.sendRequest({ url, method: "POST", body });
  }
  /**
   * 发送 PUT 请求
   */
  async put(url, body) {
    return this.sendRequest({ url, method: "PUT", body });
  }
  /**
   * 发送 DELETE 请求
   */
  async delete(url) {
    return this.sendRequest({ url, method: "DELETE" });
  }
};

export { API_ROUTES, BaseApiClient, STORAGE_KEYS };
//# sourceMappingURL=chunk-KZKIH4AS.mjs.map
//# sourceMappingURL=chunk-KZKIH4AS.mjs.map