/**
 * Baseline 比较阈值 — PR-16
 *
 * 集中管理 Historical Baseline Compare 的阈值配置。
 *
 * 纯配置模块，无副作用。
 *
 * @module baseline-thresholds
 */

// ─── 状态常量 ────────────────────────────────────────

export const COMPARE_PASS = "pass";
export const COMPARE_WARN = "warn";
export const COMPARE_FAIL = "fail";

// ─── 阈值配置 ────────────────────────────────────────

/**
 * 默认阈值：
 *   |deltaPercent| ≤ 20%  → pass
 *   |deltaPercent| > 20%  → warn
 *   |deltaPercent| > 50%  → fail
 */
export const DEFAULT_THRESHOLDS = {
  /** 超过此百分比触发 warning */
  warnPercent: 20,
  /** 超过此百分比触发 fail */
  failPercent: 50,
};

// ─── 比较指标定义 ─────────────────────────────────────

/**
 * 指标定义：指标名 → 数据路径 + 显示名
 */
export const METRICS = /** @type {const} */ ([
  { key: "blocCount",              label: "Bloc Count",              path: ["blocCount"] },
  { key: "cycleCount",             label: "Cycle Count",             path: ["cycleCount"] },
  { key: "candidateCount",         label: "Candidate Count",         path: ["candidateCount"] },
  { key: "activeCandidateCount",   label: "Active Candidate Count",  path: ["activeCandidateCount"] },
  { key: "archivedCandidateCount", label: "Archived Candidate Count", path: ["archivedCandidateCount"] },
]);

/**
 * 从数据对象中按路径取值（浅层，仅支持一层）
 * @param {object} data
 * @param {string[]} path
 * @returns {number|null}
 */
export function getMetricValue(data, path) {
  if (!data || typeof data !== "object") return null;
  let val = data;
  for (const key of path) {
    if (val == null || typeof val !== "object") return null;
    val = val[key];
  }
  if (typeof val !== "number" || !Number.isFinite(val)) return null;
  return val;
}
