"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const lodash_1 = require("lodash");
const operators_1 = require("rxjs/operators");
const metadata_helper_1 = require("../../../decorator/metadata/metadata-helper");
const mapper_1 = require("../../../mapper/mapper");
// TODO add support for indexes
class BatchGetSingleTableRequest {
    constructor(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);
    }
    get metaData() {
        if (!this._metadata) {
            this._metadata = metadata_helper_1.MetadataHelper.get(this.modelClazz);
        }
        return this._metadata;
    }
    execFullResponse() {
        return this.dynamoRx.batchGetItems(this.params).pipe(operators_1.map(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.Mapper.fromDb(attributeMap, this.modelClazz));
                items = mapped;
            }
            else {
                items = [];
            }
            return {
                Items: items,
                UnprocessedKeys: response.UnprocessedKeys,
                ConsumedCapacity: response.ConsumedCapacity,
            };
        }));
    }
    exec() {
        return this.dynamoRx.batchGetItems(this.params).pipe(operators_1.map(response => {
            if (response.Responses && Object.keys(response.Responses).length && response.Responses[this.tableName]) {
                return response.Responses[this.tableName].map(attributeMap => mapper_1.Mapper.fromDb(attributeMap, this.modelClazz));
            }
            else {
                return [];
            }
        }));
    }
    addKeyParams(keys) {
        const attributeMaps = [];
        keys.forEach(key => {
            const idOb = {};
            if (lodash_1.isObject(key)) {
                // TODO add some more checks
                // got a composite primary key
                // partition key
                const mappedPartitionKey = mapper_1.Mapper.toDbOne(key.partitionKey);
                if (mappedPartitionKey === null) {
                    throw Error('please provide an actual value for partition key');
                }
                idOb[this.metaData.getPartitionKey()] = mappedPartitionKey;
                // sort key
                const mappedSortKey = mapper_1.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
                const value = mapper_1.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,
        };
    }
}
exports.BatchGetSingleTableRequest = BatchGetSingleTableRequest;
//# sourceMappingURL=batch-get-single-table.request.js.map