import { createMrtClient, DEFAULT_MRT_ORIGIN } from '../../clients/mrt.js';
import { getLogger } from '../../logging/logger.js';
/**
 * Lists notifications for an MRT project.
 *
 * @param options - List options including project slug
 * @param auth - Authentication strategy (ApiKeyStrategy)
 * @returns Paginated list of notifications
 * @throws Error if request fails
 *
 * @example
 * ```typescript
 * import { ApiKeyStrategy } from '@salesforce/b2c-tooling-sdk/auth';
 * import { listNotifications } from '@salesforce/b2c-tooling-sdk/operations/mrt';
 *
 * const auth = new ApiKeyStrategy(process.env.MRT_API_KEY!, 'Authorization');
 *
 * const result = await listNotifications({
 *   projectSlug: 'my-storefront'
 * }, auth);
 *
 * for (const notification of result.notifications) {
 *   console.log(`Notification ${notification.id}`);
 * }
 * ```
 */
export async function listNotifications(options, auth) {
    const logger = getLogger();
    const { projectSlug, limit, offset, targetSlug, origin } = options;
    logger.debug({ projectSlug }, '[MRT] Listing notifications');
    const client = createMrtClient({ origin: origin || DEFAULT_MRT_ORIGIN }, auth);
    const { data, error } = await client.GET('/api/projects/{project_slug}/notifications/', {
        params: {
            path: { project_slug: projectSlug },
            query: {
                limit,
                offset,
                targets__slug: targetSlug,
            },
        },
    });
    if (error) {
        const errorMessage = typeof error === 'object' && error !== null && 'message' in error
            ? String(error.message)
            : JSON.stringify(error);
        throw new Error(`Failed to list notifications: ${errorMessage}`);
    }
    logger.debug({ count: data.count }, '[MRT] Notifications listed');
    return {
        count: data.count ?? 0,
        next: data.next ?? null,
        previous: data.previous ?? null,
        notifications: data.results ?? [],
    };
}
/**
 * Creates a notification for an MRT project.
 *
 * @param options - Create notification options
 * @param auth - Authentication strategy (ApiKeyStrategy)
 * @returns The created notification
 * @throws Error if request fails
 *
 * @example
 * ```typescript
 * import { ApiKeyStrategy } from '@salesforce/b2c-tooling-sdk/auth';
 * import { createNotification } from '@salesforce/b2c-tooling-sdk/operations/mrt';
 *
 * const auth = new ApiKeyStrategy(process.env.MRT_API_KEY!, 'Authorization');
 *
 * const notification = await createNotification({
 *   projectSlug: 'my-storefront',
 *   targets: ['staging', 'production'],
 *   recipients: ['team@example.com'],
 *   deploymentStart: true,
 *   deploymentFailed: true
 * }, auth);
 * ```
 */
export async function createNotification(options, auth) {
    const logger = getLogger();
    const { projectSlug, targets, recipients, deploymentStart, deploymentSuccess, deploymentFailed, origin } = options;
    logger.debug({ projectSlug, targets, recipients }, '[MRT] Creating notification');
    const client = createMrtClient({ origin: origin || DEFAULT_MRT_ORIGIN }, auth);
    const body = {
        resourcetype: 'EmailNotification',
        targets,
        recipients,
        deployment_start: deploymentStart,
        deployment_success: deploymentSuccess,
        deployment_failed: deploymentFailed,
    };
    const { data, error } = await client.POST('/api/projects/{project_slug}/notifications/', {
        params: {
            path: { project_slug: projectSlug },
        },
        body,
    });
    if (error) {
        const errorMessage = typeof error === 'object' && error !== null && 'message' in error
            ? String(error.message)
            : JSON.stringify(error);
        throw new Error(`Failed to create notification: ${errorMessage}`);
    }
    logger.debug({ id: data.id }, '[MRT] Notification created');
    return data;
}
/**
 * Gets a notification from an MRT project.
 *
 * @param options - Get notification options
 * @param auth - Authentication strategy (ApiKeyStrategy)
 * @returns The notification
 * @throws Error if request fails
 */
export async function getNotification(options, auth) {
    const logger = getLogger();
    const { projectSlug, notificationId, origin } = options;
    logger.debug({ projectSlug, notificationId }, '[MRT] Getting notification');
    const client = createMrtClient({ origin: origin || DEFAULT_MRT_ORIGIN }, auth);
    const { data, error } = await client.GET('/api/projects/{project_slug}/notifications/{id}/', {
        params: {
            path: { project_slug: projectSlug, id: notificationId },
        },
    });
    if (error) {
        const errorMessage = typeof error === 'object' && error !== null && 'message' in error
            ? String(error.message)
            : JSON.stringify(error);
        throw new Error(`Failed to get notification: ${errorMessage}`);
    }
    logger.debug({ id: data.id }, '[MRT] Notification retrieved');
    return data;
}
/**
 * Updates a notification in an MRT project.
 *
 * @param options - Update notification options
 * @param auth - Authentication strategy (ApiKeyStrategy)
 * @returns The updated notification
 * @throws Error if request fails
 */
export async function updateNotification(options, auth) {
    const logger = getLogger();
    const { projectSlug, notificationId, targets, recipients, deploymentStart, deploymentSuccess, deploymentFailed, origin, } = options;
    logger.debug({ projectSlug, notificationId }, '[MRT] Updating notification');
    const client = createMrtClient({ origin: origin || DEFAULT_MRT_ORIGIN }, auth);
    const body = {
        resourcetype: 'EmailNotification',
    };
    if (targets !== undefined) {
        body.targets = targets;
    }
    if (recipients !== undefined) {
        body.recipients = recipients;
    }
    if (deploymentStart !== undefined) {
        body.deployment_start = deploymentStart;
    }
    if (deploymentSuccess !== undefined) {
        body.deployment_success = deploymentSuccess;
    }
    if (deploymentFailed !== undefined) {
        body.deployment_failed = deploymentFailed;
    }
    const { data, error } = await client.PATCH('/api/projects/{project_slug}/notifications/{id}/', {
        params: {
            path: { project_slug: projectSlug, id: notificationId },
        },
        body,
    });
    if (error) {
        const errorMessage = typeof error === 'object' && error !== null && 'message' in error
            ? String(error.message)
            : JSON.stringify(error);
        throw new Error(`Failed to update notification: ${errorMessage}`);
    }
    logger.debug({ id: data.id }, '[MRT] Notification updated');
    return data;
}
/**
 * Deletes a notification from an MRT project.
 *
 * @param options - Delete notification options
 * @param auth - Authentication strategy (ApiKeyStrategy)
 * @throws Error if request fails
 */
export async function deleteNotification(options, auth) {
    const logger = getLogger();
    const { projectSlug, notificationId, origin } = options;
    logger.debug({ projectSlug, notificationId }, '[MRT] Deleting notification');
    const client = createMrtClient({ origin: origin || DEFAULT_MRT_ORIGIN }, auth);
    const { error } = await client.DELETE('/api/projects/{project_slug}/notifications/{id}/', {
        params: {
            path: { project_slug: projectSlug, id: notificationId },
        },
    });
    if (error) {
        const errorMessage = typeof error === 'object' && error !== null && 'message' in error
            ? String(error.message)
            : JSON.stringify(error);
        throw new Error(`Failed to delete notification: ${errorMessage}`);
    }
    logger.debug({ notificationId }, '[MRT] Notification deleted');
}
//# sourceMappingURL=notification.js.map