"use strict";
/**
* API Service for acui
*
* This file demonstrates how to integrate the acapiClient into the acui project.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.enhancedApiClient = exports.agentApi = exports.rfpApi = exports.contentApi = exports.userApi = exports.logout = exports.initializeAuth = exports.setupAuthentication = exports.apiClient = void 0;
const api_client_1 = require("@bluefly/api-client");
// Create a cache manager
const cacheManager = new api_client_1.CacheManager({
    strategy: new api_client_1.InMemoryCache(),
    defaultTtl: 300000, // 5 minutes
});
// Create a singleton API client instance
exports.apiClient = new api_client_1.RestClient({
    baseUrl: process.env.REACT_APP_API_URL || 'https://api.bluefly.io',
    defaultHeaders: {
        "Content-Type": "application/json",
        "X-Client-ID": "acui",
    },
    timeout: 30000, // 30 seconds
    cacheStrategy: cacheManager,
});
// Add logging interceptors
exports.apiClient.addRequestInterceptor((0, api_client_1.requestLoggingInterceptor)({
    level: 'debug',
    logHeaders: true,
    logBody: process.env.NODE_ENV === 'development',
}));
// Add retry interceptor
exports.apiClient.addErrorInterceptor((0, api_client_1.retryInterceptor)({
    maxRetries: 3,
    retryDelay: 1000,
    useExponentialBackoff: true,
    retryableStatusCodes: [408, 429, 500, 502, 503, 504],
}));
// Add error logging
exports.apiClient.addErrorInterceptor((0, api_client_1.errorLoggingInterceptor)({
    level: 'error',
    logDetails: true,
}));
// Authentication setup
const setupAuthentication = (token) => {
    // Store token in localStorage
    localStorage.setItem('auth_token', token);
    // Add bearer token interceptor
    exports.apiClient.addRequestInterceptor((0, api_client_1.bearerTokenInterceptor)(token));
};
exports.setupAuthentication = setupAuthentication;
// Initialize authentication from localStorage
const initializeAuth = () => {
    const token = localStorage.getItem('auth_token');
    if (token) {
        (0, exports.setupAuthentication)(token);
    }
};
exports.initializeAuth = initializeAuth;
// Logout function
const logout = () => {
    localStorage.removeItem('auth_token');
    window.location.href = '/login';
};
exports.logout = logout;
exports.userApi = {
    /**
    * Get the current user's profile
    */
    getProfile: async () => {
        const response = await exports.apiClient.get('/user/profile');
        return response.data;
    },
    /**
    * Update the current user's profile
    */
    updateProfile: async (data) => {
        const response = await exports.apiClient.put('/user/profile', data);
        return response.data;
    },
    /**
    * Get the current user's settings
    */
    getSettings: async () => {
        const response = await exports.apiClient.get('/user/settings');
        return response.data;
    },
    /**
    * Update the current user's settings
    */
    updateSettings: async (data) => {
        const response = await exports.apiClient.put('/user/settings', data);
        return response.data;
    },
};
exports.contentApi = {
    /**
    * Get a list of articles
    */
    getArticles: async (page = 1, limit = 10) => {
        const response = await exports.apiClient.get('/articles', {
            params: { page, limit },
        });
        return response.data;
    },
    /**
    * Get a single article by ID
    */
    getArticle: async (id) => {
        const response = await exports.apiClient.get(`/articles/${id}`);
        return response.data;
    },
    /**
    * Create a new article
    */
    createArticle: async (data) => {
        const response = await exports.apiClient.post('/articles', data);
        return response.data;
    },
    /**
    * Update an existing article
    */
    updateArticle: async (id, data) => {
        const response = await exports.apiClient.put(`/articles/${id}`, data);
        return response.data;
    },
    /**
    * Delete an article
    */
    deleteArticle: async (id) => {
        await exports.apiClient.delete(`/articles/${id}`);
    },
};
// Initialize authentication on import
(0, exports.initializeAuth)();
// Extend the API client with RFP methods
exports.rfpApi = {
    /**
     * Upload an RFP document for analysis
     */
    uploadDocument: async (file) => {
        const formData = new FormData();
        formData.append('file', file);
        const response = await fetch('/api/rfp/upload', {
            method: 'POST',
            body: formData,
        });
        if (!response.ok) {
            throw new Error(`Upload failed: ${response.statusText}`);
        }
        return response.json();
    },
    /**
     * Get all RFP analyses
     */
    getAnalyses: async () => {
        const response = await exports.apiClient.get('/rfp/analyses');
        return response.data;
    },
    /**
     * Get a specific analysis by ID
     */
    getAnalysisById: async (id) => {
        const response = await exports.apiClient.get(`/rfp/analyses/${id}`);
        return response.data;
    },
    /**
     * Generate RFP response based on analysis
     */
    generateRFPResponse: async (analysisId) => {
        const response = await exports.apiClient.post('/rfp/generate', { analysisId });
        return response.data;
    },
};
// Agent API for agent management
exports.agentApi = {
    /**
     * Get agent details by ID
     */
    getAgentDetails: async (agentId) => {
        const response = await exports.apiClient.get(`/agents/${agentId}`);
        return response.data;
    },
    /**
     * Get all agents
     */
    getAgents: async () => {
        const response = await exports.apiClient.get('/agents');
        return response.data;
    },
};
// Enhanced API client with all methods combined
exports.enhancedApiClient = {
    ...exports.apiClient,
    ...exports.rfpApi,
    ...exports.agentApi,
    // Alias for backward compatibility
    getAgentDetails: exports.agentApi.getAgentDetails,
    uploadDocument: exports.rfpApi.uploadDocument,
    getAnalyses: exports.rfpApi.getAnalyses,
    getAnalysisById: exports.rfpApi.getAnalysisById,
    generateRFPResponse: exports.rfpApi.generateRFPResponse,
};
// Export the API client for direct use
exports.default = exports.enhancedApiClient;
//# sourceMappingURL=api.js.map