/*
 * 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
 */
/**
 * SCAPI Schemas API client for B2C Commerce.
 *
 * Provides a fully typed client for SCAPI Schemas API operations using
 * openapi-fetch with OAuth authentication middleware. Used for discovering
 * and retrieving OpenAPI schema specifications for SCAPI APIs.
 *
 * @module clients/scapi-schemas
 */
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, toTenantId, buildTenantScope } from './custom-apis.js';
/**
 * Re-export organization/tenant utilities for convenience.
 */
export { toOrganizationId, toTenantId, buildTenantScope };
/** Default OAuth scopes required for SCAPI Schemas (read-only) */
export const SCAPI_SCHEMAS_DEFAULT_SCOPES = ['sfcc.scapi-schemas'];
/**
 * Creates a typed SCAPI Schemas 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.scapi-schemas` (or custom via config.scopes)
 * - Tenant scope: `SALESFORCE_COMMERCE_API:{tenantId}`
 *
 * @param config - SCAPI Schemas client configuration including shortCode and tenantId
 * @param auth - Authentication strategy (typically OAuth)
 * @returns Typed openapi-fetch client
 *
 * @example
 * // Create SCAPI Schemas client - scopes are handled automatically
 * const oauthStrategy = new OAuthStrategy({
 *   clientId: 'your-client-id',
 *   clientSecret: 'your-client-secret',
 * });
 *
 * const client = createScapiSchemasClient(
 *   { shortCode: 'kv7kzm78', tenantId: 'zzxy_prd' },
 *   oauthStrategy
 * );
 *
 * // List available SCAPI schemas
 * const { data, error } = await client.GET('/organizations/{organizationId}/schemas', {
 *   params: {
 *     path: { organizationId: toOrganizationId('zzxy_prd') }
 *   }
 * });
 *
 * // Get a specific schema
 * const { data: schema } = await client.GET(
 *   '/organizations/{organizationId}/schemas/{apiFamily}/{apiName}/{apiVersion}',
 *   {
 *     params: {
 *       path: {
 *         organizationId: toOrganizationId('zzxy_prd'),
 *         apiFamily: 'shopper',
 *         apiName: 'products',
 *         apiVersion: 'v1'
 *       },
 *       query: { expand: 'custom_properties' }
 *     }
 *   }
 * );
 */
export function createScapiSchemasClient(config, auth) {
    const registry = config.middlewareRegistry ?? globalMiddlewareRegistry;
    const client = createClient({
        baseUrl: `https://${config.shortCode}.api.commercecloud.salesforce.com/dx/scapi-schemas/v1`,
    });
    // Build required scopes: domain scope + tenant-specific scope
    const requiredScopes = config.scopes ?? [...SCAPI_SCHEMAS_DEFAULT_SCOPES, 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('scapi-schemas')) {
        client.use(middleware);
    }
    // Logging middleware last (sees complete request with all modifications)
    client.use(createLoggingMiddleware('SCAPI-SCHEMAS'));
    return client;
}
//# sourceMappingURL=scapi-schemas.js.map