"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const lodash_1 = require("lodash");
const unique_attribute_value_name_function_1 = require("../functions/unique-attribute-value-name.function");
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 = lodash_1.mapKeys(condition.attributeValues, (value, key) => {
                const unique = unique_attribute_value_name_function_1.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;
    };
}
exports.mergeConditions = mergeConditions;
//# sourceMappingURL=merge-conditions.function.js.map