import * as tslib_1 from "tslib";
import { map } from 'rxjs/operators';
import { metadataForClass } from '../../decorator/metadata/metadata-helper';
import { randomExponentialBackoffTimer } from '../../helper';
import { createToKeyFn, fromDb } from '../../mapper';
import { DynamoRx } from '../dynamo-rx';
import { getTableName } from '../get-table-name.function';
import { batchGetItemsFetchAll } from './batch-get-utils';
import { BATCH_GET_DEFAULT_TIME_SLOT, BATCH_GET_MAX_REQUEST_ITEM_COUNT } from './batch-get.const';
var BatchGetRequest = /** @class */ (function () {
    function BatchGetRequest() {
        var _this = this;
        this.tables = new Map();
        this.itemCounter = 0;
        this.mapResponse = function (response) {
            var Responses = {};
            if (response.Responses && Object.keys(response.Responses).length) {
                Responses = Object.entries(response.Responses).reduce(function (u, _a) {
                    var _b = tslib_1.__read(_a, 2), key = _b[0], val = _b[1];
                    u[key] = val.map(function (attributes) { return fromDb(attributes, _this.tables.get(key)); });
                    return u;
                }, {});
            }
            return {
                ConsumedCapacity: response.ConsumedCapacity,
                UnprocessedKeys: response.UnprocessedKeys,
                Responses: Responses,
            };
        };
        this.dynamoRx = new DynamoRx();
        this.params = {
            RequestItems: {},
        };
    }
    /**
     * @param {ModelConstructor<T>} modelClazz
     * @param {Partial<T>[]} keys a partial of T that contains Partition key and SortKey (if necessary). Throws if missing.
     * @param consistentRead
     * @returns {BatchGetSingleTableRequest}
     */
    BatchGetRequest.prototype.forModel = function (modelClazz, keys, consistentRead) {
        if (consistentRead === void 0) { consistentRead = false; }
        // check if modelClazz is really an @Model() decorated class
        var metadata = metadataForClass(modelClazz);
        if (!metadata.modelOptions) {
            throw new Error('given ModelConstructor has no @Model decorator');
        }
        // check if table was already used in this request
        var tableName = getTableName(metadata);
        if (this.tables.has(tableName)) {
            throw new Error('table name already exists, please provide all the keys for the same table at once');
        }
        this.tables.set(tableName, modelClazz);
        // check if keys to add do not exceed max count
        if (this.itemCounter + 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[tableName] = {
            Keys: keys.map(createToKeyFn(modelClazz)),
            ConsistentRead: consistentRead,
        };
        this.itemCounter += keys.length;
        return this;
    };
    BatchGetRequest.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);
    };
    BatchGetRequest.prototype.execFullResponse = function (backoffTimer, throttleTimeSlot) {
        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));
    };
    BatchGetRequest.prototype.exec = function (backoffTimer, throttleTimeSlot) {
        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.Responses; }));
    };
    BatchGetRequest.prototype.fetch = function (backoffTimer, throttleTimeSlot) {
        if (backoffTimer === void 0) { backoffTimer = randomExponentialBackoffTimer; }
        if (throttleTimeSlot === void 0) { throttleTimeSlot = BATCH_GET_DEFAULT_TIME_SLOT; }
        return batchGetItemsFetchAll(this.dynamoRx, tslib_1.__assign({}, this.params), backoffTimer(), throttleTimeSlot);
    };
    return BatchGetRequest;
}());
export { BatchGetRequest };
//# sourceMappingURL=batch-get.request.js.map