// Maple Pavers — Quote Launcher (full-page navigation, replaces the modal).
// Listens for the global `mp-quote` event (already dispatched by MP.startQuote
// on every CTA) and navigates the page to the Quotebox /demo with Maple brand
// params + a return_to URL so the demo page renders a "Back to homepage" link.
(function () {
  const MP = window.MP;

  // Same-origin path in production — vercel.json rewrites /quote -> the
  // Quotebox /demo route so visitors stay on this brand's domain. For local
  // dev, fall through to a Quotebox dev server on :3000.
  function defaultDemoBase() {
    const h = window.location.hostname;
    if (h === 'localhost' || h === '127.0.0.1') return 'http://localhost:3000/demo';
    return '/quote';
  }
  MP.demoBaseUrl = MP.demoBaseUrl || defaultDemoBase();

  // Maple brand params handed to the Quotebox demo via URL.
  // The Maple mark lives in Quotebox's public folder at /brands/maple/mark.png
  // (copied from maple-pavers/designs/website-v1/assets/mark-solid.png) so the
  // demo serves it from its own origin — every avatar slot (header card,
  // BotAvatar in chat bubbles, GlowLogo on the loading screen) picks it up
  // via the BrandContext.
  const MAPLE_PARAMS = {
    contractor: 'Maple Pavers',
    tagline: 'Premium pavers, priced in 90 seconds.',
    primary: '#b04f24',
    font_display: 'Cormorant Garamond',
    logo: '/brands/maple/mark.png',
    // Reversed mark (cream M on terracotta tile) used in the demo's header
    // card. Chat-bubble avatars + the loading screen stick with the regular
    // mark above for the lighter terracotta-on-white look.
    logo_dark: '/brands/maple/mark-dark.png',
  };

  function buildDemoUrl() {
    const params = { ...MAPLE_PARAMS, return_to: window.location.href };
    const qs = new URLSearchParams(params).toString();
    const base = MP.demoBaseUrl;
    return base + (base.includes('?') ? '&' : '?') + qs;
  }

  // No React component to render — this is a navigation handler. We expose a
  // stub QuoteModal that returns null so app-site.jsx's existing reference
  // still works without errors.
  function QuoteModal() {
    React.useEffect(() => {
      const onLaunch = () => { window.location.href = buildDemoUrl(); };
      window.addEventListener('mp-quote', onLaunch);
      return () => window.removeEventListener('mp-quote', onLaunch);
    }, []);
    return null;
  }

  MP.QuoteModal = QuoteModal;
})();
