File size: 2,549 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
(function() {
  console.log('Initializing PlayGo Chat Widget');

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

    // Create container for positioning
    const container = document.createElement('div');
    Object.assign(container.style, {
      position: 'fixed',
      bottom: '0',
      right: '0',
      zIndex: '9999',
      padding: '20px',
      transition: 'all 0.3s ease-in-out',
      background: 'transparent',
      width: '250px',
      height: '120px',
      display: 'flex',
      alignItems: 'flex-end',
      justifyContent: 'flex-end'
    });

    // Create iframe
    const iframe = document.createElement('iframe');
    Object.assign(iframe.style, {
      border: 'none',
      background: 'transparent',
      width: '180px',
      height: '60px',
      transition: 'all 0.3s ease-in-out',
      transform: 'scale(1)',
      transformOrigin: 'bottom right'
    });

    // Set source with config parameters
    const queryParams = new URLSearchParams(config).toString();
    const chatUrl = `${window.location.origin}/embed/chat?${queryParams}`;
    iframe.src = chatUrl;

    // Handle messages from the iframe
    window.addEventListener('message', function(event) {
      if (event.data.type === 'chatOpen') {
        container.style.padding = '20px';
        container.style.width = '380px';
        container.style.height = '600px';
        iframe.style.width = '100%';
        iframe.style.height = '100%';
      } else if (event.data.type === 'chatClose') {
        container.style.padding = '20px';
        container.style.width = '250px';
        container.style.height = '120px';
        iframe.style.width = '180px';
        iframe.style.height = '60px';
      }
    });

    container.appendChild(iframe);
    return container;
  }

  // Initialize the widget
  window.playgo = function(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);
      }
    }
  };

  // Process any queued commands
  if (window.PlayGoChatWidget?.q) {
    window.PlayGoChatWidget.q.forEach(args => window.playgo(...args));
  }
})();