import { createApp } from "vue";
import { createPinia } from "pinia";
import App from "./App.vue";
import router from "./router";
import "./styles/tailwind.css";
import "./styles/global.css";
import { useI18n } from "./utils/i18n";
import { initCustomTitlebar } from "./utils/titlebar";

const log = (msg: string) => {
  import("@tauri-apps/plugin-log")
    .then(({ debug }) => debug(msg))
    .catch(() => console.log(`[debug] ${msg}`));
};

document.addEventListener("contextmenu", e => {
  e.preventDefault();
});

const app = createApp(App);
const pinia = createPinia();

app.use(pinia).use(router).use(useI18n).mount("#app");

function removeSkeleton() {
  setTimeout(() => {
    const skeleton = document.getElementById("app-skeleton");
    if (skeleton) {
      skeleton.style.opacity = "0";
      setTimeout(() => skeleton.remove(), 300);
    }
  }, 100);
}

(async () => {
  log("Initializing application...");

  try {
    const { getCurrentWindow } = await import("@tauri-apps/api/window");
    const appWindow = getCurrentWindow();
    await appWindow.setFullscreen(false);
    log("Window initialized successfully");
  } catch (err) {
    log(`Failed to initialize window state: ${err}`);
  }

  initCustomTitlebar();
  removeSkeleton();
})();
