"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const operators_1 = require("rxjs/operators");
const logger_1 = require("../../../logger/logger");
const mapper_1 = require("../../../mapper");
const logical_operator_1 = require("../../expression/logical-operator");
const write_request_1 = require("../write.request");
class PutRequest extends write_request_1.WriteRequest {
    constructor(dynamoRx, modelClazz, tableName, item) {
        super(dynamoRx, modelClazz, tableName);
        this.logger = logger_1.createLogger('dynamo.request.PutRequest', modelClazz);
        this.params.Item = mapper_1.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(logical_operator_1.attribute(this.metadata.getPartitionKey()).attributeNotExists());
            const sortKey = this.metadata.getSortKey();
            if (sortKey !== null) {
                conditionDefFns.push(logical_operator_1.attribute(sortKey).attributeNotExists());
            }
            this.onlyIf(...conditionDefFns);
        }
        return this;
    }
    execFullResponse() {
        this.logger.debug('request', this.params);
        return this.dynamoRx.putItem(this.params).pipe(operators_1.tap(response => this.logger.debug('response', response)));
    }
    exec() {
        return this.execFullResponse().pipe(operators_1.map(response => {
            return;
        }));
    }
}
exports.PutRequest = PutRequest;
//# sourceMappingURL=put.request.js.map