import * as tslib_1 from "tslib";
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';
var GetRequest = /** @class */ (function (_super) {
    tslib_1.__extends(GetRequest, _super);
    function GetRequest(dynamoRx, modelClazz, partitionKey, sortKey) {
        var _this = _super.call(this, dynamoRx, modelClazz) || this;
        _this.logger = createLogger('dynamo.request.GetRequest', modelClazz);
        _this.params.Key = createKeyAttributes(_this.metadata, partitionKey, sortKey);
        return _this;
    }
    GetRequest.prototype.consistentRead = function (consistentRead) {
        this.params.ConsistentRead = consistentRead;
        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(', ');
        Object.values(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;
}(StandardRequest));
export { GetRequest };
//# sourceMappingURL=get.request.js.map