import { of } from 'rxjs';
import { delay, mergeMap } from 'rxjs/operators';
export function batchWriteItemsWriteAll(dynamoRx, params, backoffTimer, throttleTimeSlot) {
    return dynamoRx.batchWriteItem(params)
        .pipe(mergeMap(response => {
        if (hasUnprocessedItems(response)) {
            return of(response.UnprocessedItems)
                .pipe(delay(backoffTimer.next().value * throttleTimeSlot), mergeMap((unprocessedKeys) => {
                const nextParams = Object.assign({}, params, { RequestItems: unprocessedKeys });
                return batchWriteItemsWriteAll(dynamoRx, nextParams, backoffTimer, throttleTimeSlot);
            }));
        }
        return of(response);
    }));
}
export function hasUnprocessedItems(response) {
    if (!response.UnprocessedItems) {
        return false;
    }
    return Object.values(response.UnprocessedItems)
        .some(t => !!t && t.length > 0);
}
//# sourceMappingURL=batch-write-utils.js.map