import { mapKeys } from 'lodash';
import { uniqAttributeValueName } from '../functions/unique-attribute-value-name.function';
export function mergeConditions(operator, conditionDefinitionFns) {
    return (expressionAttributeValues, metadata) => {
        const mergedCondition = {
            statement: '',
            attributeNames: {},
            attributeValues: {},
        };
        const statements = [];
        conditionDefinitionFns.forEach(conditionDefinitionFn => {
            // we can reuse the same for multiple conditions
            const condition = conditionDefinitionFn(expressionAttributeValues, metadata);
            Object.assign(mergedCondition.attributeNames, condition.attributeNames);
            // we need to make sure the value variable name is unique
            const attributeValues = mapKeys(condition.attributeValues, (value, key) => {
                const unique = uniqAttributeValueName(key.replace(':', ''), Object.keys(mergedCondition.attributeValues));
                if (key !== unique) {
                    condition.statement = condition.statement.replace(key, unique);
                }
                return unique;
            });
            Object.assign(mergedCondition.attributeValues, attributeValues);
            statements.push(condition.statement);
        });
        mergedCondition.statement = `(${statements.join(' ' + operator + ' ')})`;
        return mergedCondition;
    };
}
//# sourceMappingURL=merge-conditions.function.js.map