"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const lodash_1 = require("lodash");
const operators_1 = require("rxjs/operators");
const metadata_1 = require("../../../decorator/metadata");
const logger_1 = require("../../../logger/logger");
const mapper_1 = require("../../../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);
        this.logger = logger_1.createLogger('dynamo.request.GetRequest', modelClazz);
        if (metadata_1.hasSortKey(this.metadata) && (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.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 (metadata_1.hasSortKey(this.metadata)) {
            const sortKeyValue = mapper_1.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) {
        // tslint:disable-next-line:no-unnecessary-callback-wrapper
        const resolved = attributesToGet.map(a => attribute_names_function_1.resolveAttributeNames(a));
        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() {
        this.logger.debug('request', this.params);
        return this.dynamoRx.getItem(this.params).pipe(operators_1.tap(response => this.logger.debug('response', response)), operators_1.map(getItemResponse => {
            const response = Object.assign({}, getItemResponse);
            if (getItemResponse.Item) {
                response.Item = mapper_1.fromDb(getItemResponse.Item, this.modelClazz);
            }
            else {
                response.Item = null;
            }
            return response;
        }), operators_1.tap(response => this.logger.debug('mapped item', response.Item)));
    }
    exec() {
        this.logger.debug('request', this.params);
        return this.dynamoRx.getItem(this.params).pipe(operators_1.tap(response => this.logger.debug('response', response)), operators_1.map(response => {
            if (response.Item) {
                return mapper_1.fromDb(response.Item, this.modelClazz);
            }
            else {
                return null;
            }
        }), operators_1.tap(item => this.logger.debug('mapped item', item)));
    }
}
exports.GetRequest = GetRequest;
//# sourceMappingURL=get.request.js.map