import * as tslib_1 from "tslib";
import { map } from 'rxjs/operators';
import { metadataForClass } from '../../../decorator/metadata';
import { randomExponentialBackoffTimer } from '../../../helper';
import { createLogger } from '../../../logger/logger';
import { createToKeyFn, toDb } from '../../../mapper';
import { getTableName } from '../../get-table-name.function';
import { batchWriteItemsWriteAll } from './batch-write-utils';
var MAX_BATCH_WRITE_ITEMS = 25;
var DEFAULT_TIME_SLOT = 1000;
var BatchWriteSingleTableRequest = /** @class */ (function () {
    function BatchWriteSingleTableRequest(dynamoRx, modelClazz) {
        var _a;
        var _this = this;
        this.createDeleteRequest = function (item) { return ({ DeleteRequest: { Key: _this.toKey(item) } }); };
        this.createPutRequest = function (item) { return ({ PutRequest: { Item: toDb(item, _this.modelClazz) } }); };
        this.logger = createLogger('dynamo.request.BatchWriteSingleTableRequest', modelClazz);
        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.metadata = metadataForClass(this.modelClazz);
        if (!this.metadata.modelOptions) {
            throw new Error('given ModelConstructor has no @Model decorator');
        }
        this.tableName = getTableName(this.metadata);
        this.params = {
            RequestItems: (_a = {},
                _a[this.tableName] = [],
                _a),
        };
    }
    Object.defineProperty(BatchWriteSingleTableRequest.prototype, "toKey", {
        get: function () {
            if (!this.keyFn) {
                this.keyFn = createToKeyFn(this.modelClazz);
            }
            return this.keyFn;
        },
        enumerable: true,
        configurable: true
    });
    BatchWriteSingleTableRequest.prototype.returnConsumedCapacity = function (value) {
        this.params.ReturnConsumedCapacity = value;
    };
    BatchWriteSingleTableRequest.prototype.returnItemCollectionMetrics = function (value) {
        this.params.ReturnItemCollectionMetrics = value;
    };
    BatchWriteSingleTableRequest.prototype.delete = function (items) {
        var _a;
        if (this.params.RequestItems[this.tableName].length + items.length > MAX_BATCH_WRITE_ITEMS) {
            throw new Error("batch write takes at max " + MAX_BATCH_WRITE_ITEMS + " items");
        }
        (_a = this.params.RequestItems[this.tableName]).push.apply(_a, tslib_1.__spread(items.map(this.createDeleteRequest)));
        return this;
    };
    BatchWriteSingleTableRequest.prototype.put = function (items) {
        var _a;
        if (this.params.RequestItems[this.tableName].length + items.length > MAX_BATCH_WRITE_ITEMS) {
            throw new Error("batch write takes at max " + MAX_BATCH_WRITE_ITEMS + " items");
        }
        (_a = this.params.RequestItems[this.tableName]).push.apply(_a, tslib_1.__spread(items.map(this.createPutRequest)));
        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
     */
    BatchWriteSingleTableRequest.prototype.exec = function (backoffTimer, throttleTimeSlot) {
        if (backoffTimer === void 0) { backoffTimer = randomExponentialBackoffTimer; }
        if (throttleTimeSlot === void 0) { throttleTimeSlot = DEFAULT_TIME_SLOT; }
        this.logger.debug('starting batchWriteItem');
        return this.write(backoffTimer, throttleTimeSlot).pipe(map(function () {
            return;
        }));
    };
    BatchWriteSingleTableRequest.prototype.execFullResponse = function (backoffTimer, throttleTimeSlot) {
        if (backoffTimer === void 0) { backoffTimer = randomExponentialBackoffTimer; }
        if (throttleTimeSlot === void 0) { throttleTimeSlot = DEFAULT_TIME_SLOT; }
        return this.write(backoffTimer, throttleTimeSlot);
    };
    BatchWriteSingleTableRequest.prototype.write = function (backoffTimer, throttleTimeSlot) {
        return batchWriteItemsWriteAll(this.dynamoRx, tslib_1.__assign({}, this.params), backoffTimer(), throttleTimeSlot);
    };
    return BatchWriteSingleTableRequest;
}());
export { BatchWriteSingleTableRequest };
//# sourceMappingURL=batch-write-single-table.request.js.map