import { createMrtClient, DEFAULT_MRT_ORIGIN } from '../../clients/mrt.js';
import { getLogger } from '../../logging/logger.js';
/**
 * Invalidates cached objects in the CDN for an MRT environment.
 *
 * Cache invalidations are asynchronous and usually complete within two minutes.
 *
 * @param options - Invalidation options
 * @param auth - Authentication strategy (ApiKeyStrategy)
 * @returns Invalidation result
 * @throws Error if request fails
 *
 * @example
 * ```typescript
 * import { ApiKeyStrategy } from '@salesforce/b2c-tooling-sdk/auth';
 * import { invalidateCache } from '@salesforce/b2c-tooling-sdk/operations/mrt';
 *
 * const auth = new ApiKeyStrategy(process.env.MRT_API_KEY!, 'Authorization');
 *
 * // Invalidate all cached paths
 * const result = await invalidateCache({
 *   projectSlug: 'my-storefront',
 *   targetSlug: 'production',
 *   pattern: '/*'
 * }, auth);
 *
 * console.log(result.result);
 *
 * // Invalidate specific path
 * const result2 = await invalidateCache({
 *   projectSlug: 'my-storefront',
 *   targetSlug: 'production',
 *   pattern: '/products/*'
 * }, auth);
 * ```
 */
export async function invalidateCache(options, auth) {
    const logger = getLogger();
    const { projectSlug, targetSlug, pattern, origin } = options;
    logger.debug({ projectSlug, targetSlug, pattern }, '[MRT] Invalidating cache');
    const client = createMrtClient({ origin: origin || DEFAULT_MRT_ORIGIN }, auth);
    const { data, error } = await client.POST('/api/projects/{project_slug}/target/{target_slug}/invalidation/', {
        params: {
            path: { project_slug: projectSlug, target_slug: targetSlug },
        },
        body: {
            pattern,
            items: null, // Deprecated but required by schema
        },
    });
    if (error) {
        const errorMessage = typeof error === 'object' && error !== null && 'message' in error
            ? String(error.message)
            : JSON.stringify(error);
        throw new Error(`Failed to invalidate cache: ${errorMessage}`);
    }
    // Response has different fields than request schema indicates
    const response = data;
    logger.debug({ pattern }, '[MRT] Cache invalidation started');
    return {
        result: response.result ?? 'Cache invalidation is in progress.',
        slug: response.slug ?? targetSlug,
    };
}
//# sourceMappingURL=cache.js.map