import { values as objValues } from 'lodash';
import { map, tap } from 'rxjs/operators';
import { hasSortKey } from '../../../decorator/metadata';
import { createLogger } from '../../../logger/logger';
import { fromDb, toDbOne } from '../../../mapper';
import { resolveAttributeNames } from '../../expression/functions/attribute-names.function';
import { BaseRequest } from '../base.request';
export class GetRequest extends BaseRequest {
    constructor(dynamoRx, modelClazz, tableName, partitionKey, sortKey) {
        super(dynamoRx, modelClazz, tableName);
        this.logger = createLogger('dynamo.request.GetRequest', modelClazz);
        if (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 = 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(this.metadata)) {
            const sortKeyValue = 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 => 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