// d4not — shared utilities & atoms

const { useState, useEffect, useMemo, useRef, useCallback } = React;

function fmt(n) {
  if (n == null) return "—";
  if (n >= 1_000_000) return (n / 1_000_000).toFixed(1).replace(/\.0$/, "") + "M";
  if (n >= 1_000)     return (n / 1_000).toFixed(1).replace(/\.0$/, "") + "k";
  return String(n);
}
function fmtTokens(n) {
  if (!n) return "0";
  if (n >= 1_000_000) return (n / 1_000_000).toFixed(2) + "M";
  if (n >= 1_000)     return Math.round(n / 1_000) + "k";
  return String(n);
}
function fmtDate(s) {
  if (!s) return "—";
  return new Date(s).toLocaleDateString("en-US", { month: "short", day: "numeric", year: "numeric" });
}
function relTime(s) {
  const days = Math.max(1, Math.round((Date.now() - new Date(s).getTime()) / 86400000));
  if (days < 7) return `${days}d ago`;
  if (days < 35) return `${Math.round(days/7)}w ago`;
  if (days < 365) return `${Math.round(days/30)}mo ago`;
  return `${Math.round(days/365)}y ago`;
}
function hostLabel(h) {
  const lookup = { github: "GitHub", gitlab: "GitLab", codeberg: "Codeberg", external: "External" };
  return lookup[h] || h;
}

function Wordmark({ big = false }) {
  return (
    <span className="wordmark" style={big ? { fontSize: 62, lineHeight: 0.9 } : null}>
      <span className="d">d</span><span className="four">4</span><span className="not">not</span>
      {!big && <span className="lib">/library</span>}
    </span>
  );
}

function Tag({ children, variant = "" }) {
  return <span className={"tag " + variant}>{children}</span>;
}

Object.assign(window, { fmt, fmtTokens, fmtDate, relTime, hostLabel, Wordmark, Tag });
