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';
export class PutRequest extends BaseRequest {
    constructor(dynamoRx, modelClazz, tableName, item) {
        super(dynamoRx, modelClazz, tableName);
        this.params.Item = 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(this.metaData.getPartitionKey()).attributeNotExists());
            const sortKey = this.metaData.getSortKey();
            if (sortKey !== null) {
                conditionDefFns.push(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 RequestExpressionBuilder.addCondition('ConditionExpression', attributePath, this, this.metaData);
    }
    where(...conditionDefFns) {
        const condition = and(...conditionDefFns)(undefined, this.metaData);
        ParamUtil.addExpression('ConditionExpression', condition, this.params);
        return this;
    }
    execFullResponse() {
        return this.dynamoRx.putItem(this.params);
    }
    exec() {
        return this.dynamoRx.putItem(this.params).pipe(map(response => {
            return;
        }));
    }
}
//# sourceMappingURL=put.request.js.map