///|
/// 批量路径过滤汇总结果。
#derive(Eq, Debug)
pub struct FilterReport {
  included : Array[PathDecision]
  ignored : Array[PathDecision]
  invalid_paths : Array[(String, PathError)]
}

///|
/// 详细归因报告:除完整决策外,当路径因父目录剪枝被阻断时,
/// 额外列出所有本可匹配该路径但因父目录被忽略而失效的否定规则。
#derive(Eq, Debug)
pub struct ExplainDetail {
  decision : PathDecision
  blocked_negations : Array[CompiledRule]
}

///|
/// 返回最终决定该路径裁定的规则来源;若默认保留则返回 `None`。
pub fn PathDecision::effective_rule(self : PathDecision) -> RuleSource? {
  match self.reason {
    DefaultIncluded => None
    MatchedRule(_, src, _) => Some(src)
    BlockedByIgnoredParent(_, _, src) => Some(src)
  }
}

///|
/// 解释单一路径为何被纳入或忽略,返回包含完整命中轨迹的 `PathDecision`。
pub fn RuleSet::explain(
  self : RuleSet,
  path : String,
  is_dir~ : Bool = false,
) -> Result[PathDecision, PathError] {
  match normalize_path(path, is_dir~) {
    Err(e) => Err(e)
    Ok(norm) => Ok(self.evaluate_normalized(norm))
  }
}

///|
/// 深度解释单一路径的裁定原因,并识别因父目录剪枝而失效的否定规则。
pub fn RuleSet::explain_detailed(
  self : RuleSet,
  path : String,
  is_dir~ : Bool = false,
) -> Result[ExplainDetail, PathError] {
  let norm = match normalize_path(path, is_dir~) {
    Err(e) => return Err(e)
    Ok(p) => p
  }
  let decision = self.evaluate_normalized(norm)
  let blocked_negations : Array[CompiledRule] = []
  match decision.reason {
    BlockedByIgnoredParent(_, _, _) =>
      for rule in self.rules {
        if rule.action == Include &&
          rule.matches(norm, case_sensitive=self.options.case_sensitive) {
          blocked_negations.push(rule)
        }
      }
    _ => ()
  }
  Ok({ decision, blocked_negations, })
}

///|
/// 对一组带目录标记的路径条目执行批量过滤,分别归集保留项、忽略项与非法路径。
pub fn RuleSet::filter_entries(
  self : RuleSet,
  entries : Array[PathEntry],
) -> FilterReport {
  let included : Array[PathDecision] = []
  let ignored : Array[PathDecision] = []
  let invalid_paths : Array[(String, PathError)] = []
  for entry in entries {
    match normalize_path(entry.path, is_dir=entry.is_dir) {
      Err(err) => invalid_paths.push((entry.path, err))
      Ok(norm) => {
        let decision = self.evaluate_normalized(norm)
        match decision.verdict {
          Included => included.push(decision)
          Ignored => ignored.push(decision)
        }
      }
    }
  }
  { included, ignored, invalid_paths, }
}

///|
/// 对字符串路径数组执行批量过滤(以 `/` 或 `\` 结尾的路径自动视为目录)。
pub fn RuleSet::filter_paths(
  self : RuleSet,
  paths : Array[String],
) -> FilterReport {
  let entries : Array[PathEntry] = []
  for p in paths {
    entries.push(PathEntry::new(p))
  }
  self.filter_entries(entries)
}