/*
 * Copyright OpenSearch Contributors
 * SPDX-License-Identifier: Apache-2.0
 */
/**
 * Data Source Configuration - Frontend API Client
 *
 * This module provides API calls to manage server-side configuration.
 * Credentials are stored on the server (in agent-health.config.json), NOT in browser.
 *
 * Security: No credentials are ever stored in localStorage or sent via headers.
 */
import { ENV_CONFIG } from '../lib/config.js';
// Default OTEL index patterns (kept for UI defaults)
export const DEFAULT_OTEL_INDEXES = {
    traces: 'otel-v1-apm-span-*',
    logs: 'ml-commons-logs-*',
    metrics: 'otel-v1-apm-service-map*',
};
// API base URL
const API_BASE = ENV_CONFIG.backendUrl;
// ============================================================================
// Config Status API
// ============================================================================
/**
 * Get configuration status from the server
 * Returns source (file/environment/none) and endpoints, never credentials
 */
export async function getConfigStatus() {
    const response = await fetch(`${API_BASE}/api/storage/config/status`);
    if (!response.ok) {
        throw new Error('Failed to get config status');
    }
    return response.json();
}
export async function saveStorageConfig(config) {
    const response = await fetch(`${API_BASE}/api/storage/config/storage`, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({
            endpoint: config.endpoint,
            authType: config.authType || undefined,
            username: config.username || undefined,
            password: config.password || undefined,
            awsProfile: config.awsProfile || undefined,
            awsRegion: config.awsRegion || undefined,
            awsService: config.awsService || undefined,
            tlsSkipVerify: config.tlsSkipVerify,
        }),
    });
    if (!response.ok) {
        const error = await response.json().catch(() => ({ error: 'Unknown error' }));
        throw new Error(error.error || 'Failed to save storage configuration');
    }
    return response.json();
}
/**
 * Clear storage configuration from server
 */
export async function clearStorageConfig() {
    const response = await fetch(`${API_BASE}/api/storage/config/storage`, {
        method: 'DELETE',
    });
    if (!response.ok) {
        const error = await response.json().catch(() => ({ error: 'Unknown error' }));
        throw new Error(error.error || 'Failed to clear storage configuration');
    }
}
// ============================================================================
// Observability Configuration API
// ============================================================================
/**
 * Save observability configuration to server (agent-health.config.json)
 */
export async function saveObservabilityConfig(config) {
    const response = await fetch(`${API_BASE}/api/storage/config/observability`, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({
            endpoint: config.endpoint,
            authType: config.authType || undefined,
            username: config.username || undefined,
            password: config.password || undefined,
            awsProfile: config.awsProfile || undefined,
            awsRegion: config.awsRegion || undefined,
            awsService: config.awsService || undefined,
            tlsSkipVerify: config.tlsSkipVerify,
            indexes: config.indexes,
        }),
    });
    if (!response.ok) {
        const error = await response.json().catch(() => ({ error: 'Unknown error' }));
        throw new Error(error.error || 'Failed to save observability configuration');
    }
}
/**
 * Clear observability configuration from server
 */
export async function clearObservabilityConfig() {
    const response = await fetch(`${API_BASE}/api/storage/config/observability`, {
        method: 'DELETE',
    });
    if (!response.ok) {
        const error = await response.json().catch(() => ({ error: 'Unknown error' }));
        throw new Error(error.error || 'Failed to clear observability configuration');
    }
}
// ============================================================================
// Storage Runtime Control API
// ============================================================================
/**
 * Retry storage connection using current config (file or env).
 * Used when config was edited externally or after fixing connectivity.
 */
export async function retryStorageConnection() {
    const response = await fetch(`${API_BASE}/api/storage/config/retry`, {
        method: 'POST',
    });
    if (!response.ok) {
        const error = await response.json().catch(() => ({ error: 'Unknown error' }));
        throw new Error(error.error || 'Failed to retry storage connection');
    }
    return response.json();
}
/**
 * Force file storage regardless of config file contents.
 */
export async function useFileStorage() {
    const response = await fetch(`${API_BASE}/api/storage/config/use-file-storage`, {
        method: 'POST',
    });
    if (!response.ok) {
        const error = await response.json().catch(() => ({ error: 'Unknown error' }));
        throw new Error(error.error || 'Failed to switch to file storage');
    }
}
// ============================================================================
// Legacy Compatibility Stubs
// These functions are no-ops or return empty values for backwards compatibility
// with any code that might still import them during transition
// ============================================================================
/** @deprecated No longer used - configuration is server-side only */
export function loadDataSourceConfig() {
    return null;
}
/** @deprecated No longer used - configuration is server-side only */
export function clearDataSourceConfig() {
    // No-op - use clearStorageConfig() and clearObservabilityConfig() instead
}
/** @deprecated No longer used - credentials are not sent via headers */
export function getStorageConfigHeaders() {
    return {};
}
/** @deprecated No longer used - credentials are not sent via headers */
export function getObservabilityConfigHeaders() {
    return {};
}
/** @deprecated No longer used - credentials not stored in browser */
export function hasStorageCredentials() {
    return false;
}
/** @deprecated No longer used - credentials not stored in browser */
export function hasObservabilityCredentials() {
    return false;
}
//# sourceMappingURL=dataSourceConfig.js.map