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 } = 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());
    if (settings.window.start_fullscreen) fullscreen();
  }, []);

  return null;
};
