File size: 3,318 Bytes
c4412d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { NextResponse } from 'next/server';

export async function GET() {
  const baseUrl = process.env.NEXT_PUBLIC_APP_URL || 'http://localhost:3000';

  // Return the actual embed.js content directly instead of loading it from a file
  const embedScript = `
    (function() {
      console.log('Initializing PlayGo Chat Widget');

      function createChatWidget(config) {
        console.log('Creating chat widget with config:', config);

        const iframe = document.createElement('iframe');

        const iframeStyles = {
          border: 'none',
          position: 'fixed',
          bottom: '0',
          right: '0',
          width: '380px',
          height: '100px',
          maxHeight: '600px',
          zIndex: '9999',
          background: 'none',
          pointerEvents: 'all',
          transition: 'height 0.5s ease-in-out',
          overflow: 'hidden',
        };

        Object.assign(iframe.style, iframeStyles);

        iframe.onload = function() {
          const style = document.createElement('style');
          style.textContent = \`
            ::-webkit-scrollbar {
              display: none !important;
            }
            * {
              -ms-overflow-style: none !important;
              scrollbar-width: none !important;
            }
          \`;
          iframe.contentDocument.head.appendChild(style);
        };

        const queryParams = new URLSearchParams(config).toString();
        const chatUrl = '${baseUrl}/embed/chat?' + queryParams;
        console.log('Chat URL:', chatUrl);

        iframe.src = chatUrl;

        window.addEventListener('message', function(event) {
          console.log('Received message:', event.data);
          if (event.data.type === 'CHAT_READY') {
            console.log('Chat widget ready');
          } else if (event.data.type === 'chatOpen') {
            iframe.style.height = '600px';
          } else if (event.data.type === 'chatClose') {
            iframe.style.height = '100px';
          }
        });

        return iframe;
      }

      window.playgo = function(action, config) {
        console.log('PlayGo action called:', action, config);

        if (action === 'init') {
          try {
            const existingWidget = document.getElementById('playgo-chat-widget');
            if (existingWidget) {
              existingWidget.remove();
            }

            const widget = createChatWidget(config);
            widget.id = 'playgo-chat-widget';
            document.body.appendChild(widget);

            console.log('PlayGo chat initialized successfully');
          } catch (error) {
            console.error('Error initializing PlayGo chat:', error);
          }
        }
      };

      if (window.PlayGoChatWidget && window.PlayGoChatWidget.q) {
        console.log('Processing queued commands:', window.PlayGoChatWidget.q);
        window.PlayGoChatWidget.q.forEach(args => {
          window.playgo.apply(null, args);
        });
      }

      if (window.PlayGoChatWidget) {
        window.PlayGoChatWidget.ready = true;
      }

      console.log('PlayGo Chat Widget initialization complete');
    })();
  `;

  return new NextResponse(embedScript, {
    headers: {
      'Content-Type': 'application/javascript',
      'Cache-Control': 'no-store, max-age=0',
    },
  });
}