import * as tslib_1 from "tslib";
import { of } from 'rxjs';
import { delay, map, mergeMap, tap } from 'rxjs/operators';
import { randomExponentialBackoffTimer } from '../../../helper';
import { Mapper } from '../../../mapper';
var MAX_BATCH_WRITE_ITEMS = 25;
var BatchWriteSingleTableRequest = /** @class */ (function () {
    function BatchWriteSingleTableRequest(dynamoRx, modelClazz, tableName) {
        this.dynamoRx = dynamoRx;
        if (modelClazz === null || modelClazz === undefined) {
            throw new Error("please provide the model clazz for the request, won't work otherwise");
        }
        this.modelClazz = modelClazz;
        this.tableName = tableName;
        this.itemsToProcess = [];
    }
    Object.defineProperty(BatchWriteSingleTableRequest.prototype, "toKey", {
        get: function () {
            if (!this._keyFn) {
                this._keyFn = Mapper.createToKeyFn(this.modelClazz);
            }
            return this._keyFn;
        },
        enumerable: true,
        configurable: true
    });
    BatchWriteSingleTableRequest.prototype.delete = function (items) {
        var _this = this;
        var _a;
        (_a = this.itemsToProcess).push.apply(_a, tslib_1.__spread(items.map(function (item) { return ({ DeleteRequest: { Key: _this.toKey(item) } }); })));
        return this;
    };
    BatchWriteSingleTableRequest.prototype.put = function (items) {
        var _this = this;
        var _a;
        (_a = this.itemsToProcess).push.apply(_a, tslib_1.__spread(items.map(function (item) { return ({ PutRequest: { Item: Mapper.toDb(item, _this.modelClazz) } }); })));
        return this;
    };
    BatchWriteSingleTableRequest.prototype.execNextBatch = function () {
        var _this = this;
        var _a;
        var batch = this.itemsToProcess.splice(0, MAX_BATCH_WRITE_ITEMS);
        var batchWriteItemInput = {
            RequestItems: (_a = {},
                _a[this.tableName] = batch,
                _a),
        };
        return this.dynamoRx.batchWriteItem(batchWriteItemInput).pipe(tap(function (batchWriteManyResponse) {
            var _a;
            if (batchWriteManyResponse.UnprocessedItems && batchWriteManyResponse.UnprocessedItems[_this.tableName]) {
                (_a = _this.itemsToProcess).unshift.apply(_a, tslib_1.__spread(batchWriteManyResponse.UnprocessedItems[_this.tableName]));
            }
        }), map(function (batchWriteManyResponse) { return ({
            remainingItems: _this.itemsToProcess.length,
            capacityExceeded: !!(batchWriteManyResponse.UnprocessedItems && batchWriteManyResponse.UnprocessedItems[_this.tableName]),
            consumedCapacity: batchWriteManyResponse.ConsumedCapacity,
        }); }));
    };
    /**
     *
     * @param backoffTimer generator for how much timeSlots should be waited before requesting next batch. only used when capacity was exceeded. default randomExponentialBackoffTimer
     * @param throttleTimeSlot defines how long one timeSlot is for throttling, default 1 second
     */
    BatchWriteSingleTableRequest.prototype.exec = function (backoffTimer, throttleTimeSlot) {
        var _this = this;
        if (backoffTimer === void 0) { backoffTimer = randomExponentialBackoffTimer; }
        if (throttleTimeSlot === void 0) { throttleTimeSlot = 1000; }
        var rBoT = backoffTimer();
        var backoffTime = 0;
        return this.execNextBatch().pipe(mergeMap(function (r) {
            if (!r.capacityExceeded) {
                rBoT = backoffTimer();
                backoffTime = 0;
            }
            else {
                backoffTime = rBoT.next().value;
            }
            return of(r).pipe(delay(backoffTime * throttleTimeSlot));
        }), mergeMap(function (r) {
            if (r.remainingItems > 0) {
                return _this.exec();
            }
            else {
                return of();
            }
        }));
    };
    return BatchWriteSingleTableRequest;
}());
export { BatchWriteSingleTableRequest };
//# sourceMappingURL=batch-write-single-table.request.js.map