/*
 * 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
 */
import createClient from 'openapi-fetch';
import { createAuthMiddleware, createLoggingMiddleware, createRateLimitMiddleware } from './middleware.js';
import { globalMiddlewareRegistry } from './middleware-registry.js';
import { OAuthStrategy } from '../auth/oauth.js';
import { buildTenantScope, toOrganizationId, normalizeTenantId } from './custom-apis.js';
export { toOrganizationId, normalizeTenantId, buildTenantScope };
/**
 * Creates a Granular Replications API client for publishing individual items.
 *
 * The Granular Replications API enables programmatic publishing of individual items
 * (products, price tables, content assets) from staging to production environments.
 *
 * @param config - Client configuration with shortCode and tenantId
 * @param auth - OAuth authentication strategy
 * @returns Typed Granular Replications API client
 *
 * @example
 * ```typescript
 * import {createGranularReplicationsClient, OAuthStrategy} from '@salesforce/b2c-tooling-sdk';
 *
 * const auth = new OAuthStrategy({
 *   clientId: 'your-client-id',
 *   clientSecret: 'your-client-secret',
 *   tokenEndpoint: 'https://account.demandware.com/dwsso/oauth2/access_token'
 * });
 *
 * const client = createGranularReplicationsClient({
 *   shortCode: 'kv7kzm78',
 *   tenantId: 'zzxy_prd'
 * }, auth);
 *
 * // Queue a product for publishing
 * const result = await client.POST('/organizations/{organizationId}/granular-processes', {
 *   params: {path: {organizationId: 'f_ecom_zzxy_prd'}},
 *   body: {product: {productId: 'PROD-123'}}
 * });
 *
 * // List all publish processes
 * const processes = await client.GET('/organizations/{organizationId}/granular-processes', {
 *   params: {
 *     path: {organizationId: 'f_ecom_zzxy_prd'},
 *     query: {limit: 20, offset: 0}
 *   }
 * });
 * ```
 *
 * @see https://developer.salesforce.com/docs/commerce/commerce-api/references/replications
 */
export function createGranularReplicationsClient(config, auth) {
    const registry = config.middlewareRegistry ?? globalMiddlewareRegistry;
    const client = createClient({
        baseUrl: `https://${config.shortCode}.api.commercecloud.salesforce.com/operation/replications/v1`,
    });
    // Build required scopes: domain scope + tenant-specific scope
    const requiredScopes = config.scopes ?? ['sfcc.granular-replications.rw', buildTenantScope(config.tenantId)];
    const scopedAuth = auth instanceof OAuthStrategy ? auth.withAdditionalScopes(requiredScopes) : auth;
    client.use(createAuthMiddleware(scopedAuth));
    for (const middleware of registry.getMiddleware('granular-replications')) {
        client.use(middleware);
    }
    client.use(createRateLimitMiddleware({ prefix: 'GRANULAR-REPLICATIONS' }));
    client.use(createLoggingMiddleware('GRANULAR-REPLICATIONS'));
    return client;
}
//# sourceMappingURL=granular-replications.js.map