document.addEventListener('DOMContentLoaded', function() {

  // Create and style the widget
  var widget = document.createElement('div');
  widget.id = 'myExtensionWidget';
  widget.style.backgroundImage = `url(`+widgetData.Blue_purple_colorful_smart_brain_logo+`)`;
  widget.style.backgroundSize = 'cover';
  widget.style.cursor = 'pointer';

  // Create an iframe for the chat widget
  var iframe = document.createElement('iframe');
  iframe.style.display = 'none';
  iframe.style.width = '400px';
  iframe.style.height = '600px';
  iframe.style.position = 'fixed';
  iframe.style.bottom = '70px';
  iframe.style.right = '10px';
  iframe.style.border = 'none';
  iframe.style.boxShadow = '0 0 10px rgba(0,0,0,0.5)';
  iframe.style.borderRadius = '10px';
  iframe.style.zIndex = '9999999';

  // Append widget and iframe to the body
  document.body.appendChild(widget);

  iframe.onload = function() {
    var iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
    // Dynamically inject JS
    var scriptElement_hoy3lrg_js = iframeDocument.createElement("script");
    scriptElement_hoy3lrg_js.src = widgetData.hoy3lrg_js; // Assuming this is a correct URL
    iframeDocument.head.appendChild(scriptElement_hoy3lrg_js);

    var scriptElement_jquer_min_js = iframeDocument.createElement("script");
    scriptElement_jquer_min_js.src = widgetData.jquer_min_js; // Assuming this is a correct URL
    iframeDocument.body.appendChild(scriptElement_jquer_min_js);

    setTimeout(function() {
      var scriptElement_chat_widget_js = iframeDocument.createElement("script");
      scriptElement_chat_widget_js.src = widgetData.chat_widget_js; // Assuming this is a correct URL
      iframeDocument.body.appendChild(scriptElement_chat_widget_js);
    }, 2000);

    var styleLink_reset_css = iframeDocument.createElement("link");
    styleLink_reset_css.rel = "stylesheet";
    styleLink_reset_css.href = widgetData.reset_css; // Assuming this is a correct URL
    iframeDocument.head.appendChild(styleLink_reset_css);

    var styleLink_fontawesome_css = iframeDocument.createElement("link");
    styleLink_fontawesome_css.rel = "stylesheet";
    styleLink_fontawesome_css.href = widgetData.fontawesome_css; // Assuming this is a correct URL
    iframeDocument.head.appendChild(styleLink_fontawesome_css);

    var styleLink_chat_widget_css = iframeDocument.createElement("link");
    styleLink_chat_widget_css.rel = "stylesheet";
    styleLink_chat_widget_css.href = widgetData.chat_widget_css; // Assuming this is a correct URL
    iframeDocument.head.appendChild(styleLink_chat_widget_css);

    var img = iframeDocument.createElement('img');
    img.setAttribute('id', 'chat-image');
    img.setAttribute('src', widgetData.Blue_purple_colorful_smart_brain_logo);
    img.setAttribute('hidden',true);
    iframeDocument.body.appendChild(img);
  };

  document.body.appendChild(iframe);

  iframe.src = widgetData.chatWidgetURL + "?apiKey=" + encodeURIComponent(widgetData.apiKey);

  // Toggle iframe on widget click
  widget.addEventListener('click', function() {
      if (iframe.style.display === 'none') {
          iframe.style.display = 'block';
      } else {
          iframe.style.display = 'none';
      }
  });

}, false);
