/*
 * 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
 */
/**
 * Managed Runtime (MRT) API client for B2C Commerce.
 *
 * Provides a fully typed client for Managed Runtime API operations using
 * openapi-fetch with API key authentication middleware. Used for
 * managing deployments, bundles, projects, and environments.
 *
 * @module clients/mrt
 */
import createClient, {} from 'openapi-fetch';
import { createAuthMiddleware, createLoggingMiddleware, createRateLimitMiddleware } from './middleware.js';
import { globalMiddlewareRegistry } from './middleware-registry.js';
/**
 * Default MRT API origin.
 */
export const DEFAULT_MRT_ORIGIN = 'https://cloud.mobify.com';
/**
 * Creates a typed Managed Runtime 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.
 *
 * @param config - MRT client configuration
 * @param auth - Authentication strategy (typically ApiKeyStrategy)
 * @returns Typed openapi-fetch client
 *
 * @example
 * // Create MRT client with API key auth
 * const apiKeyStrategy = new ApiKeyStrategy(apiKey, 'Authorization');
 *
 * const client = createMrtClient({}, apiKeyStrategy);
 *
 * // Push a bundle to a project
 * const { data, error } = await client.POST('/api/projects/{projectSlug}/builds/', {
 *   params: {
 *     path: { projectSlug: 'my-project' }
 *   },
 *   body: {
 *     message: 'My bundle',
 *     encoding: 'base64',
 *     data: bundleData,
 *     ssr_parameters: {},
 *     ssr_only: ['ssr.js'],
 *     ssr_shared: ['shared.js']
 *   }
 * });
 *
 * @example
 * // List all projects
 * const { data, error } = await client.GET('/api/projects/', {});
 *
 * @example
 * // Get a specific target/environment
 * const { data, error } = await client.GET('/api/projects/{projectSlug}/target/{targetId}/', {
 *   params: { path: { projectSlug: 'my-project', targetId: 'staging' } }
 * });
 */
export function createMrtClient(config, auth) {
    let origin = config.origin || DEFAULT_MRT_ORIGIN;
    const registry = config.middlewareRegistry ?? globalMiddlewareRegistry;
    // Normalize origin: add https:// if no protocol specified.
    // The config resolver also normalizes, but this is a safety net for direct SDK usage.
    if (origin && !origin.startsWith('http://') && !origin.startsWith('https://')) {
        origin = `https://${origin}`;
    }
    const client = createClient({
        baseUrl: origin,
    });
    // Core middleware: auth first
    client.use(createAuthMiddleware(auth));
    // Plugin middleware from registry
    for (const middleware of registry.getMiddleware('mrt')) {
        client.use(middleware);
    }
    // Rate limiting middleware (retries on 429 using Retry-After/header-based backoff)
    client.use(createRateLimitMiddleware({
        prefix: 'MRT',
    }));
    // Logging middleware last (sees complete request with all modifications)
    client.use(createLoggingMiddleware({
        prefix: 'MRT',
        // Mask large base64-encoded bundle data in logs
        maskBodyKeys: ['data'],
    }));
    return client;
}
//# sourceMappingURL=mrt.js.map