// Plazify · Reviews landing — shared primitives + analytics helper
// Exports to window: MIcon, MBtn, Eyebrow, Section, Stars, track, goForm

/* ============================================================================
   ANALYTICS PLACEHOLDER LAYER
   TODO(analytics): wire real providers in index.html <head>:
     - Yandex Metrica (ym counter)            → TODO insert real counter id
     - Google Analytics / GTM (gtag/dataLayer) → TODO insert real GTM/GA id
   `track(event, payload)` is the single funnel. It currently logs + pushes to
   window.dataLayer and calls ym() if present. No real IDs are wired yet.
   ========================================================================== */
function track(event, payload = {}) {
  try {
    // TODO(analytics): GTM / GA dataLayer push
    (window.dataLayer = window.dataLayer || []).push({ event, ...payload });
    // TODO(analytics): Yandex Metrica reachGoal — wire a real counter id before enabling
  } catch (e) { /* noop */ }
  if (window.__PLZ_DEBUG_ANALYTICS) console.log('[track]', event, payload);
}

// Open the reusable lead modal and fire a CTA event.
// Intent is derived from the CTA source: any "*_demo" source opens the modal in
// demo/consultation mode, everything else (e.g. "*_try") opens it in trial mode.
// The single <LeadModal/> listens for the 'plz-open-lead' event (see LeadCrossFooter.jsx).
function goForm(source) {
  const intent = /_demo$/.test(String(source)) ? 'demo' : 'trial';
  track('cta_click', { source, intent });
  window.dispatchEvent(new CustomEvent('plz-open-lead', { detail: { intent, source } }));
}

const MICONS = {
  arrowRight: 'M5 12h14M13 6l6 6-6 6',
  chevronDown: 'M6 9l6 6 6-6',
  plus: 'M12 5v14M5 12h14',
  check: 'M20 6 9 17l-5-5',
  menu: 'M4 7h16M4 12h16M4 17h16',
  sliders: 'M4 21v-7M4 10V3M12 21v-9M12 8V3M20 21v-5M20 12V3M1 14h6M9 8h6M17 16h6',
  shield: 'M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z',
  chart: 'M3 3v18h18M7 16v-5M12 16V8M17 16v-9',
  message: 'M21 11.5a8.38 8.38 0 0 1-.9 3.8 8.5 8.5 0 0 1-7.6 4.7 8.38 8.38 0 0 1-3.8-.9L3 21l1.9-5.7a8.38 8.38 0 0 1-.9-3.8 8.5 8.5 0 0 1 4.7-7.6 8.38 8.38 0 0 1 3.8-.9h.5a8.48 8.48 0 0 1 8 8v.5z',
  lock: 'M5 11h14v10H5zM8 11V7a4 4 0 0 1 8 0v4',
  tag: 'M20.59 13.41 13.42 20.6a2 2 0 0 1-2.83 0L2 12V2h10l8.59 8.59a2 2 0 0 1 0 2.82z M7 7h.01',
  layers: 'M12 2 2 7l10 5 10-5-10-5zM2 17l10 5 10-5M2 12l10 5 10-5',
};
function MIcon({ name, size = 20, color = 'currentColor', stroke = 2 }) {
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color}
      strokeWidth={stroke} strokeLinecap="round" strokeLinejoin="round" style={{ display: 'block', flex: '0 0 auto' }}>
      {MICONS[name].split('M').filter(Boolean).map((seg, i) => <path key={i} d={'M' + seg} />)}
    </svg>
  );
}

function MBtn({ variant = 'primary', children, icon, onClick, big, type = 'button', full, disabled }) {
  const v = {
    primary:   { background: '#5E3EE5', color: '#fff', border: 'none' },
    dark:      { background: '#fff', color: '#202023', border: 'none', boxShadow: 'var(--plz-shadow-soft)' },
    ghostDark: { background: 'rgba(255,255,255,.10)', color: '#fff', border: '1px solid rgba(255,255,255,.20)' },
    outline:   { background: '#fff', color: '#202023', border: '1px solid #D5D7DB' },
  }[variant];
  const dark = variant === 'primary' || variant === 'ghostDark';
  return (
    <button type={type} onClick={onClick} disabled={disabled} className="plz-btn" style={{
      display: 'inline-flex', alignItems: 'center', justifyContent: 'center', gap: 10, cursor: disabled ? 'default' : 'pointer', borderRadius: 12,
      padding: big ? '17px 28px' : '13px 22px', fontFamily: 'var(--plz-font-head)', fontWeight: 700,
      fontSize: big ? 17 : 15, whiteSpace: 'nowrap', width: full ? '100%' : undefined, opacity: disabled ? 0.6 : 1, ...v,
    }}>{children}{icon && <MIcon name={icon} size={big ? 20 : 18} color={dark ? '#fff' : '#202023'} />}</button>
  );
}

function Eyebrow({ children, dark }) {
  return (
    <span style={{
      display: 'inline-flex', alignItems: 'center', gap: 8,
      background: dark ? 'rgba(127,133,247,.16)' : '#E0E4FF',
      color: dark ? '#B8BCFF' : '#5E3EE5',
      borderRadius: 100, padding: '8px 16px', fontFamily: 'var(--plz-font-head)', fontWeight: 600, fontSize: 14,
    }}>{children}</span>
  );
}

function Section({ children, style, max = 1280, id }) {
  return (
    <section id={id} style={{ padding: '0 40px', ...style }}>
      <div style={{ maxWidth: max, margin: '0 auto' }}>{children}</div>
    </section>
  );
}

function Stars({ n = 5, size = 14, color = '#F5B53D' }) {
  return (
    <span style={{ display: 'inline-flex', gap: 2, alignItems: 'center' }} aria-label={n + ' звёзд'}>
      {Array.from({ length: 5 }).map((_, i) => (
        <svg key={i} width={size} height={size} viewBox="0 0 24 24" fill={i < n ? color : '#E4E6EB'} style={{ display: 'block' }}>
          <path d="M12 2l3.09 6.26L22 9.27l-5 4.87 1.18 6.88L12 17.77l-6.18 3.25L7 14.14l-5-4.87 6.91-1.01L12 2z" />
        </svg>
      ))}
    </span>
  );
}

Object.assign(window, { MIcon, MBtn, Eyebrow, Section, Stars, track, goForm });
