import * as tslib_1 from "tslib";
import { forEach } from 'lodash';
import { map } from 'rxjs/operators';
import { Mapper } from '../../../mapper/mapper';
import { and } from '../../expression/logical-operator/and.function';
import { ParamUtil } from '../../expression/param-util';
import { RequestExpressionBuilder } from '../../expression/request-expression-builder';
import { BaseRequest } from '../base.request';
var UpdateRequest = /** @class */ (function (_super) {
    tslib_1.__extends(UpdateRequest, _super);
    function UpdateRequest(dynamoRx, modelClazz, tableName, partitionKey, sortKey) {
        var _this = _super.call(this, dynamoRx, modelClazz, tableName) || this;
        var hasSortKey = _this.metaData.getSortKey() !== null;
        if (hasSortKey && (sortKey === null || sortKey === undefined)) {
            throw new Error("please provide the sort key for attribute " + _this.metaData.getSortKey());
        }
        var keyAttributeMap = {};
        // partition key
        var partitionKeyValue = 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) {
            var sortKeyValue = 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;
        return _this;
    }
    UpdateRequest.prototype.whereAttribute = function (attributePath) {
        return RequestExpressionBuilder.addCondition('ConditionExpression', attributePath, this, this.metaData);
    };
    UpdateRequest.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;
    };
    UpdateRequest.prototype.operations = function () {
        var _this = this;
        var updateDefFns = [];
        for (var _i = 0; _i < arguments.length; _i++) {
            updateDefFns[_i] = arguments[_i];
        }
        if (updateDefFns && updateDefFns.length) {
            var sortedByActionKeyWord = updateDefFns
                .map(function (updateDefFn) {
                return updateDefFn(_this.params.ExpressionAttributeValues, _this.metaData);
            })
                .reduce(function (result, expr) {
                if (!result[expr.type]) {
                    result[expr.type] = [];
                }
                result[expr.type].push(expr);
                return result;
            }, {});
            var actionStatements_1 = [];
            var attributeValues_1 = {};
            var attributeNames_1 = {};
            forEach(sortedByActionKeyWord, function (value, key) {
                var statements = [];
                if (value && value.length) {
                    value.forEach(function (updateExpression) {
                        statements.push(updateExpression.statement);
                        attributeValues_1 = tslib_1.__assign({}, attributeValues_1, updateExpression.attributeValues);
                        attributeNames_1 = tslib_1.__assign({}, attributeNames_1, updateExpression.attributeNames);
                    });
                    actionStatements_1.push(key + " " + statements.join(', '));
                }
            });
            var expression = {
                statement: actionStatements_1.join(' '),
                attributeValues: attributeValues_1,
                attributeNames: attributeNames_1,
            };
            ParamUtil.addUpdateExpression(expression, this.params);
            return this;
        }
        else {
            throw new Error('at least one update operation must be defined');
        }
    };
    UpdateRequest.prototype.returnConsumedCapacity = function (level) {
        this.params.ReturnConsumedCapacity = level;
        return this;
    };
    UpdateRequest.prototype.returnItemCollectionMetrics = function (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.
     */
    UpdateRequest.prototype.returnValues = function (returnValues) {
        this.params.ReturnValues = returnValues;
        return this;
    };
    UpdateRequest.prototype.execFullResponse = function () {
        return this.dynamoRx.updateItem(this.params);
    };
    UpdateRequest.prototype.exec = function () {
        return this.dynamoRx.updateItem(this.params).pipe(map(function (response) {
            return;
        }));
    };
    return UpdateRequest;
}(BaseRequest));
export { UpdateRequest };
//# sourceMappingURL=update.request.js.map