// Shared utilities and icons for Clearspace

const fmt$ = (n) => {
  if (n == null) return "—";
  return "$" + Math.round(n).toLocaleString();
};

const fmt$k = (n) => {
  if (n >= 1000) return "$" + (n / 1000).toFixed(n % 1000 === 0 ? 0 : 1) + "k";
  return "$" + n;
};

const typeLabel = (t) => ({
  "whole-lot": "Whole Lot",
  "category-lot": "Category Lot",
  "premium": "Premium Item",
}[t] || t);

const typeTagClass = (t) => ({
  "whole-lot": "tag-whole",
  "category-lot": "tag-category",
  "premium": "tag-premium",
}[t] || "");

function useCountdown(targetMs) {
  const [now, setNow] = React.useState(Date.now());
  React.useEffect(() => {
    const i = setInterval(() => setNow(Date.now()), 1000);
    return () => clearInterval(i);
  }, []);
  const diff = Math.max(0, targetMs - now);
  const h = Math.floor(diff / 3600000);
  const m = Math.floor((diff % 3600000) / 60000);
  const s = Math.floor((diff % 60000) / 1000);
  return {
    diff,
    h, m, s,
    pad: (n) => String(n).padStart(2, "0"),
    short: diff === 0 ? "Closed"
      : h > 24 ? `${Math.floor(h/24)}d ${h%24}h`
      : h > 0 ? `${h}h ${String(m).padStart(2,"0")}m`
      : `${String(m).padStart(2,"0")}:${String(s).padStart(2,"0")}`,
    long: diff === 0 ? "Closed"
      : `${String(h).padStart(2,"0")}:${String(m).padStart(2,"0")}:${String(s).padStart(2,"0")}`,
    urgent: diff > 0 && diff < 1000 * 60 * 60 * 3,
    closed: diff === 0,
  };
}

// --- Icons (stroke 1.5, 20px) ---
const Ic = ({ children, size = 18 }) => (
  <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round">
    {children}
  </svg>
);

const IcSearch = (p) => <Ic {...p}><circle cx="11" cy="11" r="7"/><path d="m20 20-3.5-3.5"/></Ic>;
const IcBookmark = (p) => <Ic {...p}><path d="M6 3h12v18l-6-4-6 4V3z"/></Ic>;
const IcBookmarkFill = (p) => <Ic {...p}><path d="M6 3h12v18l-6-4-6 4V3z" fill="currentColor"/></Ic>;
const IcClock = (p) => <Ic {...p}><circle cx="12" cy="12" r="9"/><path d="M12 7v5l3 2"/></Ic>;
const IcMapPin = (p) => <Ic {...p}><path d="M12 22s-7-7.5-7-13a7 7 0 0 1 14 0c0 5.5-7 13-7 13z"/><circle cx="12" cy="9" r="2.5"/></Ic>;
const IcArrow = (p) => <Ic {...p}><path d="M5 12h14m-6-6 6 6-6 6"/></Ic>;
const IcCheck = (p) => <Ic {...p}><path d="m5 13 5 5 9-11"/></Ic>;
const IcShield = (p) => <Ic {...p}><path d="M12 2 4 6v6c0 5 3.5 8.5 8 10 4.5-1.5 8-5 8-10V6l-8-4z"/></Ic>;
const IcBolt = (p) => <Ic {...p}><path d="M13 2 4 14h7l-1 8 9-12h-7l1-8z"/></Ic>;
const IcEye = (p) => <Ic {...p}><path d="M2 12s4-7 10-7 10 7 10 7-4 7-10 7S2 12 2 12z"/><circle cx="12" cy="12" r="3"/></Ic>;
const IcDownload = (p) => <Ic {...p}><path d="M12 3v12m0 0-4-4m4 4 4-4M4 21h16"/></Ic>;
const IcFilter = (p) => <Ic {...p}><path d="M3 5h18l-7 9v5l-4 2v-7L3 5z"/></Ic>;
const IcBell = (p) => <Ic {...p}><path d="M6 9a6 6 0 1 1 12 0c0 7 2 8 2 8H4s2-1 2-8z"/><path d="M10 21a2 2 0 0 0 4 0"/></Ic>;
const IcChev = (p) => <Ic {...p}><path d="m9 6 6 6-6 6"/></Ic>;
const IcChevDown = (p) => <Ic {...p}><path d="m6 9 6 6 6-6"/></Ic>;
const IcSparkle = (p) => <Ic {...p}><path d="M12 3v4m0 10v4M3 12h4m10 0h4M5.6 5.6l2.8 2.8m7.2 7.2 2.8 2.8M5.6 18.4l2.8-2.8m7.2-7.2 2.8-2.8"/></Ic>;
const IcTruck = (p) => <Ic {...p}><path d="M3 7h11v10H3zM14 10h4l3 3v4h-7"/><circle cx="7" cy="19" r="2"/><circle cx="17" cy="19" r="2"/></Ic>;
const IcBox = (p) => <Ic {...p}><path d="M3 7 12 3l9 4v10l-9 4-9-4V7z"/><path d="M3 7l9 4 9-4M12 11v10"/></Ic>;
const IcX = (p) => <Ic {...p}><path d="M6 6l12 12M18 6 6 18"/></Ic>;
const IcPlus = (p) => <Ic {...p}><path d="M12 5v14M5 12h14"/></Ic>;

Object.assign(window, {
  fmt$, fmt$k, typeLabel, typeTagClass, useCountdown,
  IcSearch, IcBookmark, IcBookmarkFill, IcClock, IcMapPin, IcArrow, IcCheck,
  IcShield, IcBolt, IcEye, IcDownload, IcFilter, IcBell, IcChev, IcChevDown,
  IcSparkle, IcTruck, IcBox, IcX, IcPlus,
  IcClose: IcX,
});
