import { isObject } from 'lodash';
import { map } from 'rxjs/operators';
import { MetadataHelper } from '../../../decorator/metadata/metadata-helper';
import { Mapper } from '../../../mapper/mapper';
// TODO add support for indexes
var BatchGetSingleTableRequest = /** @class */ (function () {
    function BatchGetSingleTableRequest(dynamoRx, modelClazz, tableName, keys) {
        this.dynamoRx = dynamoRx;
        if (modelClazz === null || modelClazz === undefined) {
            throw new Error("please provide the model clazz for the request, won't work otherwise");
        }
        this.modelClazz = modelClazz;
        this.params = {
            RequestItems: {},
        };
        this.tableName = tableName;
        this.addKeyParams(keys);
    }
    Object.defineProperty(BatchGetSingleTableRequest.prototype, "metaData", {
        get: function () {
            if (!this._metadata) {
                this._metadata = MetadataHelper.get(this.modelClazz);
            }
            return this._metadata;
        },
        enumerable: true,
        configurable: true
    });
    BatchGetSingleTableRequest.prototype.execFullResponse = function () {
        var _this = this;
        return this.dynamoRx.batchGetItems(this.params).pipe(map(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 Mapper.fromDb(attributeMap, _this.modelClazz);
                });
                items = mapped;
            }
            else {
                items = [];
            }
            return {
                Items: items,
                UnprocessedKeys: response.UnprocessedKeys,
                ConsumedCapacity: response.ConsumedCapacity,
            };
        }));
    };
    BatchGetSingleTableRequest.prototype.exec = function () {
        var _this = this;
        return this.dynamoRx.batchGetItems(this.params).pipe(map(function (response) {
            if (response.Responses && Object.keys(response.Responses).length && response.Responses[_this.tableName]) {
                return response.Responses[_this.tableName].map(function (attributeMap) { return Mapper.fromDb(attributeMap, _this.modelClazz); });
            }
            else {
                return [];
            }
        }));
    };
    BatchGetSingleTableRequest.prototype.addKeyParams = function (keys) {
        var _this = this;
        var attributeMaps = [];
        keys.forEach(function (key) {
            var idOb = {};
            if (isObject(key)) {
                // TODO add some more checks
                // got a composite primary key
                // partition key
                var mappedPartitionKey = Mapper.toDbOne(key.partitionKey);
                if (mappedPartitionKey === null) {
                    throw Error('please provide an actual value for partition key');
                }
                idOb[_this.metaData.getPartitionKey()] = mappedPartitionKey;
                // sort key
                var mappedSortKey = Mapper.toDbOne(key.sortKey);
                if (mappedSortKey === null) {
                    throw Error('please provide an actual value for partition key');
                }
                idOb[_this.metaData.getSortKey()] = mappedSortKey;
            }
            else {
                // got a simple primary key
                var value = Mapper.toDbOne(key);
                if (value === null) {
                    throw Error('please provide an actual value for partition key');
                }
                idOb[_this.metaData.getPartitionKey()] = value;
            }
            attributeMaps.push(idOb);
        });
        this.params.RequestItems[this.tableName] = {
            Keys: attributeMaps,
        };
    };
    return BatchGetSingleTableRequest;
}());
export { BatchGetSingleTableRequest };
//# sourceMappingURL=batch-get-single-table.request.js.map