import jsonLogic from 'json-logic-js';
/**
 * Constructs an array of object from the object map.
 *
 * @param {Object} obj ex: {"a": "customerID", "b": "firstName"}
 * @returns an array of object ex. [{"variable": "a", "value": "serviceLocationID"}, {"variable": "b", "value" "firstName"}]
 */
export const getPropertyMappingList = (obj) => {
    const mappings = [];
    if (obj) {
        const keys = Object.keys(obj);
        keys.forEach((key) => {
            const mappingObj = { variable: key, value: obj[key] };
            mappings.push(mappingObj);
        });
    }
    return mappings;
};
/**
 * Constructs a map object where variables are keys and values are the values.
 *
 * @param {Array<Object>} properties ex. [{"variable": "a", "value": "serviceLocationID"}, {"variable": "b", "value" "firstName"}]
 * @returns mapped object ex: {"a": "customerID", "b": "firstName"}
 */
export const getPropertyMappingObj = (properties) => {
    const mapping = {};
    if (properties === null || properties === void 0 ? void 0 : properties.length) {
        properties.forEach((property) => {
            mapping[property.variable] = property.value;
        });
    }
    return mapping;
};
/**
 * Maps the keys of mapping to the values of data.
 *
 * @param {Object} mapping ex: {"a": "customerID", "b": "firstName"}
 * @param {Object} data ex: {"customerID": 1001, "firstName": "John"}
 * @returns flat object ex: {"a": 1001, "b": "John"}
 */
export const mapRuleData = (mapping, data) => {
    const mappedData = {};
    if (mapping && mapping.length) {
        const keys = Object.keys(mapping);
        keys.forEach((key) => {
            const dataKey = mapping[key];
            if (dataKey) {
                const dataValue = data[dataKey];
                mappedData[key] = dataValue;
            }
        });
    }
    return mappedData;
};
/**
 * Maps the mapping values in the mapping object to the variables in the rule.
 *
 * @param {Object} mapping ex: {"a": "customerID", "b": "firstName"}
 * @param {Object} rule ex: {"or": [{"==": [{"var": "a"}, 400]}, {"==": [{"var": "b"}, John]}]}
 * @returns jsonLogic object with the correct variable names ex: [{"==": [{"var": "customerID"}, 400]}, {"==": [{"var": "firstName"}, John]}]
 */
export const mapRule = (mapping = {}, rule = '') => {
    const keys = Object.keys(mapping);
    let updatedJsonLogic = JSON.stringify(rule);
    keys.forEach((key) => {
        const substr = JSON.stringify({ var: key });
        const newSubstr = JSON.stringify({ var: mapping[key] });
        updatedJsonLogic = updatedJsonLogic.replaceAll(substr, newSubstr);
    });
    try {
        return JSON.parse(updatedJsonLogic);
    }
    catch (err) {
        console.err('Error with parsing JSON Logic String');
        return {};
    }
};
/**
 * Evaluates the rule with provided data.
 *
 * @param {Object} rule ex: {"or": [{"==": [{"var": "customerID"}, 400]}, {"==": [{"var": "firstName"}, John]}]}
 * @param {Object} data ex: {"customerID": 1001, "firstName": "John"}
 * @param {string} objectKey ex: "users"
 * @param {boolean} dataLazyLoaded ex: "true"
 * @returns the evaluated rule ex: true
 */
export const evaluateRule = (rule, data, objectKey, dataLazyLoaded) => {
    let formattedData = {};
    if (objectKey && dataLazyLoaded) {
        formattedData[objectKey] = data[objectKey] ? data[objectKey] : data;
    }
    else {
        formattedData = data;
    }
    return jsonLogic.apply(rule, formattedData);
};
//# sourceMappingURL=RuleUtilities.js.map