/* CoordGlyph — abstract POI data fingerprint derived from seed.
   Used as a small corner badge overlay on photos, not as main visual. */

function hash(seed) {
  let h = 2166136261;
  for (let i = 0; i < seed.length; i++) {
    h ^= seed.charCodeAt(i);
    h = Math.imul(h, 16777619);
  }
  return (h >>> 0) / 4294967296;
}

function CoordGlyph({ seed = 'x', size = 64, accent }) {
  const h1 = hash(seed);
  const h2 = hash(seed + 'a');
  const h3 = hash(seed + 'b');

  const cx = 32 + (h1 - 0.5) * 18;
  const cy = 32 + (h2 - 0.5) * 18;

  const dots = [];
  for (let i = 0; i < 6; i++) {
    const a = hash(seed + 'd' + i);
    const b = hash(seed + 'e' + i);
    dots.push({
      x: 8 + a * 48,
      y: 8 + b * 48,
      r: 0.7 + hash(seed + 'r' + i) * 1.0,
    });
  }

  return (
    <svg width={size} height={size} viewBox="0 0 64 64" style={{display:'block'}}>
      <defs>
        <clipPath id={`clip-${seed}`}>
          <rect width="64" height="64" rx="10" />
        </clipPath>
      </defs>
      <g clipPath={`url(#clip-${seed})`}>
        <rect width="64" height="64" fill="var(--bg-sunk)" />
        {/* concentric rings */}
        {[7, 14, 21].map((r, i) => (
          <circle
            key={r}
            cx={cx} cy={cy} r={r}
            fill="none"
            stroke={accent || 'var(--ink-2)'}
            strokeOpacity={0.40 - i * 0.10}
            strokeWidth="0.8"
          />
        ))}
        {/* dot constellation */}
        {dots.map((d, i) => (
          <circle key={i} cx={d.x} cy={d.y} r={d.r} fill="var(--ink-3)" opacity="0.45" />
        ))}
        {/* center pin */}
        <circle cx={cx} cy={cy} r="2.4" fill={accent || 'var(--ink)'} />
        <circle cx={cx} cy={cy} r="5" fill="none" stroke={accent || 'var(--ink)'} strokeWidth="0.7" />
        {/* corner ticks */}
        <g stroke={accent || 'var(--ink)'} strokeWidth="0.9" fill="none" opacity="0.6">
          <path d="M 3 3 L 8 3 M 3 3 L 3 8" />
          <path d="M 61 3 L 56 3 M 61 3 L 61 8" />
          <path d="M 3 61 L 8 61 M 3 61 L 3 56" />
          <path d="M 61 61 L 56 61 M 61 61 L 61 56" />
        </g>
      </g>
    </svg>
  );
}

/* Deterministic photo placeholder using picsum.photos */
function poiPhoto(seed, w, h) {
  const s = (seed || 'place')
    .replace(/[^a-z0-9]/gi, '')
    .toLowerCase()
    .substring(0, 16) || 'place';
  return `https://picsum.photos/seed/${s}/${w}/${h}`;
}

window.CoordGlyph = CoordGlyph;
window.govibeHash = hash;
window.poiPhoto = poiPhoto;
