"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const rxjs_1 = require("rxjs");
const operators_1 = require("rxjs/operators");
function batchWriteItemsWriteAll(dynamoRx, params, backoffTimer, throttleTimeSlot) {
    return dynamoRx.batchWriteItem(params)
        .pipe(operators_1.mergeMap(response => {
        if (hasUnprocessedItems(response)) {
            return rxjs_1.of(response.UnprocessedItems)
                .pipe(operators_1.delay(backoffTimer.next().value * throttleTimeSlot), operators_1.mergeMap((unprocessedKeys) => {
                const nextParams = Object.assign({}, params, { RequestItems: unprocessedKeys });
                return batchWriteItemsWriteAll(dynamoRx, nextParams, backoffTimer, throttleTimeSlot);
            }));
        }
        return rxjs_1.of(response);
    }));
}
exports.batchWriteItemsWriteAll = batchWriteItemsWriteAll;
function hasUnprocessedItems(response) {
    if (!response.UnprocessedItems) {
        return false;
    }
    return Object.values(response.UnprocessedItems)
        .some(t => !!t && t.length > 0);
}
exports.hasUnprocessedItems = hasUnprocessedItems;
//# sourceMappingURL=batch-write-utils.js.map