import * as tslib_1 from "tslib";
import { map, tap } from 'rxjs/operators';
import { randomExponentialBackoffTimer } from '../../../helper';
import { createLogger } from '../../../logger/logger';
import { createToKeyFn, fromDb } from '../../../mapper';
import { batchGetItemsFetchAll } from '../../batchget/batch-get-utils';
import { BATCH_GET_DEFAULT_TIME_SLOT, BATCH_GET_MAX_REQUEST_ITEM_COUNT } from '../../batchget/batch-get.const';
import { BaseRequest } from '../base.request';
var BatchGetSingleTableRequest = /** @class */ (function (_super) {
    tslib_1.__extends(BatchGetSingleTableRequest, _super);
    function BatchGetSingleTableRequest(dynamoRx, modelClazz, keys) {
        var _a;
        var _this = _super.call(this, dynamoRx, modelClazz) || this;
        _this.mapResponse = function (response) {
            var items = [];
            if (response.Responses && Object.keys(response.Responses).length && response.Responses[_this.tableName]) {
                var mapped = response.Responses[_this.tableName].map(function (attributeMap) {
                    return fromDb(attributeMap, _this.modelClazz);
                });
                items = mapped;
            }
            return {
                Items: items,
                UnprocessedKeys: response.UnprocessedKeys,
                ConsumedCapacity: response.ConsumedCapacity,
            };
        };
        _this.logger = createLogger('dynamo.request.BatchGetSingleTableRequest', modelClazz);
        if (keys.length > BATCH_GET_MAX_REQUEST_ITEM_COUNT) {
            throw new Error("you can request at max " + BATCH_GET_MAX_REQUEST_ITEM_COUNT + " items per request");
        }
        _this.params.RequestItems = (_a = {},
            _a[_this.tableName] = {
                Keys: keys.map(createToKeyFn(modelClazz)),
            },
            _a);
        return _this;
    }
    BatchGetSingleTableRequest.prototype.consistentRead = function (value) {
        if (value === void 0) { value = true; }
        this.params.RequestItems[this.tableName].ConsistentRead = value;
        return this;
    };
    BatchGetSingleTableRequest.prototype.execNoMap = function (backoffTimer, throttleTimeSlot) {
        if (backoffTimer === void 0) { backoffTimer = randomExponentialBackoffTimer; }
        if (throttleTimeSlot === void 0) { throttleTimeSlot = BATCH_GET_DEFAULT_TIME_SLOT; }
        return this.fetch(backoffTimer, throttleTimeSlot);
    };
    BatchGetSingleTableRequest.prototype.execFullResponse = function (backoffTimer, throttleTimeSlot) {
        var _this = this;
        if (backoffTimer === void 0) { backoffTimer = randomExponentialBackoffTimer; }
        if (throttleTimeSlot === void 0) { throttleTimeSlot = BATCH_GET_DEFAULT_TIME_SLOT; }
        return this.fetch(backoffTimer, throttleTimeSlot).pipe(map(this.mapResponse), tap(function (response) { return _this.logger.debug('mapped items', response.Items); }));
    };
    BatchGetSingleTableRequest.prototype.exec = function (backoffTimer, throttleTimeSlot) {
        var _this = this;
        if (backoffTimer === void 0) { backoffTimer = randomExponentialBackoffTimer; }
        if (throttleTimeSlot === void 0) { throttleTimeSlot = BATCH_GET_DEFAULT_TIME_SLOT; }
        return this.fetch(backoffTimer, throttleTimeSlot).pipe(map(this.mapResponse), map(function (r) { return r.Items; }), tap(function (items) { return _this.logger.debug('mapped items', items); }));
    };
    BatchGetSingleTableRequest.prototype.fetch = function (backoffTimer, throttleTimeSlot) {
        var _this = this;
        if (backoffTimer === void 0) { backoffTimer = randomExponentialBackoffTimer; }
        if (throttleTimeSlot === void 0) { throttleTimeSlot = BATCH_GET_DEFAULT_TIME_SLOT; }
        this.logger.debug('request', this.params);
        return batchGetItemsFetchAll(this.dynamoRx, tslib_1.__assign({}, this.params), backoffTimer(), throttleTimeSlot).pipe(tap(function (response) { return _this.logger.debug('response', response); }));
    };
    return BatchGetSingleTableRequest;
}(BaseRequest));
export { BatchGetSingleTableRequest };
//# sourceMappingURL=batch-get-single-table.request.js.map