"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const lodash_1 = require("lodash");
const operators_1 = require("rxjs/operators");
const metadata_1 = require("../../../decorator/metadata");
const logger_1 = require("../../../logger/logger");
const mapper_1 = require("../../../mapper");
const param_util_1 = require("../../expression/param-util");
const write_request_1 = require("../write.request");
class UpdateRequest extends write_request_1.WriteRequest {
    constructor(dynamoRx, modelClazz, tableName, partitionKey, sortKey) {
        super(dynamoRx, modelClazz, tableName);
        this.logger = logger_1.createLogger('dynamo.request.UpdateRequest', modelClazz);
        if (metadata_1.hasSortKey(this.metadata) && (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.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 (metadata_1.hasSortKey(this.metadata)) {
            const sortKeyValue = mapper_1.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;
    }
    operations(...updateDefFns) {
        if (updateDefFns && updateDefFns.length) {
            const sortedByActionKeyWord = updateDefFns
                .map(updateDefFn => {
                return updateDefFn(this.params.ExpressionAttributeValues, this.metadata);
            })
                .reduce((result, expr) => {
                if (!result[expr.type]) {
                    result[expr.type] = [];
                }
                result[expr.type].push(expr);
                return result;
            }, {});
            const actionStatements = [];
            let attributeValues = {};
            let attributeNames = {};
            lodash_1.forEach(sortedByActionKeyWord, (value, key) => {
                const statements = [];
                if (value && value.length) {
                    value.forEach(updateExpression => {
                        statements.push(updateExpression.statement);
                        attributeValues = Object.assign({}, attributeValues, updateExpression.attributeValues);
                        attributeNames = Object.assign({}, attributeNames, updateExpression.attributeNames);
                    });
                    actionStatements.push(`${key} ${statements.join(', ')}`);
                }
            });
            const expression = {
                statement: actionStatements.join(' '),
                attributeValues,
                attributeNames,
            };
            param_util_1.addUpdateExpression(expression, this.params);
            return this;
        }
        else {
            throw new Error('at least one update operation must be defined');
        }
    }
    execFullResponse() {
        this.logger.debug('request', this.params);
        return this.dynamoRx.updateItem(this.params).pipe(operators_1.tap(response => this.logger.debug('response', response)));
    }
    exec() {
        return this.execFullResponse().pipe(operators_1.map(response => {
            return;
        }));
    }
}
exports.UpdateRequest = UpdateRequest;
//# sourceMappingURL=update.request.js.map