/*
 * 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
 */
/**
 * CDN Zones API client for B2C Commerce.
 *
 * Provides a fully typed client for CDN Zones API operations using
 * openapi-fetch with OAuth authentication middleware. Used for managing
 * eCDN configurations including zones, certificates, WAF, rate limiting,
 * cache purge, and other CDN settings.
 *
 * @module clients/cdn-zones
 */
import createClient, {} from 'openapi-fetch';
import { OAuthStrategy } from '../auth/oauth.js';
import { createAuthMiddleware, createLoggingMiddleware } from './middleware.js';
import { globalMiddlewareRegistry } from './middleware-registry.js';
import { toOrganizationId, normalizeTenantId, buildTenantScope } from './custom-apis.js';
/**
 * Re-export organization/tenant utilities for convenience.
 */
export { toOrganizationId, normalizeTenantId, buildTenantScope };
/** Default OAuth scopes required for CDN Zones (read-only) */
export const CDN_ZONES_READ_SCOPES = ['sfcc.cdn-zones'];
/** OAuth scopes required for CDN Zones (read-write) */
export const CDN_ZONES_RW_SCOPES = ['sfcc.cdn-zones.rw'];
/**
 * Creates a typed CDN Zones 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.cdn-zones` (read) or `sfcc.cdn-zones.rw` (read-write)
 * - Tenant scope: `SALESFORCE_COMMERCE_API:{tenantId}`
 *
 * @param config - CDN Zones client configuration including shortCode and tenantId
 * @param auth - Authentication strategy (typically OAuth)
 * @param options - Optional settings like readWrite scope
 * @returns Typed openapi-fetch client
 *
 * @example
 * // Create CDN Zones client for read-only operations
 * const oauthStrategy = new OAuthStrategy({
 *   clientId: 'your-client-id',
 *   clientSecret: 'your-client-secret',
 * });
 *
 * const client = createCdnZonesClient(
 *   { shortCode: 'kv7kzm78', tenantId: 'zzxy_prd' },
 *   oauthStrategy
 * );
 *
 * // List all zones
 * const { data, error } = await client.GET('/organizations/{organizationId}/zones/info', {
 *   params: {
 *     path: { organizationId: toOrganizationId('zzxy_prd') }
 *   }
 * });
 *
 * @example
 * // Create CDN Zones client for read-write operations (e.g., cache purge)
 * const rwClient = createCdnZonesClient(
 *   { shortCode: 'kv7kzm78', tenantId: 'zzxy_prd' },
 *   oauthStrategy,
 *   { readWrite: true }
 * );
 *
 * // Purge cache
 * const { data } = await rwClient.POST('/organizations/{organizationId}/zones/{zoneId}/cachepurge', {
 *   params: {
 *     path: { organizationId: toOrganizationId('zzxy_prd'), zoneId: 'zone-id' }
 *   },
 *   body: { path: ['/some/path'] }
 * });
 */
export function createCdnZonesClient(config, auth, options) {
    const registry = config.middlewareRegistry ?? globalMiddlewareRegistry;
    const client = createClient({
        baseUrl: `https://${config.shortCode}.api.commercecloud.salesforce.com/cdn/zones/v1`,
    });
    // Build required scopes: domain scope + tenant-specific scope
    const domainScopes = options?.readWrite ? CDN_ZONES_RW_SCOPES : CDN_ZONES_READ_SCOPES;
    const requiredScopes = config.scopes ?? [...domainScopes, buildTenantScope(config.tenantId)];
    // If OAuth strategy, add required scopes; otherwise use as-is
    const scopedAuth = auth instanceof OAuthStrategy ? auth.withAdditionalScopes(requiredScopes) : auth;
    // Core middleware: auth first
    client.use(createAuthMiddleware(scopedAuth));
    // Plugin middleware from registry
    for (const middleware of registry.getMiddleware('cdn-zones')) {
        client.use(middleware);
    }
    // Logging middleware last (sees complete request with all modifications)
    client.use(createLoggingMiddleware('CDN-ZONES'));
    return client;
}
//# sourceMappingURL=cdn-zones.js.map