import { createDir, exists } from "@tauri-apps/api/fs";
import { useEffect } from "react";
import { useSettings } from "../../contexts/SettingsContext";
import Paths from "../../lib/path";

export const Initialization = () => {
  const { settings, settingsLoading } = useSettings();

  useEffect(() => {
    const initializePaths = async () => {
      try {
        await Paths.initialize();
      } catch (error) {
        console.error(`Failed to initialize paths: ${error}`);
      }
    };

    const createDirectories = async () => {
      const configDirectoryExists = await exists(Paths.getPath("configDirectory"));
      if (!configDirectoryExists) await createDir(Paths.getPath("configDirectory"));
    };

    const fullscreen = async () => {
      const { appWindow } = await import("@tauri-apps/api/window");
      await appWindow.setFullscreen(true);
    };

    initializePaths().then(() => createDirectories());
    // TODO: race condition here, need to wait for settings to load
    if (settings.window.start_fullscreen) fullscreen();
  }, [settingsLoading]);

  return null;
};
