import * as tslib_1 from "tslib";
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';
var GetRequest = /** @class */ (function (_super) {
    tslib_1.__extends(GetRequest, _super);
    function GetRequest(dynamoRx, modelClazz, tableName, partitionKey, sortKey) {
        var _this = _super.call(this, dynamoRx, modelClazz, tableName) || this;
        _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());
        }
        var keyAttributeMap = {};
        // partition key
        var 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)) {
            var 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;
        return _this;
    }
    GetRequest.prototype.consistentRead = function (consistendRead) {
        this.params.ConsistentRead = consistendRead;
        return this;
    };
    GetRequest.prototype.returnConsumedCapacity = function (level) {
        this.params.ReturnConsumedCapacity = level;
        return this;
    };
    GetRequest.prototype.projectionExpression = function () {
        var _this = this;
        var attributesToGet = [];
        for (var _i = 0; _i < arguments.length; _i++) {
            attributesToGet[_i] = arguments[_i];
        }
        // tslint:disable-next-line:no-unnecessary-callback-wrapper
        var resolved = attributesToGet.map(function (a) { return resolveAttributeNames(a); });
        this.params.ProjectionExpression = resolved.map(function (attr) { return attr.placeholder; }).join(', ');
        objValues(resolved).forEach(function (r) {
            _this.params.ExpressionAttributeNames = tslib_1.__assign({}, _this.params.ExpressionAttributeNames, r.attributeNames);
        });
        return this;
    };
    GetRequest.prototype.execFullResponse = function () {
        var _this = this;
        this.logger.debug('request', this.params);
        return this.dynamoRx.getItem(this.params).pipe(tap(function (response) { return _this.logger.debug('response', response); }), map(function (getItemResponse) {
            var response = tslib_1.__assign({}, getItemResponse);
            if (getItemResponse.Item) {
                response.Item = fromDb(getItemResponse.Item, _this.modelClazz);
            }
            else {
                response.Item = null;
            }
            return response;
        }), tap(function (response) { return _this.logger.debug('mapped item', response.Item); }));
    };
    GetRequest.prototype.exec = function () {
        var _this = this;
        this.logger.debug('request', this.params);
        return this.dynamoRx.getItem(this.params).pipe(tap(function (response) { return _this.logger.debug('response', response); }), map(function (response) {
            if (response.Item) {
                return fromDb(response.Item, _this.modelClazz);
            }
            else {
                return null;
            }
        }), tap(function (item) { return _this.logger.debug('mapped item', item); }));
    };
    return GetRequest;
}(BaseRequest));
export { GetRequest };
//# sourceMappingURL=get.request.js.map