/* atoms.jsx — shared primitives for the BLAST ONCE site variations.
   Adapted from BlastBoard atoms — red accent, light/cream surfaces. */

const { useState, useEffect, useRef } = React;

/* ─── Icons (Phosphor-style, single stroke, currentColor) ─────────── */
const boStrokeFor = (w) => (w === 'bold' ? 2.25 : w === 'regular' ? 1.75 : 1.5);
const BoIcon = ({ size = 16, weight, children, style }) => (
  <svg
    width={size}
    height={size}
    viewBox="0 0 24 24"
    fill="none"
    stroke="currentColor"
    strokeWidth={boStrokeFor(weight)}
    strokeLinecap="round"
    strokeLinejoin="round"
    style={style}
  >
    {children}
  </svg>
);

const BoIcons = {
  ArrowRight:   (p) => <BoIcon {...p}><path d="M5 12h14M13 5l7 7-7 7" /></BoIcon>,
  ArrowDown:    (p) => <BoIcon {...p}><path d="M12 5v14M5 13l7 7 7-7" /></BoIcon>,
  ArrowUpRight: (p) => <BoIcon {...p}><path d="M7 17L17 7M9 7h8v8" /></BoIcon>,
  Phone:        (p) => <BoIcon {...p}><path d="M22 16.92v3a2 2 0 01-2.18 2 19.79 19.79 0 01-8.63-3.07 19.5 19.5 0 01-6-6 19.79 19.79 0 01-3.07-8.67A2 2 0 014.11 2h3a2 2 0 012 1.72 12.84 12.84 0 00.7 2.81 2 2 0 01-.45 2.11L8.09 9.91a16 16 0 006 6l1.27-1.27a2 2 0 012.11-.45 12.84 12.84 0 002.81.7A2 2 0 0122 16.92z" /></BoIcon>,
  Mail:         (p) => <BoIcon {...p}><rect x="3" y="5" width="18" height="14" rx="2" /><path d="M3 7l9 6 9-6" /></BoIcon>,
  MapPin:       (p) => <BoIcon {...p}><path d="M12 21s-7-7.2-7-12a7 7 0 1114 0c0 4.8-7 12-7 12z" /><circle cx="12" cy="9" r="2.5" /></BoIcon>,
  Drill:        (p) => <BoIcon {...p}><path d="M3 5h12v6H3zM15 7h3l2 2v0M9 11v4M7 15h4l-2 6z" /></BoIcon>,
  HardHat:      (p) => <BoIcon {...p}><path d="M3 18h18M5 18v-3a7 7 0 0114 0v3M10 8V5h4v3" /></BoIcon>,
  Shield:       (p) => <BoIcon {...p}><path d="M12 3l8 3v6c0 5-3.5 8.5-8 9-4.5-.5-8-4-8-9V6l8-3z" /><path d="M9 12l2 2 4-4" /></BoIcon>,
  Mountain:     (p) => <BoIcon {...p}><path d="M3 20l6-12 4 7 3-5 5 10z" /></BoIcon>,
  Compass:      (p) => <BoIcon {...p}><circle cx="12" cy="12" r="9" /><path d="M15.5 8.5L13 13l-4.5 2.5L11 11z" /></BoIcon>,
  Clipboard:    (p) => <BoIcon {...p}><rect x="6" y="4" width="12" height="17" rx="2" /><rect x="9" y="2" width="6" height="4" rx="1" /><path d="M9 11h6M9 15h6" /></BoIcon>,
  Pen:          (p) => <BoIcon {...p}><path d="M3 21l3.5-1L20 6.5 17.5 4 4 17.5 3 21z" /><path d="M15 6.5l2.5 2.5" /></BoIcon>,
  Quotes:       (p) => <BoIcon {...p}><path d="M6 17c0-3 2-5 5-5V9c-4 0-7 3-7 8v0M14 17c0-3 2-5 5-5V9c-4 0-7 3-7 8v0" /></BoIcon>,
  Check:        (p) => <BoIcon {...p}><path d="M5 12l5 5L20 7" /></BoIcon>,
  Spark:        (p) => <BoIcon {...p}><path d="M12 2v6M12 16v6M2 12h6M16 12h6M5 5l4 4M15 15l4 4M19 5l-4 4M9 15l-4 4" /></BoIcon>,
  Menu:         (p) => <BoIcon {...p}><path d="M4 7h16M4 12h16M4 17h16" /></BoIcon>,
};

/* ─── Logo / Wordmark ────────────────────────────────────────────────
   Real logo PNG when sized large; CSS wordmark for nav-scale use. */
function BoLogo({ height = 36, mode = 'image' }) {
  if (mode === 'image') {
    return (
      <img
        src="assets/blastonce_type1.jpg"
        alt="Blast Once Pty Ltd"
        style={{ height, width: 'auto', display: 'block' }}
      />
    );
  }
  // wordmark fallback — split treatment matching the logo nameplate
  const px = height;
  return (
    <span style={{
      font: `800 ${px * 0.55}px var(--font-sans)`,
      letterSpacing: '-0.04em',
      display: 'inline-flex',
      alignItems: 'center',
      lineHeight: 1,
    }}>
      <span style={{ color: 'var(--ink-900)' }}>BLAST</span>
      <span style={{
        background: 'var(--bo-red)',
        color: '#fff',
        padding: '4px 8px',
        marginLeft: 6,
        letterSpacing: 0,
      }}>ONCE</span>
    </span>
  );
}

/* ─── Eyebrow ────────────────────────────────────────────────────────
   Capsule with red dot + uppercase tracked label. */
function BoEyebrow({ children, tone = 'light' }) {
  const dark = tone === 'dark';
  return (
    <div style={{
      display: 'inline-flex', alignItems: 'center', gap: 8,
      border: `1px solid ${dark ? 'rgba(255,255,255,0.14)' : 'var(--bo-hair)'}`,
      background: dark ? 'rgba(255,255,255,0.06)' : '#fff',
      borderRadius: 9999, padding: '6px 12px',
      backdropFilter: dark ? 'blur(8px)' : undefined,
    }}>
      <span style={{
        width: 4, height: 4, borderRadius: '50%',
        background: 'var(--bo-red)',
      }} />
      <span style={{
        font: '500 10px/1 var(--font-sans)',
        letterSpacing: '0.22em',
        textTransform: 'uppercase',
        whiteSpace: 'nowrap',
        color: dark ? 'rgba(255,255,255,0.85)' : 'var(--ink-700)',
      }}>{children}</span>
    </div>
  );
}

/* ─── Bezel — the signature card frame ───────────────────────────── */
function BoBezel({ children, size = 'lg', style = {}, padding }) {
  const outerR = size === 'lg' ? 32 : 24;
  const pad = padding ?? (size === 'lg' ? 6 : 4);
  return (
    <div style={{
      position: 'relative',
      borderRadius: outerR,
      padding: pad,
      background: 'rgba(13,12,10,0.04)',
      boxShadow: '0 0 0 1px rgba(13,12,10,0.05)',
      ...style,
    }}>
      <div style={{
        position: 'relative',
        height: '100%', width: '100%',
        borderRadius: outerR - pad,
        background: '#fff',
        boxShadow: 'var(--bo-inset-light)',
      }}>
        {children}
      </div>
    </div>
  );
}

/* ─── CTAs ─────────────────────────────────────────────────────────── */
function BoPrimaryCTA({ href = '#contact', children, onClick, size = 'md' }) {
  const [hover, setHover] = useState(false);
  const big = size === 'lg';
  return (
    <a
      href={href} onClick={onClick}
      onMouseEnter={() => setHover(true)} onMouseLeave={() => setHover(false)}
      style={{
        display: 'inline-flex', alignItems: 'center', gap: 4,
        background: 'var(--bo-red)', color: '#fff',
        borderRadius: 9999,
        padding: big ? '10px 10px 10px 28px' : '8px 8px 8px 24px',
        font: `600 ${big ? 16 : 15}px var(--font-sans)`,
        letterSpacing: '-0.01em',
        boxShadow: hover ? 'var(--bo-shadow-red-strong)' : 'var(--bo-shadow-red)',
        textDecoration: 'none',
        transition: 'box-shadow 500ms var(--ease-magnetic)',
      }}
    >
      <span style={{ padding: '0 8px', whiteSpace: 'nowrap' }}>{children}</span>
      <span style={{
        display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
        width: big ? 36 : 32, height: big ? 36 : 32, borderRadius: 9999,
        background: 'rgba(255,255,255,0.18)',
        transform: hover ? 'translate(0.5px, -0.5px) scale(1.05)' : 'none',
        transition: 'transform 500ms var(--ease-magnetic)',
      }}>
        <BoIcons.ArrowUpRight size={big ? 16 : 14} weight="bold" />
      </span>
    </a>
  );
}

function BoSecondaryCTA({ href = '#', children, dark = false }) {
  const [hover, setHover] = useState(false);
  return (
    <a
      href={href}
      onMouseEnter={() => setHover(true)} onMouseLeave={() => setHover(false)}
      style={{
        display: 'inline-flex', alignItems: 'center', gap: 8,
        border: `1px solid ${dark ? 'rgba(255,255,255,0.18)' : 'var(--bo-hair)'}`,
        background: dark
          ? (hover ? 'rgba(255,255,255,0.08)' : 'rgba(255,255,255,0.04)')
          : (hover ? 'var(--ink-50)' : '#fff'),
        color: dark ? '#fff' : 'var(--ink-900)',
        borderRadius: 9999, padding: '10px 22px',
        font: '500 15px var(--font-sans)', letterSpacing: '-0.01em',
        whiteSpace: 'nowrap',
        textDecoration: 'none',
        transition: 'background 500ms var(--ease-magnetic)',
      }}
    >
      {children}
      <span style={{
        transform: hover ? 'translateX(2px)' : 'none',
        transition: 'transform 500ms var(--ease-magnetic)',
        opacity: 0.7,
      }}>
        <BoIcons.ArrowRight size={14} weight="light" />
      </span>
    </a>
  );
}

/* ─── Hairline kicker row ───────────────────────────────────────── */
function BoKickers({ items, color }) {
  return (
    <div style={{
      display: 'flex', alignItems: 'center', gap: 18,
      font: '400 11px/1 var(--font-sans)',
      letterSpacing: '0.22em',
      textTransform: 'uppercase',
      color: color || 'var(--ink-500)',
    }}>
      {items.map((label, i) => (
        <React.Fragment key={label}>
          <span style={{ whiteSpace: 'nowrap' }}>{label}</span>
          {i < items.length - 1 && (
            <span style={{
              height: 1, flex: 1,
              background: 'var(--bo-hair)',
              minWidth: 16,
            }} />
          )}
        </React.Fragment>
      ))}
    </div>
  );
}

/* ─── Icon container (rounded tile) ────────────────────────────── */
function BoIconTile({ children, size = 44, tone = 'ink' }) {
  const tones = {
    ink:   { bg: 'var(--ink-900)', fg: '#fff' },
    red:   { bg: 'var(--bo-red)',  fg: '#fff' },
    cream: { bg: 'var(--bo-cream-2)', fg: 'var(--ink-900)' },
    paper: { bg: '#fff', fg: 'var(--ink-900)', ring: true },
  }[tone];
  return (
    <div style={{
      width: size, height: size,
      borderRadius: 14,
      background: tones.bg,
      color: tones.fg,
      display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
      boxShadow: tones.ring
        ? '0 0 0 1px var(--bo-hair), 0 4px 12px -4px rgba(13,12,10,0.1)'
        : '0 4px 12px -4px rgba(13,12,10,0.18)',
      flexShrink: 0,
    }}>
      {children}
    </div>
  );
}

Object.assign(window, {
  BoIcons, BoLogo, BoEyebrow, BoBezel,
  BoPrimaryCTA, BoSecondaryCTA, BoKickers, BoIconTile,
});
