"use strict";

(function () {
  const framexEngine = {
    init() {
      this.bindThemeToggle();
      this.bindMobileMenu();
    },

    bindThemeToggle() {
      const buttons = document.querySelectorAll("[data-theme-toggle]");
      if (!buttons.length) {
        return;
      }

      const applyTheme = (theme) => {
        const isDark = theme === "dark";
        document.documentElement.classList.toggle("dark", isDark);
        try {
          localStorage.setItem("framex-theme", theme);
        } catch (error) {}
        buttons.forEach((button) => {
          button.setAttribute("aria-pressed", String(isDark));
        });
      };

      buttons.forEach((button) => {
        button.addEventListener("click", () => {
          const nextTheme = document.documentElement.classList.contains("dark")
            ? "light"
            : "dark";
          applyTheme(nextTheme);
        });
      });
    },

    bindMobileMenu() {
      const button = document.querySelector("[data-mobile-menu-toggle]");
      const menu = document.querySelector("[data-mobile-menu]");
      if (!button || !menu) {
        return;
      }

      button.addEventListener("click", () => {
        const isOpen = button.getAttribute("aria-expanded") === "true";
        button.setAttribute("aria-expanded", String(!isOpen));
        menu.classList.toggle("hidden", isOpen);
      });
    },
  };

  // Initialize on DOM ready
  if (document.readyState === "loading") {
    document.addEventListener("DOMContentLoaded", () => framexEngine.init());
  } else {
    framexEngine.init();
  }
})();
