"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 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 DeleteRequest extends base_request_1.BaseRequest {
    constructor(dynamoRx, modelClazz, tableName, partitionKey, sortKey) {
        super(dynamoRx, modelClazz, tableName);
        const hasSortKey = this.metaData.getSortKey() !== null;
        if (hasSortKey && (sortKey === null || sortKey === undefined)) {
            throw new Error(`please provide the sort key for attribute ${this.metaData.getSortKey()}`);
        }
        const keyAttributeMap = {};
        // partition key
        const partitionKeyValue = mapper_1.Mapper.toDbOne(partitionKey, this.metaData.forProperty(this.metaData.getPartitionKey()));
        if (partitionKeyValue === null) {
            throw new Error('please provide an acutal value for partition key, got null');
        }
        keyAttributeMap[this.metaData.getPartitionKey()] = partitionKeyValue;
        // sort key
        if (hasSortKey) {
            const sortKeyValue = mapper_1.Mapper.toDbOne(sortKey, this.metaData.forProperty(this.metaData.getSortKey()));
            if (sortKeyValue === null) {
                throw new Error('please provide an actual value for sort key, got null');
            }
            keyAttributeMap[this.metaData.getSortKey()] = sortKeyValue;
        }
        this.params.Key = keyAttributeMap;
    }
    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;
    }
    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,
       * DeleteItem does not recognize any values other than NONE or ALL_OLD.
       */
    returnValues(returnValues) {
        this.params.ReturnValues = returnValues;
        return this;
    }
    execFullResponse() {
        return this.dynamoRx.deleteItem(this.params);
    }
    exec() {
        return this.dynamoRx.deleteItem(this.params).pipe(operators_1.map(response => {
            return;
        }));
    }
}
exports.DeleteRequest = DeleteRequest;
//# sourceMappingURL=delete.request.js.map