import * as tslib_1 from "tslib";
import { map, tap } from 'rxjs/operators';
import { createLogger } from '../../../logger/logger';
import { toDb } from '../../../mapper';
import { attribute } from '../../expression/logical-operator';
import { WriteRequest } from '../write.request';
var PutRequest = /** @class */ (function (_super) {
    tslib_1.__extends(PutRequest, _super);
    function PutRequest(dynamoRx, modelClazz, tableName, item) {
        var _this = _super.call(this, dynamoRx, modelClazz, tableName) || this;
        _this.logger = createLogger('dynamo.request.PutRequest', modelClazz);
        _this.params.Item = toDb(item, _this.modelClazz);
        return _this;
    }
    /**
     * Adds a condition expression to the request, which makes sure the item will only be saved if the id does not exist
     * @returns {PutRequest<T>}
     */
    PutRequest.prototype.ifNotExists = function (predicate) {
        // FIXME should we check for sort key too?
        var conditionDefFns = [];
        if (predicate === undefined || (predicate !== undefined && predicate === true)) {
            conditionDefFns.push(attribute(this.metadata.getPartitionKey()).attributeNotExists());
            var sortKey = this.metadata.getSortKey();
            if (sortKey !== null) {
                conditionDefFns.push(attribute(sortKey).attributeNotExists());
            }
            this.onlyIf.apply(this, tslib_1.__spread(conditionDefFns));
        }
        return this;
    };
    PutRequest.prototype.execFullResponse = function () {
        var _this = this;
        this.logger.debug('request', this.params);
        return this.dynamoRx.putItem(this.params).pipe(tap(function (response) { return _this.logger.debug('response', response); }));
    };
    PutRequest.prototype.exec = function () {
        return this.execFullResponse().pipe(map(function (response) {
            return;
        }));
    };
    return PutRequest;
}(WriteRequest));
export { PutRequest };
//# sourceMappingURL=put.request.js.map