import * as tslib_1 from "tslib";
import { map } from 'rxjs/operators';
import { Mapper } from '../../../mapper/mapper';
import { and } from '../../expression/logical-operator/and.function';
import { attribute } from '../../expression/logical-operator/attribute.function';
import { ParamUtil } from '../../expression/param-util';
import { RequestExpressionBuilder } from '../../expression/request-expression-builder';
import { BaseRequest } from '../base.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.params.Item = Mapper.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.where.apply(this, tslib_1.__spread(conditionDefFns));
        }
        return this;
    };
    PutRequest.prototype.returnConsumedCapacity = function (level) {
        this.params.ReturnConsumedCapacity = level;
        return this;
    };
    PutRequest.prototype.returnItemCollectionMetrics = function (returnItemCollectionMetrics) {
        this.params.ReturnItemCollectionMetrics = returnItemCollectionMetrics;
        return this;
    };
    /*
     * The ReturnValues parameter is used by several DynamoDB operations,
     * however, PutItem does not recognize any values other than NONE or ALL_OLD.
     */
    PutRequest.prototype.returnValues = function (returnValues) {
        this.params.ReturnValues = returnValues;
        return this;
    };
    PutRequest.prototype.whereAttribute = function (attributePath) {
        return RequestExpressionBuilder.addCondition('ConditionExpression', attributePath, this, this.metaData);
    };
    PutRequest.prototype.where = function () {
        var conditionDefFns = [];
        for (var _i = 0; _i < arguments.length; _i++) {
            conditionDefFns[_i] = arguments[_i];
        }
        var condition = and.apply(void 0, tslib_1.__spread(conditionDefFns))(undefined, this.metaData);
        ParamUtil.addExpression('ConditionExpression', condition, this.params);
        return this;
    };
    PutRequest.prototype.execFullResponse = function () {
        return this.dynamoRx.putItem(this.params);
    };
    PutRequest.prototype.exec = function () {
        return this.dynamoRx.putItem(this.params).pipe(map(function (response) {
            return;
        }));
    };
    return PutRequest;
}(BaseRequest));
export { PutRequest };
//# sourceMappingURL=put.request.js.map