/*
 * Copyright (c) 2025, Salesforce, Inc.
 * SPDX-License-Identifier: Apache-2
 * For full license text, see the license.txt file in the repo root or http://www.apache.org/licenses/LICENSE-2.0
 */
/**
 * Metrics API client for B2C Commerce (Observability).
 *
 * Provides a fully typed client for the SCAPI Observability Metrics API
 * (`observability/metrics/v1`) using openapi-fetch with OAuth authentication
 * middleware. The API exposes time-series operational metrics (overall, sales,
 * eCDN, third-party services, SCAPI, SCAPI hooks, MRT, controllers, and OCAPI)
 * for an organization.
 *
 * @module clients/metrics
 */
import createClient, {} from 'openapi-fetch';
import { OAuthStrategy } from '../auth/oauth.js';
import { JwtOAuthStrategy } from '../auth/oauth-jwt.js';
import { createAuthMiddleware, createLoggingMiddleware } from './middleware.js';
import { globalMiddlewareRegistry } from './middleware-registry.js';
import { buildTenantScope } from './custom-apis.js';
/** Default OAuth scope required for the Metrics API (read-only). */
export const METRICS_DEFAULT_SCOPES = ['sfcc.metrics'];
/**
 * Creates a typed Metrics API client.
 *
 * Returns the openapi-fetch client directly, with authentication handled via
 * middleware. This gives full access to all openapi-fetch features with
 * type-safe paths, parameters, and responses.
 *
 * The client automatically handles OAuth scope requirements:
 * - Domain scope: `sfcc.metrics` (or custom via config.scopes)
 * - Tenant scope: `SALESFORCE_COMMERCE_API:{tenantId}`
 *
 * @param config - Metrics client configuration including shortCode and tenantId
 * @param auth - Authentication strategy (typically OAuth client-credentials)
 * @returns Typed openapi-fetch client
 *
 * @example
 * const auth = new OAuthStrategy({ clientId: '...', clientSecret: '...' });
 * const client = createMetricsClient({ shortCode: 'kv7kzm78', tenantId: 'zzxy_prd' }, auth);
 */
export function createMetricsClient(config, auth) {
    const registry = config.middlewareRegistry ?? globalMiddlewareRegistry;
    const client = createClient({
        baseUrl: `https://${config.shortCode}.api.commercecloud.salesforce.com/observability/metrics/v1`,
    });
    // Build required scopes: domain scope + tenant-specific scope
    const requiredScopes = config.scopes ?? [...METRICS_DEFAULT_SCOPES, buildTenantScope(config.tenantId)];
    // If auth supports scopes, add required scopes; otherwise use as-is
    const scopedAuth = auth instanceof OAuthStrategy || auth instanceof JwtOAuthStrategy
        ? auth.withAdditionalScopes(requiredScopes)
        : auth;
    // Core middleware: auth first
    client.use(createAuthMiddleware(scopedAuth));
    // Plugin middleware from registry
    for (const middleware of registry.getMiddleware('metrics')) {
        client.use(middleware);
    }
    // Logging middleware last (sees complete request with all modifications)
    client.use(createLoggingMiddleware('METRICS'));
    return client;
}
//# sourceMappingURL=metrics.js.map