"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const lodash_1 = require("lodash");
const param_util_1 = require("./param-util");
function prepareAndAddUpdateExpressions(metadata, params, updateDefFns) {
    if (updateDefFns && updateDefFns.length) {
        const sortedByActionKeyWord = updateDefFns
            .map(updateDefFn => {
            return updateDefFn(params.ExpressionAttributeNames, 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, params);
    }
    else {
        throw new Error('at least one update operation must be defined');
    }
}
exports.prepareAndAddUpdateExpressions = prepareAndAddUpdateExpressions;
//# sourceMappingURL=prepare-and-add-update-expressions.function.js.map