import * as tslib_1 from "tslib";
import { map } from 'rxjs/operators';
import { randomExponentialBackoffTimer } from '../../helper';
import { createToKeyFn, toDb } from '../../mapper';
import { DynamoRx } from '../dynamo-rx';
import { getTableName } from '../get-table-name.function';
import { batchWriteItemsWriteAll } from './batch-write-utils';
import { BATCH_WRITE_DEFAULT_TIME_SLOT, BATCH_WRITE_MAX_REQUEST_ITEM_COUNT } from './batch-write.const';
var BatchWriteRequest = /** @class */ (function () {
    function BatchWriteRequest() {
        this.itemCount = 0;
        this.createDeleteRequest = function (modelClazz) {
            var toKey = createToKeyFn(modelClazz);
            return function (item) { return ({ DeleteRequest: { Key: toKey(item) } }); };
        };
        this.createPutRequest = function (modelClazz) {
            return function (item) { return ({ PutRequest: { Item: toDb(item, modelClazz) } }); };
        };
        this.dynamoRx = new DynamoRx();
        this.params = {
            RequestItems: {},
        };
    }
    BatchWriteRequest.prototype.returnConsumedCapacity = function (value) {
        this.params.ReturnConsumedCapacity = value;
        return this;
    };
    BatchWriteRequest.prototype.returnItemCollectionMetrics = function (value) {
        this.params.ReturnItemCollectionMetrics = value;
        return this;
    };
    BatchWriteRequest.prototype.delete = function (modelClazz, items) {
        this.requestItems(modelClazz, items.map(this.createDeleteRequest(modelClazz)));
        return this;
    };
    BatchWriteRequest.prototype.put = function (modelClazz, items) {
        this.requestItems(modelClazz, items.map(this.createPutRequest(modelClazz)));
        return this;
    };
    /**
     *
     * @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
     */
    BatchWriteRequest.prototype.exec = function (backoffTimer, throttleTimeSlot) {
        if (backoffTimer === void 0) { backoffTimer = randomExponentialBackoffTimer; }
        if (throttleTimeSlot === void 0) { throttleTimeSlot = BATCH_WRITE_DEFAULT_TIME_SLOT; }
        return this.write(backoffTimer, throttleTimeSlot).pipe(map(function () {
            return;
        }));
    };
    BatchWriteRequest.prototype.execFullResponse = function (backoffTimer, throttleTimeSlot) {
        if (backoffTimer === void 0) { backoffTimer = randomExponentialBackoffTimer; }
        if (throttleTimeSlot === void 0) { throttleTimeSlot = BATCH_WRITE_DEFAULT_TIME_SLOT; }
        return this.write(backoffTimer, throttleTimeSlot);
    };
    BatchWriteRequest.prototype.requestItems = function (modelClazz, items) {
        var _a;
        if (this.itemCount + items.length > BATCH_WRITE_MAX_REQUEST_ITEM_COUNT) {
            throw new Error("batch write takes at max " + BATCH_WRITE_MAX_REQUEST_ITEM_COUNT + " items");
        }
        var tableName = getTableName(modelClazz);
        this.params.RequestItems[tableName] = this.params.RequestItems[tableName] || [];
        (_a = this.params.RequestItems[tableName]).push.apply(_a, tslib_1.__spread(items));
        this.itemCount += items.length;
    };
    BatchWriteRequest.prototype.write = function (backoffTimer, throttleTimeSlot) {
        return batchWriteItemsWriteAll(this.dynamoRx, tslib_1.__assign({}, this.params), backoffTimer(), throttleTimeSlot);
    };
    return BatchWriteRequest;
}());
export { BatchWriteRequest };
//# sourceMappingURL=batch-write.request.js.map