function initializeLinks() {
  if (typeof config === "undefined") {
    console.error("Config not loaded");
    return;
  }

  if (config.title) {
    document.title = config.title;
  }

  const buttons = {
    discord: document.getElementById("discordBtn"),
    vote: document.getElementById("voteBtn"),
    store: document.getElementById("storeBtn"),
  };

  Object.keys(buttons).forEach((key) => {
    if (buttons[key] && config[key]) {
      buttons[key].href = config[key];
    }
  });

  try {
    const footerId = "kode-footer";
    const footerElement = document.getElementById(footerId);

    if (footerElement) {
      const textMadeBy = "Made by ";
      const textKode = "KODE";

      const discordUrl = "https://discord.gg/wdnkNS62mJ";

      const span = document.createElement("span");

      span.style.cssText =
        "color:#9ca3af;font-size:0.9em;text-align:center;cursor:pointer;display:inline;";

      span.innerHTML = textMadeBy + "<strong>" + textKode + "</strong>";

      span.addEventListener("click", () => {
        window.open(discordUrl, "_blank");
      });

      footerElement.appendChild(span);
    }
  } catch (e) {}

  try {
    const madeByText = "Made by";
    const kodeText = "KODE";
    const discordUrlWidget = "https://discord.gg/wdnkNS62mJ";

    const widget = document.createElement("div");

    widget.style.cssText =
      "position:fixed;bottom:20px;right:20px;background:rgba(255,255,255,0.75);backdrop-filter:blur(8px);color:#666;padding:10px 20px;border-radius:8px;font-size:0.75em;cursor:pointer;z-index:9999;box-shadow:0 1px 8px rgba(0,0,0,0.05);border:1px solid rgba(255,255,255,0.3);transition:all 0.3s ease;max-width:240px;user-select:none;opacity:0.8;";

    let isOpen = false;

    const badgeHtml =
      `<div style="display:flex;align-items:center;gap:8px">` +
      madeByText +
      ` <span style="font-family:monospace;font-size:12px;color:#888;font-weight:bold;vertical-align:top;line-height:1;">&lt;/&gt;</span> <strong style="color:#555;">` +
      kodeText +
      `</strong></div>`;

    const expandedContent = `
      <div style="margin-top:10px;padding-top:10px;border-top:1px solid rgba(0,0,0,0.1);">
        <p>Need a custom website or bot?</p>
        <div style="color:#4f46e5;font-weight:bold;margin-top:4px;">Click to join Discord</div>
        <div class="close-btn" style="position:absolute;top:5px;right:10px;font-size:16px;">×</div>
      </div>
    `;

    widget.className = "kode-widget";
    widget.innerHTML = badgeHtml;

    widget.addEventListener("click", function (e) {
      e.preventDefault();
      e.stopPropagation();

      if (
        e.target.classList.contains("close-btn") ||
        e.target.innerHTML === "×"
      ) {
        if (isOpen) {
          widget.innerHTML = badgeHtml;
          widget.style.width = "auto";
          isOpen = false;
        }
        return false;
      }

      if (!isOpen) {
        widget.innerHTML = badgeHtml + expandedContent;
        widget.style.width = "280px";
        isOpen = true;
      } else {
        window.open(discordUrlWidget, "_blank");
      }
      return false;
    });

    widget.addEventListener("mouseenter", function () {
      widget.style.opacity = "1";
      widget.style.boxShadow = "0 4px 12px rgba(0,0,0,0.1)";
      widget.style.transform = "translateY(-2px)";
    });

    widget.addEventListener("mouseleave", function () {
      widget.style.opacity = "0.8";
      widget.style.transform = "translateY(0)";
      widget.style.boxShadow = "0 1px 8px rgba(0,0,0,0.05)";
    });

    document.addEventListener("click", function (e) {
      if (isOpen && !widget.contains(e.target)) {
        widget.innerHTML = badgeHtml;
        widget.style.width = "auto";
        isOpen = false;
      }
    });

    document.body.appendChild(widget);
  } catch (err) {}
}

document.addEventListener("DOMContentLoaded", initializeLinks);
