import { values as objValues } from 'lodash';
import { map, tap } from 'rxjs/operators';
import { createLogger } from '../../../logger/logger';
import { createKeyAttributes, fromDb } from '../../../mapper';
import { resolveAttributeNames } from '../../expression/functions/attribute-names.function';
import { StandardRequest } from '../standard.request';
export class GetRequest extends StandardRequest {
    constructor(dynamoRx, modelClazz, partitionKey, sortKey) {
        super(dynamoRx, modelClazz);
        this.logger = createLogger('dynamo.request.GetRequest', modelClazz);
        this.params.Key = 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 => resolveAttributeNames(a));
        this.params.ProjectionExpression = resolved.map(attr => attr.placeholder).join(', ');
        objValues(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(tap(response => this.logger.debug('response', response)), map(getItemResponse => {
            const response = Object.assign({}, getItemResponse);
            if (getItemResponse.Item) {
                response.Item = fromDb(getItemResponse.Item, this.modelClazz);
            }
            else {
                response.Item = null;
            }
            return response;
        }), tap(response => this.logger.debug('mapped item', response.Item)));
    }
    exec() {
        this.logger.debug('request', this.params);
        return this.dynamoRx.getItem(this.params).pipe(tap(response => this.logger.debug('response', response)), map(response => {
            if (response.Item) {
                return fromDb(response.Item, this.modelClazz);
            }
            else {
                return null;
            }
        }), tap(item => this.logger.debug('mapped item', item)));
    }
}
//# sourceMappingURL=get.request.js.map