{"version":3,"file":"RuleUtilities.mjs","names":["jsonLogic","getPropertyMappingList","obj","mappings","keys","Object","forEach","key","mappingObj","variable","value","push","getPropertyMappingObj","properties","mapping","length","property","mapRuleData","data","mappedData","dataKey","dataValue","mapRule","rule","updatedJsonLogic","JSON","stringify","substr","var","newSubstr","replaceAll","parse","err","console","evaluateRule","objectKey","dataLazyLoaded","formattedData","apply"],"sources":["../../../src/utilities/rules/RuleUtilities.js"],"sourcesContent":["import jsonLogic from 'json-logic-js';\n\n/**\n * Constructs an array of object from the object map.\n *\n * @param {Object} obj ex: {\"a\": \"customerID\", \"b\": \"firstName\"}\n * @returns an array of object ex. [{\"variable\": \"a\", \"value\": \"serviceLocationID\"}, {\"variable\": \"b\", \"value\" \"firstName\"}]\n */\nexport const getPropertyMappingList = (obj) => {\n  const mappings = [];\n  if (obj) {\n    const keys = Object.keys(obj);\n    keys.forEach((key) => {\n      const mappingObj = { variable: key, value: obj[key] };\n      mappings.push(mappingObj);\n    });\n  }\n  return mappings;\n};\n\n/**\n * Constructs a map object where variables are keys and values are the values.\n *\n * @param {Array<Object>} properties ex. [{\"variable\": \"a\", \"value\": \"serviceLocationID\"}, {\"variable\": \"b\", \"value\" \"firstName\"}]\n * @returns mapped object ex: {\"a\": \"customerID\", \"b\": \"firstName\"}\n */\nexport const getPropertyMappingObj = (properties) => {\n  const mapping = {};\n  if (properties?.length) {\n    properties.forEach((property) => {\n      mapping[property.variable] = property.value;\n    });\n  }\n  return mapping;\n};\n\n/**\n * Maps the keys of mapping to the values of data.\n *\n * @param {Object} mapping ex: {\"a\": \"customerID\", \"b\": \"firstName\"}\n * @param {Object} data ex: {\"customerID\": 1001, \"firstName\": \"John\"}\n * @returns flat object ex: {\"a\": 1001, \"b\": \"John\"}\n */\nexport const mapRuleData = (mapping, data) => {\n  const mappedData = {};\n  if (mapping && mapping.length) {\n    const keys = Object.keys(mapping);\n    keys.forEach((key) => {\n      const dataKey = mapping[key];\n      if (dataKey) {\n        const dataValue = data[dataKey];\n        mappedData[key] = dataValue;\n      }\n    });\n  }\n  return mappedData;\n};\n\n/**\n * Maps the mapping values in the mapping object to the variables in the rule.\n *\n * @param {Object} mapping ex: {\"a\": \"customerID\", \"b\": \"firstName\"}\n * @param {Object} rule ex: {\"or\": [{\"==\": [{\"var\": \"a\"}, 400]}, {\"==\": [{\"var\": \"b\"}, John]}]}\n * @returns jsonLogic object with the correct variable names ex: [{\"==\": [{\"var\": \"customerID\"}, 400]}, {\"==\": [{\"var\": \"firstName\"}, John]}]\n */\nexport const mapRule = (mapping = {}, rule = '') => {\n  const keys = Object.keys(mapping);\n  let updatedJsonLogic = JSON.stringify(rule);\n  keys.forEach((key) => {\n    const substr = JSON.stringify({ var: key });\n    const newSubstr = JSON.stringify({ var: mapping[key] });\n    updatedJsonLogic = updatedJsonLogic.replaceAll(substr, newSubstr);\n  });\n  try {\n    return JSON.parse(updatedJsonLogic);\n  } catch (err) {\n    console.err('Error with parsing JSON Logic String');\n    return {};\n  }\n};\n\n/**\n * Evaluates the rule with provided data.\n *\n * @param {Object} rule ex: {\"or\": [{\"==\": [{\"var\": \"customerID\"}, 400]}, {\"==\": [{\"var\": \"firstName\"}, John]}]}\n * @param {Object} data ex: {\"customerID\": 1001, \"firstName\": \"John\"}\n * @param {string} objectKey ex: \"users\"\n * @param {boolean} dataLazyLoaded ex: \"true\"\n * @returns the evaluated rule ex: true\n */\nexport const evaluateRule = (rule, data, objectKey, dataLazyLoaded) => {\n  let formattedData = {};\n  if (objectKey && dataLazyLoaded) {\n    formattedData[objectKey] = data[objectKey] ? data[objectKey] : data;\n  } else {\n    formattedData = data;\n  }\n  return jsonLogic.apply(rule, formattedData);\n};\n"],"mappings":";;;;;;;;AAQA,MAAaC,0BAA0BC,QAAQ;CAC7C,MAAMC,WAAW,EAAE;AACnB,KAAID,IACWG,QAAOD,KAAKF,IAAI,CACxBI,SAASC,QAAQ;EACpB,MAAMC,aAAa;GAAEC,UAAUF;GAAKG,OAAOR,IAAIK;GAAM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