// Caliber brand primitives — logo, star glyph, iconography, misc atoms.
// Exports to window so other babel scripts can use them.

/* ---------- Caliber Star (brand glyph — exact SVG) ---------- */
function CalStar({ size = 28, grad = true, id = 'cs', style = {} }) {
  const gid = id + '-g-' + Math.floor(Math.random() * 1e6);
  return (
    <svg width={size} height={(size * 300) / 386} viewBox="0 0 386 300" fill="none" style={style} aria-hidden xmlns="http://www.w3.org/2000/svg">
      {grad && (
        <defs>
          <linearGradient id={gid} x1="377.787" y1="69.863" x2="-12.0722" y2="303.277" gradientUnits="userSpaceOnUse">
            <stop stopColor="#0084FF" />
            <stop offset="1" stopColor="#0009B6" />
          </linearGradient>
        </defs>
      )}
      <path
        d="M380.962 84.9175L367.291 98.461C324.661 140.826 307.959 202.473 323.516 260.317L328.499 278.797C332.944 295.343 312.404 307.152 300.147 295.01L286.476 281.466C284.321 279.331 282.166 277.33 279.943 275.328C228.558 229.694 152.996 222.755 91.7789 254.312L21.4025 290.606C4.70069 295.01 -7.21953 274.661 5.03742 262.518L71.9118 220.286C130.099 183.525 161.819 115.207 147.609 48.3565C147.002 45.4209 146.262 42.5521 145.521 39.6833L140.537 21.2026C136.092 4.6568 156.633 -7.15213 168.89 4.99039L182.561 18.534C225.326 60.7659 287.553 77.3117 345.942 61.9001L364.597 56.963C381.299 52.5597 393.219 72.9084 380.962 85.0509V84.9175Z"
        fill={grad ? `url(#${gid})` : 'currentColor'}
      />
    </svg>
  );
}

/* ---------- Caliber wordtext (letters only, no star) ---------- */
function CalWordText({ height = 28, color = '#111111', style = {} }) {
  // "caliber" letterforms — clean version meant to pair with a separate star glyph.
  // Source: Group 427321211.svg from brand kit.
  return (
    <svg height={height} viewBox="0 0 556 132" fill="none" style={style} aria-hidden xmlns="http://www.w3.org/2000/svg">
      <path d="M190.982 34.2593H206.164V129.571H190.256L188.308 117.94C179.108 127.218 167.641 131.879 153.819 131.879C139.996 131.879 127.669 127.127 118.197 117.578C108.724 108.029 103.966 96.126 103.966 81.8247C103.966 67.5234 108.724 55.6207 118.197 46.162C127.669 36.7032 139.543 31.9512 153.819 31.9512C168.095 31.9512 179.47 36.6579 188.625 46.0714L190.936 34.2593H190.982ZM132.926 104.227C138.863 110.156 146.341 113.143 155.359 113.143C164.378 113.143 171.811 110.156 177.612 104.227C183.458 98.2984 186.359 90.8309 186.359 81.8247C186.359 72.8185 183.458 65.3963 177.612 59.5129C171.766 53.6294 164.333 50.6877 155.269 50.6877C149.377 50.6877 144.029 52.0454 139.271 54.7608C134.512 57.4763 130.75 61.1874 128.031 65.9847C125.312 70.7367 123.952 76.077 123.952 81.9605C123.952 90.9214 126.943 98.3436 132.88 104.272L132.926 104.227Z" fill={color}/>
      <path d="M218.718 129.572V0H238.342V129.572H218.718Z" fill={color}/>
      <path d="M251.213 129.573V34.2607H270.837V129.573H251.213Z" fill={color}/>
      <path d="M334.784 31.9516C349.151 31.9516 361.025 36.7037 370.406 46.1624C379.787 55.6212 384.455 67.5691 384.455 81.9157C384.455 96.2623 379.787 108.165 370.406 117.669C361.025 127.128 349.151 131.88 334.784 131.88C321.46 131.88 310.22 127.535 301.02 118.8L298.527 129.572H283.662V0H303.286V43.0849C312.123 35.6627 322.593 31.9516 334.739 31.9516H334.784ZM310.855 104.137C316.792 110.156 324.269 113.143 333.198 113.143C342.126 113.143 349.558 110.156 355.45 104.228C361.342 98.2988 364.288 90.8314 364.288 81.9157C364.288 73 361.342 65.5326 355.45 59.6039C349.558 53.6751 342.126 50.6882 333.198 50.6882C327.306 50.6882 321.958 52.0459 317.199 54.7613C312.441 57.4768 308.679 61.1879 305.96 65.9851C303.241 70.7371 301.881 76.0775 301.881 81.961C301.881 90.7861 304.872 98.2083 310.809 104.182L310.855 104.137Z" fill={color}/>
      <path d="M488.466 82.0523C488.466 83.8173 488.33 85.9897 488.104 88.5693H410.696C411.874 96.2178 415.092 102.282 420.349 106.763C425.606 111.243 432.087 113.461 439.882 113.461C443.644 113.461 447.179 112.918 450.487 111.786C453.796 110.655 456.696 109.071 459.234 107.034C461.772 104.998 463.857 102.463 465.489 99.5216H486.155C482.393 109.659 476.366 117.534 468.117 123.282C459.869 128.984 450.442 131.835 439.837 131.835C425.833 131.835 414.14 127.083 404.759 117.534C395.377 107.985 390.709 96.1273 390.709 81.8713C390.709 67.6152 395.377 55.7578 404.759 46.2085C414.14 36.6592 425.833 31.9072 439.837 31.9072C449.4 31.9072 457.875 34.1701 465.307 38.6958C472.74 43.2215 478.45 49.286 482.439 56.8892C486.427 64.4925 488.466 72.8651 488.466 82.0523ZM439.882 49.5576C432.586 49.5576 426.377 51.6394 421.21 55.7578C416.089 59.8762 412.69 65.5334 411.058 72.684H468.842C467.664 67.8415 465.715 63.7231 462.996 60.2383C460.277 56.7535 456.968 54.1286 453.025 52.3183C449.083 50.508 444.686 49.6028 439.837 49.6028L439.882 49.5576Z" fill={color}/>
      <path d="M546.794 33.7168H555.269V52.227H545.026C528.756 52.227 520.644 61.0522 520.644 78.7025V129.526H501.02V34.2599H514.254L517.607 47.3392C524.451 38.2878 534.149 33.7621 546.749 33.7621L546.794 33.7168Z" fill={color}/>
      <path d="M50.034 131.835C35.894 131.835 24.02 127.038 14.412 117.444C4.80399 107.849 0 95.9915 0 81.7808C0 67.57 4.80399 55.7578 14.412 46.2085C24.02 36.6592 35.9393 31.9072 50.2153 31.9072C61.8627 31.9072 72.0146 35.211 80.6255 41.7733C89.2364 48.3809 94.8109 57.1155 97.3941 68.0678H77.9516C75.4589 62.6369 71.7426 58.3827 66.8027 55.26C61.8627 52.1372 56.243 50.5985 49.9887 50.5985C44.4596 50.5985 39.3837 51.9562 34.8063 54.6716C30.2289 57.3871 26.6032 61.0982 23.9293 65.8955C21.3007 70.6475 19.9411 75.9878 19.9411 81.8713C19.9411 90.6964 22.8416 98.1186 28.5973 104.093C34.3531 110.112 41.5137 113.099 49.9887 113.099C56.243 113.099 61.8174 111.515 66.712 108.347C71.6067 105.179 75.323 100.653 77.9516 94.7696H97.7567C95.0375 105.948 89.3724 114.909 80.7161 121.698C72.0599 128.486 61.8174 131.835 50.034 131.835Z" fill={color}/>
      <path d="M271.199 0H250.896V20.1395H271.199V0Z" fill={color}/>
    </svg>
  );
}

/* ---------- CalLogo — full lockup: animated star + wordtext ---------- */
function CalLogo({ size = 24, mono = false, color, animate = true }) {
  const c = color || (mono ? '#fff' : '#111111');
  const [hovered, setHovered] = React.useState(false);
  return (
    <span
      onMouseEnter={animate ? () => setHovered(true) : undefined}
      onMouseLeave={animate ? () => setHovered(false) : undefined}
      style={{
        display: 'inline-flex', alignItems: 'center',
        gap: size * 0.45, lineHeight: 1, cursor: animate ? 'pointer' : 'default',
        userSelect: 'none',
      }}
    >
      <span style={{
        display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
        transform: hovered ? 'rotate(22deg) scale(1.08)' : 'rotate(0deg) scale(1)',
        transition: 'transform 0.55s cubic-bezier(.2,.8,.2,1.05)',
        transformOrigin: '50% 55%',
        filter: hovered ? 'drop-shadow(0 6px 14px rgba(0,132,255,0.45))' : 'drop-shadow(0 1px 2px rgba(0,9,182,0.15))',
      }}>
        <CalStar size={size * 1.25} grad={!mono} />
      </span>
      <CalWordText height={size * 1.05} color={c} />
    </span>
  );
}

/* ---------- Generic icon set (stroke) ---------- */
const strokeIcon = (d, { size = 18, color = 'currentColor', stroke = 1.6 } = {}) => (
  <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color} strokeWidth={stroke} strokeLinecap="round" strokeLinejoin="round" aria-hidden>
    {d}
  </svg>
);
const Icon = {
  search: (p) => strokeIcon(<><circle cx="11" cy="11" r="7"/><path d="m20 20-3.5-3.5"/></>, p),
  send:   (p) => strokeIcon(<><path d="M22 2 11 13"/><path d="M22 2l-7 20-4-9-9-4 20-7z"/></>, p),
  check:  (p) => strokeIcon(<path d="m4 12 5 5L20 6"/>, p),
  x:      (p) => strokeIcon(<><path d="M18 6 6 18"/><path d="m6 6 12 12"/></>, p),
  arrowR: (p) => strokeIcon(<><path d="M5 12h14"/><path d="m12 5 7 7-7 7"/></>, p),
  arrowD: (p) => strokeIcon(<><path d="M12 5v14"/><path d="m5 12 7 7 7-7"/></>, p),
  chevR:  (p) => strokeIcon(<path d="m9 6 6 6-6 6"/>, p),
  chevD:  (p) => strokeIcon(<path d="m6 9 6 6 6-6"/>, p),
  plus:   (p) => strokeIcon(<><path d="M12 5v14"/><path d="M5 12h14"/></>, p),
  minus:  (p) => strokeIcon(<path d="M5 12h14"/>, p),
  mic:    (p) => strokeIcon(<><rect x="9" y="3" width="6" height="12" rx="3"/><path d="M19 11a7 7 0 0 1-14 0"/><path d="M12 19v3"/></>, p),
  briefcase: (p) => strokeIcon(<><rect x="3" y="7" width="18" height="13" rx="2"/><path d="M8 7V5a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"/></>, p),
  users:  (p) => strokeIcon(<><circle cx="9" cy="8" r="4"/><path d="M2 21a7 7 0 0 1 14 0"/><circle cx="17" cy="7" r="3"/><path d="M22 20a5 5 0 0 0-7-4.5"/></>, p),
  chat:   (p) => strokeIcon(<path d="M21 12a8 8 0 0 1-11.8 7L4 20l1-4.2A8 8 0 1 1 21 12Z"/>, p),
  sparkles: (p) => strokeIcon(<><path d="M12 3v4M12 17v4M3 12h4M17 12h4M6 6l2.5 2.5M15.5 15.5 18 18M18 6l-2.5 2.5M8.5 15.5 6 18"/></>, p),
  home:   (p) => strokeIcon(<><path d="M3 11 12 3l9 8"/><path d="M5 9v12h14V9"/></>, p),
  calendar: (p) => strokeIcon(<><rect x="3" y="5" width="18" height="16" rx="2"/><path d="M3 10h18M8 3v4M16 3v4"/></>, p),
  phone:  (p) => strokeIcon(<path d="M22 16.9v3a2 2 0 0 1-2.2 2 19.8 19.8 0 0 1-8.6-3.1 19.5 19.5 0 0 1-6-6 19.8 19.8 0 0 1-3.1-8.6A2 2 0 0 1 4 2h3a2 2 0 0 1 2 1.7c.1.9.3 1.7.6 2.5a2 2 0 0 1-.4 2.1L8 9.6a16 16 0 0 0 6 6l1.3-1.3a2 2 0 0 1 2.1-.4c.8.3 1.6.5 2.5.6A2 2 0 0 1 22 16.9z"/>, p),
  lock:   (p) => strokeIcon(<><rect x="3" y="11" width="18" height="11" rx="2"/><path d="M7 11V7a5 5 0 0 1 10 0v4"/></>, p),
  zap:    (p) => strokeIcon(<path d="M13 2 3 14h9l-1 8 10-12h-9l1-8z"/>, p),
  globe:  (p) => strokeIcon(<><circle cx="12" cy="12" r="9"/><path d="M3 12h18M12 3a14 14 0 0 1 0 18M12 3a14 14 0 0 0 0 18"/></>, p),
  shield: (p) => strokeIcon(<path d="M12 3 4 6v6c0 5 3.5 8.5 8 9 4.5-.5 8-4 8-9V6l-8-3z"/>, p),
  bolt:   (p) => strokeIcon(<path d="M13 2 3 14h9l-1 8 10-12h-9l1-8z"/>, p),
  heart:  (p) => strokeIcon(<path d="M20.8 4.6a5.5 5.5 0 0 0-7.8 0L12 5.6l-1-1a5.5 5.5 0 0 0-7.8 7.8l1 1L12 21l7.8-7.6 1-1a5.5 5.5 0 0 0 0-7.8z"/>, p),
  filter: (p) => strokeIcon(<path d="M3 5h18l-7 8v6l-4-2v-4L3 5z"/>, p),
  doc:    (p) => strokeIcon(<><path d="M14 3H6a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V9l-6-6z"/><path d="M14 3v6h6M8 13h8M8 17h5"/></>, p),
  play:   (p) => strokeIcon(<path d="m6 4 14 8-14 8V4z"/>, p),
  external:(p) => strokeIcon(<><path d="M7 17 17 7"/><path d="M7 7h10v10"/></>, p),
  dot:    (p) => strokeIcon(<circle cx="12" cy="12" r="3" fill="currentColor" stroke="none"/>, p),
  pin:    (p) => strokeIcon(<><path d="M12 21s7-6 7-11a7 7 0 1 0-14 0c0 5 7 11 7 11z"/><circle cx="12" cy="10" r="2.5"/></>, p),
};

/* ---------- LinkedIn square badge (used on candidate rows) ---------- */
function LinkedInBadge({ size = 14 }) {
  return (
    <span style={{
      display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
      width: size, height: size, borderRadius: 2, background: '#0A66C2', color: '#fff',
      fontSize: size * 0.6, fontWeight: 800, letterSpacing: -0.05,
    }}>in</span>
  );
}

/* ---------- Avatar ---------- */
// deterministic hue from string
function strHue(s = '') {
  let h = 0; for (let i = 0; i < s.length; i++) h = (h * 31 + s.charCodeAt(i)) % 360;
  return h;
}
function Avatar({ name = '', size = 28, variant = 'gradient' }) {
  const initials = name.split(' ').map(n => n[0]).slice(0, 2).join('').toUpperCase();
  const h = strHue(name);
  const bg = variant === 'gradient'
    ? `linear-gradient(135deg, hsl(${h} 78% 58%), hsl(${(h+40)%360} 72% 48%))`
    : `hsl(${h} 60% 92%)`;
  const color = variant === 'gradient' ? '#fff' : `hsl(${h} 55% 30%)`;
  return (
    <span style={{
      display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
      width: size, height: size, borderRadius: '50%',
      background: bg, color, fontWeight: 600, fontSize: size * 0.38,
      flexShrink: 0, letterSpacing: 0.02,
    }}>{initials}</span>
  );
}

/* ---------- Company dot (brand squircle used in candidate rows) ---------- */
const COMPANY_PALETTE = {
  Salesforce: { bg: '#00A1E0', fg: '#fff', glyph: '☁' },
  Spotify:    { bg: '#1DB954', fg: '#fff', glyph: '♬' },
  Netflix:    { bg: '#111', fg: '#E50914', glyph: 'N' },
  Xero:       { bg: '#13B5EA', fg: '#fff', glyph: 'X' },
  Mastercard: { bg: '#FF5F00', fg: '#fff', glyph: '●' },
  Canva:      { bg: '#00C4CC', fg: '#fff', glyph: 'C' },
  Atlassian:  { bg: '#2684FF', fg: '#fff', glyph: 'A' },
  Stripe:     { bg: '#635BFF', fg: '#fff', glyph: 'S' },
  Airbnb:     { bg: '#FF5A5F', fg: '#fff', glyph: 'A' },
  Paypal:     { bg: '#003087', fg: '#fff', glyph: 'P' },
  Intercom:   { bg: '#0057FF', fg: '#fff', glyph: 'I' },
  Shopify:    { bg: '#95BF47', fg: '#fff', glyph: 'S' },
};
function CompanyDot({ name, size = 22 }) {
  const c = COMPANY_PALETTE[name] || { bg: '#E5E7EB', fg: '#6B7280', glyph: name?.[0] || '?' };
  return (
    <span style={{
      display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
      width: size, height: size, borderRadius: 6,
      background: c.bg, color: c.fg, fontSize: size * 0.52, fontWeight: 700,
      fontFamily: 'var(--cal-font)',
    }}>{c.glyph}</span>
  );
}

/* ---------- Counter (animates to target when in view) ---------- */
function useInView(ref, threshold = 0.3) {
  const [inView, set] = React.useState(false);
  React.useEffect(() => {
    const el = ref.current; if (!el) return;
    const io = new IntersectionObserver(([e]) => { if (e.isIntersecting) set(true); }, { threshold });
    io.observe(el); return () => io.disconnect();
  }, [ref, threshold]);
  return inView;
}
function Counter({ to = 100, duration = 1400, prefix = '', suffix = '', decimals = 0, format }) {
  const ref = React.useRef(null);
  const inView = useInView(ref);
  const [val, set] = React.useState(0);
  React.useEffect(() => {
    if (!inView) return;
    let raf, start;
    const tick = (ts) => {
      if (!start) start = ts;
      const p = Math.min(1, (ts - start) / duration);
      const eased = 1 - Math.pow(1 - p, 3);
      set(to * eased);
      if (p < 1) raf = requestAnimationFrame(tick);
    };
    raf = requestAnimationFrame(tick);
    return () => cancelAnimationFrame(raf);
  }, [inView, to, duration]);
  const display = format ? format(val) : `${prefix}${val.toFixed(decimals).replace(/\B(?=(\d{3})+(?!\d))/g, ',')}${suffix}`;
  return <span ref={ref}>{display}</span>;
}

/* ---------- FadeIn scroll-triggered ---------- */
function FadeIn({ children, delay = 0, y = 18, className = '', style = {} }) {
  const ref = React.useRef(null);
  const inView = useInView(ref, 0.15);
  return (
    <div ref={ref} className={className} style={{
      opacity: inView ? 1 : 0,
      transform: inView ? 'translateY(0)' : `translateY(${y}px)`,
      transition: `opacity .7s ${delay}ms cubic-bezier(.2,.7,.2,1), transform .7s ${delay}ms cubic-bezier(.2,.7,.2,1)`,
      ...style,
    }}>{children}</div>
  );
}

/* ---------- Placeholder agencies ---------- */
const PLACEHOLDER_AGENCIES = [
  'Kārearea Talent',
  'Harbour & Co.',
  'Tuatara Search',
  'Southerly Partners',
  'Mānuka Recruiting',
  'Fenix & Co.',
  'Ridgeline Talent',
  'Aurora Executive',
  'Kauri Partners',
  'Beacon Sourcing',
  'Odyssey People',
  'Silverfern Talent',
];

Object.assign(window, {
  CalStar, CalLogo, CalWordText, Icon, LinkedInBadge, Avatar, CompanyDot,
  Counter, FadeIn, useInView, PLACEHOLDER_AGENCIES,
});
