// PapersVal — primitives: icons, pills, helpers
// Exported via window for app.jsx

// ---------- Icons (inline SVG, currentColor) ----------
const Icon = {
  Search: () => (
    <svg width="13" height="13" viewBox="0 0 16 16" fill="none">
      <circle cx="7" cy="7" r="5" stroke="currentColor" strokeWidth="1.4" />
      <path d="M11 11l3 3" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" />
    </svg>
  ),
  Plus: () => (
    <svg width="12" height="12" viewBox="0 0 12 12" fill="none">
      <path d="M6 1.5v9M1.5 6h9" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" />
    </svg>
  ),
  Close: () => (
    <svg width="14" height="14" viewBox="0 0 16 16" fill="none">
      <path d="M3 3l10 10M13 3L3 13" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" />
    </svg>
  ),
  Copy: () => (
    <svg width="13" height="13" viewBox="0 0 16 16" fill="none">
      <rect x="3" y="5" width="9" height="9" rx="1.5" stroke="currentColor" strokeWidth="1.3" />
      <path d="M6 2.5h6.5A1.5 1.5 0 0 1 14 4v6.5" stroke="currentColor" strokeWidth="1.3" strokeLinecap="round" />
    </svg>
  ),
  Check: () => (
    <svg width="13" height="13" viewBox="0 0 16 16" fill="none">
      <path d="M3 8.5l3.5 3.5 7-7.5" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" />
    </svg>
  ),
  ArrowLeft: () => (
    <svg width="12" height="12" viewBox="0 0 16 16" fill="none">
      <path d="M10 3L5 8l5 5" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round" />
    </svg>
  ),
  ArrowRight: () => (
    <svg width="12" height="12" viewBox="0 0 16 16" fill="none">
      <path d="M6 3l5 5-5 5" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round" />
    </svg>
  ),
  ChevronDown: () => (
    <svg width="10" height="10" viewBox="0 0 12 12" fill="none">
      <path d="M3 4.5l3 3 3-3" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round" />
    </svg>
  ),
  Share: () => (
    <svg width="13" height="13" viewBox="0 0 16 16" fill="none">
      <path d="M8 2v9M5 5l3-3 3 3M3 11v2a1 1 0 0 0 1 1h8a1 1 0 0 0 1-1v-2"
        stroke="currentColor" strokeWidth="1.3" strokeLinecap="round" strokeLinejoin="round" />
    </svg>
  ),
  Download: () => (
    <svg width="13" height="13" viewBox="0 0 16 16" fill="none">
      <path d="M8 2v9M5 8l3 3 3-3M3 14h10"
        stroke="currentColor" strokeWidth="1.3" strokeLinecap="round" strokeLinejoin="round" />
    </svg>
  ),
  More: () => (
    <svg width="14" height="14" viewBox="0 0 16 16" fill="none">
      <circle cx="3.5" cy="8" r="1.2" fill="currentColor" />
      <circle cx="8" cy="8" r="1.2" fill="currentColor" />
      <circle cx="12.5" cy="8" r="1.2" fill="currentColor" />
    </svg>
  ),
  Link: () => (
    <svg width="13" height="13" viewBox="0 0 16 16" fill="none">
      <path d="M7 9a3 3 0 0 0 4.2.2l2-2a3 3 0 0 0-4.2-4.2L8 4" stroke="currentColor" strokeWidth="1.3" strokeLinecap="round" />
      <path d="M9 7a3 3 0 0 0-4.2-.2l-2 2a3 3 0 0 0 4.2 4.2L8 12" stroke="currentColor" strokeWidth="1.3" strokeLinecap="round" />
    </svg>
  ),
  Edit: () => (
    <svg width="13" height="13" viewBox="0 0 16 16" fill="none">
      <path d="M10 3l3 3-8 8H2v-3l8-8z" stroke="currentColor" strokeWidth="1.3" strokeLinejoin="round" />
    </svg>
  ),
  Sparkle: () => (
    <svg width="12" height="12" viewBox="0 0 16 16" fill="none">
      <path d="M8 1l1.5 4.5L14 7l-4.5 1.5L8 13l-1.5-4.5L2 7l4.5-1.5L8 1z" stroke="currentColor" strokeWidth="1.2" strokeLinejoin="round" />
    </svg>
  ),
  Folder: () => (
    <svg width="13" height="13" viewBox="0 0 16 16" fill="none">
      <path d="M2 5a1 1 0 0 1 1-1h3l1.5 1.5H13a1 1 0 0 1 1 1V12a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V5z"
        stroke="currentColor" strokeWidth="1.3" strokeLinejoin="round" />
    </svg>
  ),
  Menu: () => (
    <svg width="14" height="14" viewBox="0 0 16 16" fill="none">
      <path d="M2.5 4.5h11M2.5 8h11M2.5 11.5h11" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" />
    </svg>
  ),
  Refresh: () => (
    <svg width="13" height="13" viewBox="0 0 16 16" fill="none">
      <path d="M13.5 8a5.5 5.5 0 1 1-1.6-3.9M13.5 2v3.5H10" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round" />
    </svg>
  ),
  Accept: () => (
    <svg width="13" height="13" viewBox="0 0 16 16" fill="none">
      <circle cx="8" cy="8" r="6" stroke="currentColor" strokeWidth="1.3" />
      <path d="M5 8l2 2 4-4" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round" />
    </svg>
  ),
  Replace: () => (
    <svg width="13" height="13" viewBox="0 0 16 16" fill="none">
      <path d="M4 4h6l2 2v6a1 1 0 0 1-1 1H4a1 1 0 0 1-1-1V5a1 1 0 0 1 1-1z" stroke="currentColor" strokeWidth="1.3" strokeLinejoin="round" />
      <path d="M6 9l2 2 3-3" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round" />
    </svg>
  ),
  Note: () => (
    <svg width="13" height="13" viewBox="0 0 16 16" fill="none">
      <path d="M3 3h10v8l-3 3H3V3z" stroke="currentColor" strokeWidth="1.3" strokeLinejoin="round" />
      <path d="M5.5 6.5h5M5.5 9h3" stroke="currentColor" strokeWidth="1.3" strokeLinecap="round" />
    </svg>
  )
};

// ---------- Status / Severity language ----------
const STATUS_LABEL = {
  match: "Matches",
  partial: "Partial match",
  does_not_match: "Mismatch",
  not_found: "Not found",
  needs_review: "Needs review",
  extra: "Extra clause",
  n_a: "Not applicable"
};
const SEV_LABEL = { high: "High", medium: "Medium", low: "Low", info: "Extra" };

function StatusPill({ status, compact }) {
  const cls = status === "does_not_match" ? "dnm" : status;
  return (
    <span className={`status-pill ${cls}`}>
      <span className="glyph"></span>
      <span>{STATUS_LABEL[status]}</span>
    </span>
  );
}
function SevPill({ severity }) {
  const cls = severity === "medium" ? "med" : severity === "info" ? "info" : severity;
  return <span className={`sev-pill ${cls}`}>{SEV_LABEL[severity]}</span>;
}

// ---------- Render contract paragraph with marks ----------
// Given paragraph text and an array of marks for this paragraph, return JSX
// with <span class="mark"> wrappers around the right offsets.
// findingsState: { [id]: { resolution: 'replaced' | 'accepted' | 'noted', ... } }
// viewMode: 'review' (default — annotations + original) | 'final' (suggestions applied inline)
// filterMatch: function(finding) -> bool. If supplied, non-matching marks render as dimmed.
function renderParagraph(text, marks, selectedFindingId, onMarkClick, findingsById, findingsState, viewMode = "review", filterMatch = null) {
  if (!marks || marks.length === 0) return text;
  const sorted = [...marks].sort((a, b) => a.start - b.start);
  const out = [];
  let cursor = 0;
  sorted.forEach((m, i) => {
    if (m.start > cursor) out.push(text.slice(cursor, m.start));
    const finding = findingsById[m.findingId];
    const isSelected = selectedFindingId === m.findingId;
    const isConflict = finding && finding.severity === "high";
    const isExtra = finding && finding.status === "extra";
    const state = findingsState && findingsState[m.findingId];
    const replaced = state?.resolution === "replaced";
    const accepted = state?.resolution === "accepted";
    const noted = state?.resolution === "noted";

    const originalSlice = text.slice(m.start, m.end);
    const matchesFilter = !filterMatch || (finding && filterMatch(finding));

    if (viewMode === "final") {
      if (replaced && finding?.suggested) {
        out.push(<span key={`m-${i}`} className="final-applied">{finding.suggested}</span>);
      } else {
        out.push(originalSlice);
      }
    } else {
      const cls = ["mark"];
      if (isConflict) cls.push("conflict");
      if (isExtra) cls.push("extra");
      if (isSelected) cls.push("selected");
      if (replaced) cls.push("applied");
      if (accepted) cls.push("accepted");
      if (noted) cls.push("noted");
      if (!matchesFilter) cls.push("dimmed");
      out.push(
        <span key={`m-${i}`} className={cls.join(" ")} data-finding={m.findingId}
          onClick={(e) => { e.stopPropagation(); onMarkClick && onMarkClick(m.findingId); }}>
          {originalSlice}
        </span>
      );
    }
    cursor = m.end;
  });
  if (cursor < text.length) out.push(text.slice(cursor));
  return out;
}

// ---------- Mini stat (for sidebar history rows) ----------
function MiniStat({ kind, value, glyph, children }) {
  return (
    <span className={`mini-stat ${kind || ""}`}>
      <span className="glyph"></span>
      {children}
    </span>
  );
}

Object.assign(window, {
  Icon,
  StatusPill,
  SevPill,
  renderParagraph,
  MiniStat,
  STATUS_LABEL,
  SEV_LABEL
});
