export class RuleCollector {
  constructor(arbiter) {
    this.arbiter = arbiter;
  }

  collectRules(config, parentRelation = null, relationName = null) {
    const rules = [];
    
    const base = {
      ruleType: config.ruleType || 'defeasible',
      priority: config.priority || 0,
      weight: config.weight !== undefined ? config.weight : 1.0,
      ...config,
      parentRelation: config.parentRelation !== undefined ? config.parentRelation : parentRelation,
    };
    
    if (!base.relation && relationName) {
      base.relation = relationName;
    }
    
    // Logical operators must not be flattened — intersection/exclusion lose their semantics if
    // treated as a flat union (max-allow). Only flatten union operators into the parent list.
    if (config.union) {
      for (const child of config.union) {
        rules.push(...this.collectRules(child, parentRelation, relationName));
      }
    } else if (config.intersection || config.exclusion) {
      // Intersection and exclusion carry structure that must be preserved at the parent level.
      // They are handled by AuthorizationChecker's logical dispatch, not flattened here.
      rules.push(base);
    } else {
      rules.push(base);
    }
    
    return rules;
  }
} 