import { createMrtClient, DEFAULT_MRT_ORIGIN } from '../../clients/mrt.js';
import { getLogger } from '../../logging/logger.js';
/**
 * Lists deployment history for an MRT environment.
 *
 * @param options - List options including project and target slugs
 * @param auth - Authentication strategy (ApiKeyStrategy)
 * @returns Paginated list of deployments
 * @throws Error if request fails
 *
 * @example
 * ```typescript
 * import { ApiKeyStrategy } from '@salesforce/b2c-tooling-sdk/auth';
 * import { listDeployments } from '@salesforce/b2c-tooling-sdk/operations/mrt';
 *
 * const auth = new ApiKeyStrategy(process.env.MRT_API_KEY!, 'Authorization');
 *
 * const result = await listDeployments({
 *   projectSlug: 'my-storefront',
 *   targetSlug: 'staging'
 * }, auth);
 *
 * for (const deploy of result.deployments) {
 *   console.log(`Bundle ${deploy.bundle_id}: ${deploy.status}`);
 * }
 * ```
 */
export async function listDeployments(options, auth) {
    const logger = getLogger();
    const { projectSlug, targetSlug, limit, offset, origin } = options;
    logger.debug({ projectSlug, targetSlug }, '[MRT] Listing deployments');
    const client = createMrtClient({ origin: origin || DEFAULT_MRT_ORIGIN }, auth);
    const { data, error } = await client.GET('/api/projects/{project_slug}/target/{target_slug}/deploy/', {
        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 deployments: ${errorMessage}`);
    }
    logger.debug({ count: data.count }, '[MRT] Deployments listed');
    return {
        count: data.count ?? 0,
        next: data.next ?? null,
        previous: data.previous ?? null,
        deployments: data.results ?? [],
    };
}
/**
 * Deploys a bundle to an MRT environment.
 *
 * This endpoint is asynchronous - the deployment will happen in the background.
 * Request the target for progress updates.
 *
 * @param options - Deployment options
 * @param auth - Authentication strategy (ApiKeyStrategy)
 * @returns Initial deployment status
 * @throws Error if request fails
 *
 * @example
 * ```typescript
 * import { ApiKeyStrategy } from '@salesforce/b2c-tooling-sdk/auth';
 * import { createDeployment } from '@salesforce/b2c-tooling-sdk/operations/mrt';
 *
 * const auth = new ApiKeyStrategy(process.env.MRT_API_KEY!, 'Authorization');
 *
 * const result = await createDeployment({
 *   projectSlug: 'my-storefront',
 *   targetSlug: 'staging',
 *   bundleId: 12345
 * }, auth);
 *
 * console.log(`Deployment started: ${result.status}`);
 * ```
 */
export async function createDeployment(options, auth) {
    const logger = getLogger();
    const { projectSlug, targetSlug, bundleId, origin } = options;
    logger.debug({ projectSlug, targetSlug, bundleId }, '[MRT] Creating deployment');
    const client = createMrtClient({ origin: origin || DEFAULT_MRT_ORIGIN }, auth);
    const { error } = await client.POST('/api/projects/{project_slug}/target/{target_slug}/deploy/', {
        params: {
            path: { project_slug: projectSlug, target_slug: targetSlug },
        },
        body: {
            bundle_id: bundleId,
        },
    });
    if (error) {
        const errorMessage = typeof error === 'object' && error !== null && 'message' in error
            ? String(error.message)
            : JSON.stringify(error);
        throw new Error(`Failed to create deployment: ${errorMessage}`);
    }
    logger.debug({ bundleId }, '[MRT] Deployment created');
    return {
        bundleId,
        targetSlug,
        status: 'pending',
    };
}
//# sourceMappingURL=deployment.js.map