import * as tslib_1 from "tslib";
import { forEach } from 'lodash';
import { map, tap } from 'rxjs/operators';
import { hasSortKey } from '../../../decorator/metadata';
import { createLogger } from '../../../logger/logger';
import { toDbOne } from '../../../mapper';
import { addUpdateExpression } from '../../expression/param-util';
import { WriteRequest } from '../write.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;
        _this.logger = createLogger('dynamo.request.UpdateRequest', modelClazz);
        if (hasSortKey(_this.metadata) && (sortKey === null || sortKey === undefined)) {
            throw new Error("please provide the sort key for attribute " + _this.metadata.getSortKey());
        }
        var keyAttributeMap = {};
        // partition key
        var partitionKeyValue = 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(_this.metadata)) {
            var sortKeyValue = 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.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,
            };
            addUpdateExpression(expression, this.params);
            return this;
        }
        else {
            throw new Error('at least one update operation must be defined');
        }
    };
    UpdateRequest.prototype.execFullResponse = function () {
        var _this = this;
        this.logger.debug('request', this.params);
        return this.dynamoRx.updateItem(this.params).pipe(tap(function (response) { return _this.logger.debug('response', response); }));
    };
    UpdateRequest.prototype.exec = function () {
        return this.execFullResponse().pipe(map(function (response) {
            return;
        }));
    };
    return UpdateRequest;
}(WriteRequest));
export { UpdateRequest };
//# sourceMappingURL=update.request.js.map