"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const operators_1 = require("rxjs/operators");
const logger_1 = require("../../../logger/logger");
const mapper_1 = require("../../../mapper");
const attribute_names_function_1 = require("../../expression/functions/attribute-names.function");
const standard_request_1 = require("../standard.request");
class GetRequest extends standard_request_1.StandardRequest {
    constructor(dynamoRx, modelClazz, partitionKey, sortKey) {
        super(dynamoRx, modelClazz);
        this.logger = logger_1.createLogger('dynamo.request.GetRequest', modelClazz);
        this.params.Key = mapper_1.createKeyAttributes(this.metadata, partitionKey, sortKey);
    }
    consistentRead(consistentRead) {
        this.params.ConsistentRead = consistentRead;
        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(', ');
        Object.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