"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const operators_1 = require("rxjs/operators");
const helper_1 = require("../../../helper");
const logger_1 = require("../../../logger/logger");
const mapper_1 = require("../../../mapper");
const batch_write_utils_1 = require("../../batchwrite/batch-write-utils");
const batch_write_const_1 = require("../../batchwrite/batch-write.const");
const base_request_1 = require("../base.request");
class BatchWriteSingleTableRequest extends base_request_1.BaseRequest {
    constructor(dynamoRx, modelClazz) {
        super(dynamoRx, modelClazz);
        this.toKey = mapper_1.createToKeyFn(this.modelClazz);
        this.createDeleteRequest = (item) => ({ DeleteRequest: { Key: this.toKey(item) } });
        this.createPutRequest = (item) => ({ PutRequest: { Item: mapper_1.toDb(item, this.modelClazz) } });
        this.logger = logger_1.createLogger('dynamo.request.BatchWriteSingleTableRequest', modelClazz);
        this.params.RequestItems = {
            [this.tableName]: [],
        };
    }
    returnItemCollectionMetrics(value) {
        this.params.ReturnItemCollectionMetrics = value;
    }
    delete(items) {
        if (this.params.RequestItems[this.tableName].length + items.length > batch_write_const_1.BATCH_WRITE_MAX_REQUEST_ITEM_COUNT) {
            throw new Error(`batch write takes at max ${batch_write_const_1.BATCH_WRITE_MAX_REQUEST_ITEM_COUNT} items`);
        }
        this.params.RequestItems[this.tableName].push(...items.map(this.createDeleteRequest));
        return this;
    }
    put(items) {
        if (this.params.RequestItems[this.tableName].length + items.length > batch_write_const_1.BATCH_WRITE_MAX_REQUEST_ITEM_COUNT) {
            throw new Error(`batch write takes at max ${batch_write_const_1.BATCH_WRITE_MAX_REQUEST_ITEM_COUNT} items`);
        }
        this.params.RequestItems[this.tableName].push(...items.map(this.createPutRequest));
        return this;
    }
    /**
     * write all given items
     * @param backoffTimer when unprocessed items are returned the next value of backoffTimer is used to determine how many time slots to wait before doing the next request
     * @param throttleTimeSlot the duration of a time slot in ms
     */
    exec(backoffTimer = helper_1.randomExponentialBackoffTimer, throttleTimeSlot = batch_write_const_1.BATCH_WRITE_DEFAULT_TIME_SLOT) {
        this.logger.debug('starting batchWriteItem');
        return this.write(backoffTimer, throttleTimeSlot).pipe(operators_1.map(() => {
            return;
        }));
    }
    execFullResponse(backoffTimer = helper_1.randomExponentialBackoffTimer, throttleTimeSlot = batch_write_const_1.BATCH_WRITE_DEFAULT_TIME_SLOT) {
        return this.write(backoffTimer, throttleTimeSlot);
    }
    write(backoffTimer, throttleTimeSlot) {
        return batch_write_utils_1.batchWriteItemsWriteAll(this.dynamoRx, Object.assign({}, this.params), backoffTimer(), throttleTimeSlot);
    }
}
exports.BatchWriteSingleTableRequest = BatchWriteSingleTableRequest;
//# sourceMappingURL=batch-write-single-table.request.js.map