"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_get_utils_1 = require("../../batchget/batch-get-utils");
const batch_get_const_1 = require("../../batchget/batch-get.const");
const base_request_1 = require("../base.request");
class BatchGetSingleTableRequest extends base_request_1.BaseRequest {
    constructor(dynamoRx, modelClazz, keys) {
        super(dynamoRx, modelClazz);
        this.mapResponse = (response) => {
            let items = [];
            if (response.Responses && Object.keys(response.Responses).length && response.Responses[this.tableName]) {
                const mapped = response.Responses[this.tableName].map(attributeMap => mapper_1.fromDb(attributeMap, this.modelClazz));
                items = mapped;
            }
            return {
                Items: items,
                UnprocessedKeys: response.UnprocessedKeys,
                ConsumedCapacity: response.ConsumedCapacity,
            };
        };
        this.logger = logger_1.createLogger('dynamo.request.BatchGetSingleTableRequest', modelClazz);
        if (keys.length > batch_get_const_1.BATCH_GET_MAX_REQUEST_ITEM_COUNT) {
            throw new Error(`you can request at max ${batch_get_const_1.BATCH_GET_MAX_REQUEST_ITEM_COUNT} items per request`);
        }
        this.params.RequestItems = {
            [this.tableName]: {
                Keys: keys.map(mapper_1.createToKeyFn(modelClazz)),
            },
        };
    }
    consistentRead(value = true) {
        this.params.RequestItems[this.tableName].ConsistentRead = value;
        return this;
    }
    execNoMap(backoffTimer = helper_1.randomExponentialBackoffTimer, throttleTimeSlot = batch_get_const_1.BATCH_GET_DEFAULT_TIME_SLOT) {
        return this.fetch(backoffTimer, throttleTimeSlot);
    }
    execFullResponse(backoffTimer = helper_1.randomExponentialBackoffTimer, throttleTimeSlot = batch_get_const_1.BATCH_GET_DEFAULT_TIME_SLOT) {
        return this.fetch(backoffTimer, throttleTimeSlot).pipe(operators_1.map(this.mapResponse), operators_1.tap(response => this.logger.debug('mapped items', response.Items)));
    }
    exec(backoffTimer = helper_1.randomExponentialBackoffTimer, throttleTimeSlot = batch_get_const_1.BATCH_GET_DEFAULT_TIME_SLOT) {
        return this.fetch(backoffTimer, throttleTimeSlot).pipe(operators_1.map(this.mapResponse), operators_1.map(r => r.Items), operators_1.tap(items => this.logger.debug('mapped items', items)));
    }
    fetch(backoffTimer = helper_1.randomExponentialBackoffTimer, throttleTimeSlot = batch_get_const_1.BATCH_GET_DEFAULT_TIME_SLOT) {
        this.logger.debug('request', this.params);
        return batch_get_utils_1.batchGetItemsFetchAll(this.dynamoRx, Object.assign({}, this.params), backoffTimer(), throttleTimeSlot).pipe(operators_1.tap(response => this.logger.debug('response', response)));
    }
}
exports.BatchGetSingleTableRequest = BatchGetSingleTableRequest;
//# sourceMappingURL=batch-get-single-table.request.js.map