import messagingService from './messaging.service';
export class ApiService {
    constructor() {
        this.config = {
            apiKey: '',
            apiBaseUrl: '',
            exporterEndpoint: '',
        };
    }
    /**
     * Initialize the API service
     * @param config - API service configuration
     */
    init(config) {
        this.config = {
            ...this.config,
            ...config,
        };
    }
    /**
     * Update the API service configuration
     * @param config - Partial configuration to update
     */
    updateConfigs(config) {
        this.config = { ...this.config, ...config };
    }
    /**
     * Start a new debug session
     * @param request - Session start request data
     * @param signal - Optional AbortSignal for request cancellation
     */
    startSession(request, signal) {
        return this.makeRequest('/debug-sessions/start', 'POST', request, signal);
    }
    /**
     * Create a new error span session
     * @param request - Session create error span request data
     * @param signal - Optional AbortSignal for request cancellation
     */
    createErrorSession(request, signal) {
        return this.makeRequest('/debug-sessions/error-span/start', 'POST', request, signal);
    }
    /**
     * Stop an active debug session
     * @param sessionId - ID of the session to stop
     * @param request - Session stop request data
     */
    stopSession(sessionId, request) {
        return this.makeRequest(`/debug-sessions/${sessionId}/stop`, 'PATCH', request);
    }
    /**
     * Cancel an active debug session
     * @param sessionId - ID of the session to cancel
     */
    cancelSession(sessionId) {
        return this.makeRequest(`/debug-sessions/${sessionId}/cancel`, 'DELETE');
    }
    /**
     * Start a new debug session
     * @param request - Session start request data
     * @param signal - Optional AbortSignal for request cancellation
     */
    startContinuousDebugSession(request, signal) {
        return this.makeRequest('/continuous-debug-sessions/start', 'POST', request, signal);
    }
    /**
     * Save a continuous debug session
     * @param sessionId - ID of the session to save
     * @param request - Session save request data
     * @param signal - Optional AbortSignal for request cancellation
     */
    saveContinuousDebugSession(sessionId, request, signal) {
        return this.makeRequest(`/continuous-debug-sessions/${sessionId}/save`, 'POST', request, signal);
    }
    /**
     * Stop an active continuous debug session
     * @param sessionId - ID of the session to stop
     */
    stopContinuousDebugSession(sessionId) {
        return this.makeRequest(`/continuous-debug-sessions/${sessionId}/cancel`, 'DELETE');
    }
    /**
     * Check debug session should be started remotely
     */
    checkRemoteSession(requestBody, signal) {
        return this.makeRequest('/remote-debug-session/check', 'POST', requestBody, signal);
    }
    updateSessionAttributes(sessionId, requestBody, signal) {
        return this.makeRequest(`/debug-sessions/${sessionId}`, 'PATCH', requestBody, signal);
    }
    /**
     * Export events to the session debugger API
     */
    exportEvents(sessionId, requestBody, signal) {
        return this.makeRequest(`/debug-sessions/${sessionId}/rrweb-events`, 'POST', requestBody, signal);
    }
    /**
     * Make a request to the session debugger API
     * @param path - API endpoint path (relative to the base URL)
     * @param method - HTTP method (GET, POST, PATCH, etc.)
     * @param body - request payload
     * @param signal - AbortSignal to set request's signal
     */
    async makeRequest(path, method, body, signal) {
        const url = `${this.config.apiBaseUrl}/v0/radar${path}`;
        const params = {
            method,
            body: body ? JSON.stringify(body) : null,
            headers: {
                'Content-Type': 'application/json',
                ...(this.config.apiKey && { 'X-Api-Key': this.config.apiKey }),
            },
        };
        try {
            const response = await fetch(url, {
                ...params,
                credentials: 'include',
                signal,
            });
            if (!response.ok) {
                throw new Error('Network response was not ok: ' + response.statusText);
            }
            if (response.status === 204) {
                return null;
            }
            return await response.json();
        }
        catch (error) {
            if ((error === null || error === void 0 ? void 0 : error.name) === 'AbortError') {
                throw new Error('Request aborted');
            }
            return this.handleRequestError(error, { url, params });
        }
    }
    async handleRequestError(error, payload) {
        if (this.config.usePostMessageFallback) {
            try {
                const response = await messagingService.sendMessagePromise('request', payload);
                return response;
            }
            catch (error) {
                throw new Error('Error making request: ' + error.message);
            }
        }
        else {
            throw new Error('Error making request: ' + error.message);
        }
    }
}
//# sourceMappingURL=api.service.js.map