(function () {

  function initWidget(el) {
    const width = el.dataset.width || "100%";
    const height = el.dataset.height || "600px";
    const redirect = el.dataset.redirect;
    const title = el.dataset.title || "Welcome";

    el.style.width = width;
    el.style.height = height;
    el.style.border = "1px solid #ccc";
    el.style.borderRadius = "8px";
    el.style.display = "flex";
    el.style.flexDirection = "column";
    el.style.justifyContent = "center";
    el.style.alignItems = "center";
    el.style.fontFamily = "Arial, sans-serif";
    el.style.background = "#fafafa";

    el.innerHTML = `
      <h2 style="margin-bottom:20px">${title}</h2>
      <p style="margin-bottom:30px">Click continue to proceed</p>
      <button class="my-widget-btn"
              style="padding:12px 24px;font-size:16px;cursor:pointer;">
        Continue
      </button>
    `;

    el.querySelector(".my-widget-btn").addEventListener("click", () => {
      if (!redirect) {
        alert("No redirect URL configured.");
        return;
      }
      window.location.href = redirect;
    });
  }

  function boot() {
    document.querySelectorAll(".my-widget").forEach(initWidget);
  }

  if (document.readyState === "loading") {
    document.addEventListener("DOMContentLoaded", boot);
  } else {
    boot();
  }

})();
