import { dispatchFetch } from './dispatch-fetch.js';
import { getLogger } from '../logging/logger.js';
/**
 * API Key authentication strategy.
 *
 * Supports two modes:
 * - Bearer token: When headerName is 'Authorization', formats as 'Bearer {key}'
 * - Direct key: For other headers (e.g., 'x-api-key'), sets the key directly
 *
 * @example
 * // For MRT API (Bearer token)
 * const auth = new ApiKeyStrategy(apiKey, 'Authorization');
 * // Sets: Authorization: Bearer {apiKey}
 *
 * @example
 * // For custom API key header
 * const auth = new ApiKeyStrategy(apiKey, 'x-api-key');
 * // Sets: x-api-key: {apiKey}
 */
export class ApiKeyStrategy {
    headerValue;
    headerName;
    /**
     * Creates a new ApiKeyStrategy instance for API key authentication.
     *
     * @param key - The API key value to use for authentication
     * @param headerName - The HTTP header name to populate with the API key. Defaults to 'x-api-key'. When set to 'Authorization', the key will be formatted as a Bearer token ('Bearer {key}'); for other header names, the key is set directly.
     */
    constructor(key, headerName = 'x-api-key') {
        const logger = getLogger();
        this.headerName = headerName;
        // For Authorization header, use Bearer prefix (standard for MRT API)
        // For other headers (like x-api-key), use the key directly
        this.headerValue = headerName === 'Authorization' ? `Bearer ${key}` : key;
        // Show partial key for identification (first 8 chars)
        const keyPreview = key.length > 8 ? `${key.slice(0, 8)}...` : key;
        logger.debug({ headerName, keyPreview }, `[Auth] Using API Key authentication (${headerName}): ${keyPreview}`);
    }
    async fetch(url, init = {}) {
        const headers = new Headers(init.headers);
        headers.set(this.headerName, this.headerValue);
        // Pass through dispatcher for TLS/mTLS support (see dispatchFetch)
        return dispatchFetch(url, { ...init, headers });
    }
    /**
     * Returns the authorization header value for use with openapi-fetch middleware.
     */
    async getAuthorizationHeader() {
        return this.headerValue;
    }
}
//# sourceMappingURL=api-key.js.map