import { map } from 'rxjs/operators';
import { metadataForClass } from '../../decorator/metadata/metadata-helper';
import { createToKeyFn, fromDb } from '../../mapper';
import { DynamoRx } from '../dynamo-rx';
import { getTableName } from '../get-table-name.function';
var MAX_REQUEST_ITEM_COUNT = 10;
var TransactGetRequest = /** @class */ (function () {
    function TransactGetRequest() {
        var _this = this;
        this.tables = [];
        this.mapResponse = function (response) {
            var Items = response.Responses && Object.keys(response.Responses).length
                ? response.Responses.map(function (item, ix) { return fromDb(item.Item, _this.tables[ix]); })
                : [];
            return {
                ConsumedCapacity: response.ConsumedCapacity,
                Items: Items,
            };
        };
        this.dynamoRx = new DynamoRx();
        this.params = {
            TransactItems: [],
        };
    }
    TransactGetRequest.prototype.forModel = function (modelClazz, key) {
        // 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');
        }
        this.tables.push(modelClazz);
        // check if table was already used in this request
        var tableName = getTableName(metadata);
        // check if keys to add do not exceed max count
        if (this.params.TransactItems.length + 1 > MAX_REQUEST_ITEM_COUNT) {
            throw new Error("you can request at max " + MAX_REQUEST_ITEM_COUNT + " items per request");
        }
        this.params.TransactItems.push({
            Get: {
                TableName: tableName,
                Key: createToKeyFn(modelClazz)(key),
            },
        });
        return this;
    };
    TransactGetRequest.prototype.returnConsumedCapacity = function (level) {
        this.params.ReturnConsumedCapacity = level;
        return this;
    };
    TransactGetRequest.prototype.execNoMap = function () {
        return this.dynamoRx.transactGetItems(this.params);
    };
    TransactGetRequest.prototype.execFullResponse = function () {
        return this.dynamoRx.transactGetItems(this.params).pipe(map(this.mapResponse));
    };
    TransactGetRequest.prototype.exec = function () {
        return this.dynamoRx.transactGetItems(this.params).pipe(map(this.mapResponse), map(function (r) { return r.Items; }));
    };
    return TransactGetRequest;
}());
export { TransactGetRequest };
//# sourceMappingURL=transact-get.request.js.map