import { createMrtClient, DEFAULT_MRT_ORIGIN } from '../../clients/mrt.js';
import { getLogger } from '../../logging/logger.js';
/**
 * Lists access control headers for an MRT environment.
 *
 * @param options - List options
 * @param auth - Authentication strategy (ApiKeyStrategy)
 * @returns Paginated list of headers
 * @throws Error if request fails
 *
 * @example
 * ```typescript
 * import { ApiKeyStrategy } from '@salesforce/b2c-tooling-sdk/auth';
 * import { listAccessControlHeaders } from '@salesforce/b2c-tooling-sdk/operations/mrt';
 *
 * const auth = new ApiKeyStrategy(process.env.MRT_API_KEY!, 'Authorization');
 *
 * const result = await listAccessControlHeaders({
 *   projectSlug: 'my-storefront',
 *   targetSlug: 'production'
 * }, auth);
 *
 * console.log(`Found ${result.count} access control headers`);
 * ```
 */
export async function listAccessControlHeaders(options, auth) {
    const logger = getLogger();
    const { projectSlug, targetSlug, limit, offset, origin } = options;
    logger.debug({ projectSlug, targetSlug }, '[MRT] Listing access control headers');
    const client = createMrtClient({ origin: origin || DEFAULT_MRT_ORIGIN }, auth);
    const { data, error } = await client.GET('/api/projects/{project_slug}/target/{target_slug}/access-control-header/', {
        params: {
            path: { project_slug: projectSlug, target_slug: targetSlug },
            query: {
                limit,
                offset,
            },
        },
    });
    if (error) {
        const errorMessage = typeof error === 'object' && error !== null && 'message' in error
            ? String(error.message)
            : JSON.stringify(error);
        throw new Error(`Failed to list access control headers: ${errorMessage}`);
    }
    logger.debug({ count: data.count }, '[MRT] Access control headers listed');
    return {
        count: data.count ?? 0,
        next: data.next ?? null,
        previous: data.previous ?? null,
        headers: data.results ?? [],
    };
}
/**
 * Creates an access control header for an MRT environment.
 *
 * @param options - Create options
 * @param auth - Authentication strategy (ApiKeyStrategy)
 * @returns The created header
 * @throws Error if request fails
 *
 * @example
 * ```typescript
 * import { ApiKeyStrategy } from '@salesforce/b2c-tooling-sdk/auth';
 * import { createAccessControlHeader } from '@salesforce/b2c-tooling-sdk/operations/mrt';
 *
 * const auth = new ApiKeyStrategy(process.env.MRT_API_KEY!, 'Authorization');
 *
 * const header = await createAccessControlHeader({
 *   projectSlug: 'my-storefront',
 *   targetSlug: 'production',
 *   value: 'my-secret-header-value'
 * }, auth);
 *
 * console.log(`Created access control header: ${header.id}`);
 * ```
 */
export async function createAccessControlHeader(options, auth) {
    const logger = getLogger();
    const { projectSlug, targetSlug, value, origin } = options;
    logger.debug({ projectSlug, targetSlug }, '[MRT] Creating access control header');
    const client = createMrtClient({ origin: origin || DEFAULT_MRT_ORIGIN }, auth);
    const { data, error } = await client.POST('/api/projects/{project_slug}/target/{target_slug}/access-control-header/', {
        params: {
            path: { project_slug: projectSlug, target_slug: targetSlug },
        },
        body: {
            value,
        },
    });
    if (error) {
        const errorMessage = typeof error === 'object' && error !== null && 'message' in error
            ? String(error.message)
            : JSON.stringify(error);
        throw new Error(`Failed to create access control header: ${errorMessage}`);
    }
    logger.debug({ id: data.id }, '[MRT] Access control header created');
    return data;
}
/**
 * Gets an access control header from an MRT environment.
 *
 * @param options - Get options
 * @param auth - Authentication strategy (ApiKeyStrategy)
 * @returns The header
 * @throws Error if request fails
 */
export async function getAccessControlHeader(options, auth) {
    const logger = getLogger();
    const { projectSlug, targetSlug, headerId, origin } = options;
    logger.debug({ projectSlug, targetSlug, headerId }, '[MRT] Getting access control header');
    const client = createMrtClient({ origin: origin || DEFAULT_MRT_ORIGIN }, auth);
    const { data, error } = await client.GET('/api/projects/{project_slug}/target/{target_slug}/access-control-header/{id}/', {
        params: {
            path: { project_slug: projectSlug, target_slug: targetSlug, id: headerId },
        },
    });
    if (error) {
        const errorMessage = typeof error === 'object' && error !== null && 'message' in error
            ? String(error.message)
            : JSON.stringify(error);
        throw new Error(`Failed to get access control header: ${errorMessage}`);
    }
    logger.debug({ id: data.id }, '[MRT] Access control header retrieved');
    return data;
}
/**
 * Deletes an access control header from an MRT environment.
 *
 * @param options - Delete options
 * @param auth - Authentication strategy (ApiKeyStrategy)
 * @throws Error if request fails
 */
export async function deleteAccessControlHeader(options, auth) {
    const logger = getLogger();
    const { projectSlug, targetSlug, headerId, origin } = options;
    logger.debug({ projectSlug, targetSlug, headerId }, '[MRT] Deleting access control header');
    const client = createMrtClient({ origin: origin || DEFAULT_MRT_ORIGIN }, auth);
    const { error } = await client.DELETE('/api/projects/{project_slug}/target/{target_slug}/access-control-header/{id}/', {
        params: {
            path: { project_slug: projectSlug, target_slug: targetSlug, id: headerId },
        },
    });
    if (error) {
        const errorMessage = typeof error === 'object' && error !== null && 'message' in error
            ? String(error.message)
            : JSON.stringify(error);
        throw new Error(`Failed to delete access control header: ${errorMessage}`);
    }
    logger.debug({ headerId }, '[MRT] Access control header deleted');
}
//# sourceMappingURL=access-control.js.map