import * as tslib_1 from "tslib";
import { values as objValues } from 'lodash';
import { map } from 'rxjs/operators';
import { Mapper } from '../../../mapper/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;
        var hasSortKey = _this.metaData.getSortKey() !== null;
        if (hasSortKey && (sortKey === null || sortKey === undefined)) {
            throw new Error("please provide the sort key for attribute " + _this.metaData.getSortKey());
        }
        var keyAttributeMap = {};
        // partition key
        var partitionKeyValue = Mapper.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) {
            var sortKeyValue = Mapper.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];
        }
        var resolved = attributesToGet.map(function (attr) { return resolveAttributeNames(attr); });
        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;
        return this.dynamoRx.getItem(this.params).pipe(map(function (getItemResponse) {
            var response = tslib_1.__assign({}, getItemResponse);
            if (getItemResponse.Item) {
                response.Item = Mapper.fromDb(getItemResponse.Item, _this.modelClazz);
            }
            else {
                response.Item = null;
            }
            return response;
        }));
    };
    GetRequest.prototype.exec = function () {
        var _this = this;
        return this.dynamoRx.getItem(this.params).pipe(map(function (response) {
            if (response.Item) {
                return Mapper.fromDb(response.Item, _this.modelClazz);
            }
            else {
                return null;
            }
        }));
    };
    return GetRequest;
}(BaseRequest));
export { GetRequest };
//# sourceMappingURL=get.request.js.map