// helpers.jsx — shared lookups and small components
// Loaded BEFORE the dashboard/app

const OUTCOME_LABELS = {
  required: "Required",
  borderline: "Borderline",
  info_needed: "Info needed",
  not_required: "Not required",
  excluded: "Screened out",
};

const OUTCOME_ORDER = ["required", "borderline", "info_needed", "not_required", "excluded"];

const CONF_LABELS = { high: "High", medium: "Medium", low: "Low" };
const CONF_PIPS = { high: 3, medium: 2, low: 1 };

function Badge({ outcome, children }) {
  return (
    <span className={`badge ${outcome}`}>
      {children || OUTCOME_LABELS[outcome]}
    </span>
  );
}

function ConfBar({ confidence }) {
  const n = CONF_PIPS[confidence] || 0;
  return (
    <span className="conf-bar" title={`${CONF_LABELS[confidence] || "—"} confidence`}>
      {[0, 1, 2].map((i) => (
        <span key={i} className={`pip ${i < n ? "on" : ""}`} />
      ))}
    </span>
  );
}

function FlagSet({ rec }) {
  const flags = [];
  if (rec.outcome === "required" || rec.outcome === "borderline" || rec.outcome === "info_needed") {
    flags.push({ cls: "attn", label: "!", title: "Needs attention" });
  }
  if (rec.overridden) flags.push({ cls: "over", label: "✎", title: "Lawyer override" });
  if (rec.approved) flags.push({ cls: "ok", label: "✓", title: "Partner-approved" });
  if (!flags.length) return null;
  return (
    <span className="flag-set">
      {flags.map((f, i) => (
        <span key={i} className={`flag ${f.cls}`} title={f.title}>{f.label}</span>
      ))}
    </span>
  );
}

function needsAttention(r) {
  return ["required", "borderline", "info_needed"].includes(r.outcome) && !r.approved;
}

function effectiveOutcome(r) {
  return r.lawyerOutcome || r.outcome;
}

function regionLabel(id, regions) {
  return regions.find((r) => r.id === id)?.short || id;
}

window.MC_HELPERS = {
  OUTCOME_LABELS, OUTCOME_ORDER, CONF_LABELS, CONF_PIPS,
  Badge, ConfBar, FlagSet, needsAttention, effectiveOutcome, regionLabel,
};
