"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const operators_1 = require("rxjs/operators");
const metadata_helper_1 = require("../../decorator/metadata/metadata-helper");
const mapper_1 = require("../../mapper");
const dynamo_rx_1 = require("../dynamo-rx");
const get_table_name_function_1 = require("../get-table-name.function");
const MAX_REQUEST_ITEM_COUNT = 10;
class TransactGetRequest {
    constructor() {
        this.tables = [];
        this.mapResponse = (response) => {
            const Items = response.Responses && Object.keys(response.Responses).length
                ? response.Responses.map((item, ix) => mapper_1.fromDb(item.Item, this.tables[ix]))
                : [];
            return {
                ConsumedCapacity: response.ConsumedCapacity,
                Items,
            };
        };
        this.dynamoRx = new dynamo_rx_1.DynamoRx();
        this.params = {
            TransactItems: [],
        };
    }
    forModel(modelClazz, key) {
        // check if modelClazz is really an @Model() decorated class
        const metadata = metadata_helper_1.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
        const tableName = get_table_name_function_1.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: mapper_1.createToKeyFn(modelClazz)(key),
            },
        });
        return this;
    }
    returnConsumedCapacity(level) {
        this.params.ReturnConsumedCapacity = level;
        return this;
    }
    execNoMap() {
        return this.dynamoRx.transactGetItems(this.params);
    }
    execFullResponse() {
        return this.dynamoRx.transactGetItems(this.params).pipe(operators_1.map(this.mapResponse));
    }
    exec() {
        return this.dynamoRx.transactGetItems(this.params).pipe(operators_1.map(this.mapResponse), operators_1.map(r => r.Items));
    }
}
exports.TransactGetRequest = TransactGetRequest;
//# sourceMappingURL=transact-get.request.js.map