import { forEach } from 'lodash';
import { addUpdateExpression } from './param-util';
export 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 = {};
        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,
        };
        addUpdateExpression(expression, params);
    }
    else {
        throw new Error('at least one update operation must be defined');
    }
}
//# sourceMappingURL=prepare-and-add-update-expressions.function.js.map