"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const lodash_1 = require("lodash");
const operators_1 = require("rxjs/operators");
const mapper_1 = require("../../../mapper/mapper");
const attribute_names_function_1 = require("../../expression/functions/attribute-names.function");
const base_request_1 = require("../base.request");
class GetRequest extends base_request_1.BaseRequest {
    constructor(dynamoRx, modelClazz, tableName, partitionKey, sortKey) {
        super(dynamoRx, modelClazz, tableName);
        const hasSortKey = this.metaData.getSortKey() !== null;
        if (hasSortKey && (sortKey === null || sortKey === undefined)) {
            throw new Error(`please provide the sort key for attribute ${this.metaData.getSortKey()}`);
        }
        const keyAttributeMap = {};
        // partition key
        const partitionKeyValue = mapper_1.Mapper.toDbOne(partitionKey, this.metaData.forProperty(this.metaData.getPartitionKey()));
        if (partitionKeyValue === null) {
            throw new Error('please provide an acutal value for partition key, got null');
        }
        keyAttributeMap[this.metaData.getPartitionKey()] = partitionKeyValue;
        // sort key
        if (hasSortKey) {
            const sortKeyValue = mapper_1.Mapper.toDbOne(sortKey, this.metaData.forProperty(this.metaData.getSortKey()));
            if (sortKeyValue === null) {
                throw new Error('please provide an actual value for sort key, got null');
            }
            keyAttributeMap[this.metaData.getSortKey()] = sortKeyValue;
        }
        this.params.Key = keyAttributeMap;
    }
    consistentRead(consistendRead) {
        this.params.ConsistentRead = consistendRead;
        return this;
    }
    returnConsumedCapacity(level) {
        this.params.ReturnConsumedCapacity = level;
        return this;
    }
    projectionExpression(...attributesToGet) {
        const resolved = attributesToGet.map(attr => attribute_names_function_1.resolveAttributeNames(attr));
        this.params.ProjectionExpression = resolved.map(attr => attr.placeholder).join(', ');
        lodash_1.values(resolved).forEach(r => {
            this.params.ExpressionAttributeNames = Object.assign({}, this.params.ExpressionAttributeNames, r.attributeNames);
        });
        return this;
    }
    execFullResponse() {
        return this.dynamoRx.getItem(this.params).pipe(operators_1.map(getItemResponse => {
            const response = Object.assign({}, getItemResponse);
            if (getItemResponse.Item) {
                response.Item = mapper_1.Mapper.fromDb(getItemResponse.Item, this.modelClazz);
            }
            else {
                response.Item = null;
            }
            return response;
        }));
    }
    exec() {
        return this.dynamoRx.getItem(this.params).pipe(operators_1.map(response => {
            if (response.Item) {
                return mapper_1.Mapper.fromDb(response.Item, this.modelClazz);
            }
            else {
                return null;
            }
        }));
    }
}
exports.GetRequest = GetRequest;
//# sourceMappingURL=get.request.js.map