/*
 * 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
 */
/**
 * ODS (On-Demand Sandbox) API client for B2C Commerce.
 *
 * Provides a fully typed client for the Developer Sandbox REST API using
 * openapi-fetch with OAuth authentication middleware. Used for managing
 * developer sandboxes including creation, deletion, start/stop operations,
 * and retrieving realm and system information.
 *
 * @module clients/ods
 */
import createClient from 'openapi-fetch';
import { createAuthMiddleware, createLoggingMiddleware, createExtraParamsMiddleware } from './middleware.js';
import { globalMiddlewareRegistry } from './middleware-registry.js';
/**
 * Default ODS API host for US region.
 */
const DEFAULT_ODS_HOST = 'admin.dx.commercecloud.salesforce.com';
/**
 * Creates a typed ODS (On-Demand Sandbox) 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 - ODS client configuration
 * @param auth - Authentication strategy (typically OAuth)
 * @returns Typed openapi-fetch client
 *
 * @example
 * // Create ODS client with OAuth auth
 * const oauthStrategy = new OAuthStrategy({
 *   clientId: 'your-client-id',
 *   clientSecret: 'your-client-secret',
 * });
 *
 * const client = createOdsClient({}, oauthStrategy);
 *
 * // Get user info
 * const { data, error } = await client.GET('/me', {});
 * if (data) {
 *   console.log('User:', data.data?.user?.name);
 *   console.log('Realms:', data.data?.realms);
 * }
 *
 * @example
 * // Get system info
 * const { data, error } = await client.GET('/system', {});
 * if (data) {
 *   console.log('Region:', data.data?.region);
 *   console.log('Sandbox IPs:', data.data?.sandboxIps);
 * }
 *
 * @example
 * // List all sandboxes
 * const { data, error } = await client.GET('/sandboxes', {});
 * if (data) {
 *   for (const sandbox of data.data ?? []) {
 *     console.log(`${sandbox.id}: ${sandbox.state}`);
 *   }
 * }
 *
 * @example
 * // Create a new sandbox
 * const { data, error } = await client.POST('/sandboxes', {
 *   body: {
 *     realm: 'abcd',
 *     ttl: 24,
 *     resourceProfile: 'medium',
 *   }
 * });
 *
 * @example
 * // Start a sandbox
 * const { data, error } = await client.POST('/sandboxes/{sandboxId}/operations', {
 *   params: { path: { sandboxId: 'sandbox-uuid' } },
 *   body: { operation: 'start' }
 * });
 */
export function createOdsClient(config, auth) {
    const host = config.host ?? DEFAULT_ODS_HOST;
    const registry = config.middlewareRegistry ?? globalMiddlewareRegistry;
    const client = createClient({
        baseUrl: `https://${host}/api/v1`,
    });
    // Core middleware: extraParams → auth
    if (config.extraParams) {
        client.use(createExtraParamsMiddleware(config.extraParams));
    }
    client.use(createAuthMiddleware(auth));
    // Plugin middleware from registry
    for (const middleware of registry.getMiddleware('ods')) {
        client.use(middleware);
    }
    // Logging middleware last (sees complete request with all modifications)
    client.use(createLoggingMiddleware('ODS'));
    return client;
}
//# sourceMappingURL=ods.js.map