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';
const MAX_REQUEST_ITEM_COUNT = 10;
export class TransactGetRequest {
    constructor() {
        this.tables = [];
        this.mapResponse = (response) => {
            const Items = response.Responses && Object.keys(response.Responses).length
                ? response.Responses.map((item, ix) => fromDb(item.Item, this.tables[ix]))
                : [];
            return {
                ConsumedCapacity: response.ConsumedCapacity,
                Items,
            };
        };
        this.dynamoRx = new DynamoRx();
        this.params = {
            TransactItems: [],
        };
    }
    forModel(modelClazz, key) {
        // check if modelClazz is really an @Model() decorated class
        const 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
        const 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;
    }
    returnConsumedCapacity(level) {
        this.params.ReturnConsumedCapacity = level;
        return this;
    }
    execNoMap() {
        return this.dynamoRx.transactGetItems(this.params);
    }
    execFullResponse() {
        return this.dynamoRx.transactGetItems(this.params).pipe(map(this.mapResponse));
    }
    exec() {
        return this.dynamoRx.transactGetItems(this.params).pipe(map(this.mapResponse), map(r => r.Items));
    }
}
//# sourceMappingURL=transact-get.request.js.map