// SannaTrip — Concierge pill (floating WhatsApp contact)
// A premium concierge touchpoint in SannaTrip's palette — deliberately NOT the
// stock green WhatsApp bubble. Navy ground, ivory text, pink accent on hover.
// The WhatsApp glyph is monochrome (currentColor) so the channel is recognizable
// without borrowing the green discount-widget branding.

const CONCIERGE_WA_NUMBER = '17864310881'; // +1 786 431 0881

// Monochrome WhatsApp glyph — inherits the surrounding ink via currentColor.
function WhatsAppGlyph({ size = 18 }) {
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="currentColor" aria-hidden="true" style={{ display: 'block' }}>
      <path d="M17.472 14.382c-.297-.149-1.758-.867-2.03-.967-.273-.099-.471-.148-.67.15-.197.297-.767.966-.94 1.164-.173.199-.347.223-.644.075-.297-.149-1.255-.463-2.39-1.475-.883-.788-1.48-1.761-1.653-2.059-.173-.297-.018-.458.13-.606.134-.133.298-.347.446-.52.149-.174.198-.298.298-.497.099-.198.05-.371-.025-.52-.075-.149-.669-1.612-.916-2.207-.242-.579-.487-.5-.669-.51-.173-.008-.371-.01-.57-.01-.198 0-.52.074-.792.372-.272.297-1.04 1.016-1.04 2.479 0 1.462 1.065 2.875 1.213 3.074.149.198 2.096 3.2 5.077 4.487.71.306 1.263.489 1.694.625.712.227 1.36.195 1.872.118.571-.085 1.758-.719 2.006-1.413.247-.694.247-1.289.173-1.413-.074-.124-.272-.198-.57-.347m-5.421 7.403h-.004a9.87 9.87 0 01-5.031-1.378l-.361-.214-3.741.982.998-3.648-.235-.374a9.86 9.86 0 01-1.51-5.26c.001-5.45 4.436-9.884 9.888-9.884 2.64 0 5.122 1.03 6.988 2.898a9.825 9.825 0 012.893 6.994c-.003 5.45-4.437 9.885-9.885 9.885M20.52 3.449C18.24 1.245 15.24 0 12.045 0 5.463 0 .104 5.334.101 11.892c0 2.096.549 4.142 1.595 5.945L0 24l6.305-1.654a11.882 11.882 0 005.683 1.448h.005c6.582 0 11.94-5.335 11.943-11.893a11.821 11.821 0 00-3.416-8.452z" />
    </svg>
  );
}

function ConciergePill({ lang }) {
  const copy = COPY[lang] || COPY.en;
  const c = copy.concierge;

  const [mounted, setMounted] = React.useState(false); // gentle entrance gate
  const [hover, setHover]     = React.useState(false);
  const [atForm, setAtForm]   = React.useState(false);  // Apply section on screen

  // Subtle delayed entrance (fade + scale-in), no loud bounce.
  React.useEffect(() => {
    const t = setTimeout(() => setMounted(true), 350);
    return () => clearTimeout(t);
  }, []);

  // Politely retreat while the Apply form is visible so the pill can never
  // cover its bottom-right submit CTA on any viewport.
  React.useEffect(() => {
    const el = document.getElementById('apply');
    if (!el || typeof IntersectionObserver === 'undefined') return;
    const io = new IntersectionObserver(
      ([entry]) => setAtForm(entry.isIntersecting),
      { rootMargin: '0px 0px -35% 0px', threshold: 0 }
    );
    io.observe(el);
    return () => io.disconnect();
  }, []);

  const open = () => {
    const url = `https://wa.me/${CONCIERGE_WA_NUMBER}?text=${encodeURIComponent(c.waMessage)}`;
    window.open(url, '_blank', 'noopener,noreferrer');
  };

  const visible = mounted && !atForm;

  return (
    <button
      type="button"
      onClick={open}
      onMouseEnter={() => setHover(true)}
      onMouseLeave={() => setHover(false)}
      aria-label={c.aria}
      title={c.label}
      style={{
        position: 'fixed',
        right: 'clamp(16px, 3vw, 28px)',
        bottom: 'clamp(16px, 3vw, 28px)',
        zIndex: 60,
        display: 'inline-flex',
        alignItems: 'center',
        gap: 9,
        padding: 'clamp(11px, 1.4vw, 13px) clamp(15px, 1.8vw, 20px)',
        background: hover ? TOKENS.color.navyDeep : TOKENS.color.navy,
        color: TOKENS.color.ivory,
        border: `1px solid ${hover ? TOKENS.color.pink : 'transparent'}`,
        borderRadius: TOKENS.r.pill,
        fontFamily: TOKENS.font.sans,
        fontSize: 'clamp(13px, 1.4vw, 14px)',
        fontWeight: 500,
        letterSpacing: TOKENS.track.tightUI,
        cursor: 'pointer',
        boxShadow: hover
          ? '0 18px 44px -18px rgba(27,54,97,0.55)'
          : '0 12px 32px -16px rgba(27,54,97,0.45)',
        opacity: visible ? 1 : 0,
        transform: visible ? 'translateY(0) scale(1)' : 'translateY(12px) scale(0.92)',
        pointerEvents: visible ? 'auto' : 'none',
        transition: 'opacity 420ms ease, transform 420ms cubic-bezier(0.22,1,0.36,1), background-color 200ms ease, border-color 200ms ease, box-shadow 200ms ease',
        WebkitTapHighlightColor: 'transparent',
      }}
    >
      <span style={{
        display: 'inline-flex',
        color: hover ? TOKENS.color.pink : TOKENS.color.ivory,
        transition: 'color 200ms ease',
      }}>
        <WhatsAppGlyph size={18} />
      </span>
      <span>{c.label}</span>
    </button>
  );
}

Object.assign(window, { ConciergePill });
