/* relationship logic  关联逻辑
  logicExpression: 
    1.logic1的逻辑: (key1='xxx'且key2='yyy') 或 (key3='aaa')
    {
      logic1: [
        [{ key: 'key1', value: '${*}' }, { key: 'key2', value: 'yyy' }],
        [{ key: 'key3', value: 'aaa' }],
      ]，
      ...
    }

  vals: {}
*/
const NOT_EMPTY = '${*}'

export function executeExpression(expression, vals) {
  let v;
  
  return expression.some(logic => {
    return logic.every(rule => {
      v = vals[rule.key];
      // 任意值且非空
      if (rule.value === NOT_EMPTY && v !== undefined && v !== null && v !== '') {
        return true;
      }
      // 相等
      if (rule.value === v) {
        return true;
      }
      // 不相等
      return false;
    });
  });
}


export function executeExpressionCollection(ExpressionCollection, vals) {
  return Object.keys(ExpressionCollection).map(key => {
    return {
      target: key,
      resolve: executeExpression(ExpressionCollection[key], vals)
    };
  });
}
