// File: monorepo/web/maplefile-frontend/src/services/API/Tag/ListTagsAPIService.js
// List Tags API Service - Handles fetching user's tags

class ListTagsAPIService {
  constructor(authManager) {
    this.authManager = authManager;
    this._apiClient = null;
    console.log(
      "[ListTagsAPIService] API service initialized with AuthManager dependency",
    );
  }

  // Import ApiClient for authenticated requests
  async getApiClient() {
    if (!this._apiClient) {
      const { default: ApiClient } = await import("../ApiClient.js");
      this._apiClient = ApiClient;
    }
    return this._apiClient;
  }

  // List all tags for current user
  async listTags() {
    try {
      console.log("[ListTagsAPIService] Fetching tags from API");

      const apiClient = await this.getApiClient();
      const response = await apiClient.getMapleFile("/tags");

      // Extract tags array from response
      const tags = response?.tags || [];

      console.log(
        "[ListTagsAPIService] Tags fetched successfully:",
        tags.length,
        "tags",
      );
      return tags;
    } catch (error) {
      console.error("[ListTagsAPIService] Failed to fetch tags:", error);
      throw error;
    }
  }

  // Get debug information
  getDebugInfo() {
    return {
      serviceName: "ListTagsAPIService",
      managedBy: "AuthManager",
      isAuthenticated: this.authManager.isAuthenticated(),
      canMakeRequests: this.authManager.canMakeAuthenticatedRequests(),
    };
  }
}

export default ListTagsAPIService;
