"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const operators_1 = require("rxjs/operators");
const helper_1 = require("../../helper");
const mapper_1 = require("../../mapper");
const dynamo_rx_1 = require("../dynamo-rx");
const get_table_name_function_1 = require("../get-table-name.function");
const batch_write_utils_1 = require("./batch-write-utils");
const batch_write_const_1 = require("./batch-write.const");
class BatchWriteRequest {
    constructor() {
        this.itemCount = 0;
        this.createDeleteRequest = (modelClazz) => {
            const toKey = mapper_1.createToKeyFn(modelClazz);
            return (item) => ({ DeleteRequest: { Key: toKey(item) } });
        };
        this.createPutRequest = (modelClazz) => {
            return (item) => ({ PutRequest: { Item: mapper_1.toDb(item, modelClazz) } });
        };
        this.dynamoRx = new dynamo_rx_1.DynamoRx();
        this.params = {
            RequestItems: {},
        };
    }
    returnConsumedCapacity(value) {
        this.params.ReturnConsumedCapacity = value;
        return this;
    }
    returnItemCollectionMetrics(value) {
        this.params.ReturnItemCollectionMetrics = value;
        return this;
    }
    delete(modelClazz, items) {
        this.requestItems(modelClazz, items.map(this.createDeleteRequest(modelClazz)));
        return this;
    }
    put(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
     */
    exec(backoffTimer = helper_1.randomExponentialBackoffTimer, throttleTimeSlot = batch_write_const_1.BATCH_WRITE_DEFAULT_TIME_SLOT) {
        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);
    }
    requestItems(modelClazz, items) {
        if (this.itemCount + 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`);
        }
        const tableName = get_table_name_function_1.getTableName(modelClazz);
        this.params.RequestItems[tableName] = this.params.RequestItems[tableName] || [];
        this.params.RequestItems[tableName].push(...items);
        this.itemCount += items.length;
    }
    write(backoffTimer, throttleTimeSlot) {
        return batch_write_utils_1.batchWriteItemsWriteAll(this.dynamoRx, Object.assign({}, this.params), backoffTimer(), throttleTimeSlot);
    }
}
exports.BatchWriteRequest = BatchWriteRequest;
//# sourceMappingURL=batch-write.request.js.map