"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const operators_1 = require("rxjs/operators");
const mapper_1 = require("../../../mapper/mapper");
const and_function_1 = require("../../expression/logical-operator/and.function");
const attribute_function_1 = require("../../expression/logical-operator/attribute.function");
const param_util_1 = require("../../expression/param-util");
const request_expression_builder_1 = require("../../expression/request-expression-builder");
const base_request_1 = require("../base.request");
class PutRequest extends base_request_1.BaseRequest {
    constructor(dynamoRx, modelClazz, tableName, item) {
        super(dynamoRx, modelClazz, tableName);
        this.params.Item = mapper_1.Mapper.toDb(item, this.modelClazz);
    }
    /**
     * 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>}
     */
    ifNotExists(predicate) {
        // FIXME should we check for sort key too?
        const conditionDefFns = [];
        if (predicate === undefined || (predicate !== undefined && predicate === true)) {
            conditionDefFns.push(attribute_function_1.attribute(this.metaData.getPartitionKey()).attributeNotExists());
            const sortKey = this.metaData.getSortKey();
            if (sortKey !== null) {
                conditionDefFns.push(attribute_function_1.attribute(sortKey).attributeNotExists());
            }
            this.where(...conditionDefFns);
        }
        return this;
    }
    returnConsumedCapacity(level) {
        this.params.ReturnConsumedCapacity = level;
        return this;
    }
    returnItemCollectionMetrics(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.
     */
    returnValues(returnValues) {
        this.params.ReturnValues = returnValues;
        return this;
    }
    whereAttribute(attributePath) {
        return request_expression_builder_1.RequestExpressionBuilder.addCondition('ConditionExpression', attributePath, this, this.metaData);
    }
    where(...conditionDefFns) {
        const condition = and_function_1.and(...conditionDefFns)(undefined, this.metaData);
        param_util_1.ParamUtil.addExpression('ConditionExpression', condition, this.params);
        return this;
    }
    execFullResponse() {
        return this.dynamoRx.putItem(this.params);
    }
    exec() {
        return this.dynamoRx.putItem(this.params).pipe(operators_1.map(response => {
            return;
        }));
    }
}
exports.PutRequest = PutRequest;
//# sourceMappingURL=put.request.js.map