import { useEffect, useState } from "react";
import { fetchRuntimeConfig } from "../../../lib/api/systemApi";
import type { RuntimeConfig } from "../../../types/system";

export function useRuntimeConfig() {
  const [runtimeConfig, setRuntimeConfig] = useState<RuntimeConfig | null>(null);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState<string | null>(null);

  useEffect(() => {
    fetchRuntimeConfig()
      .then((data) => {
        setRuntimeConfig(data);
        setError(null);
      })
      .catch((exception: unknown) => {
        setError(
          exception instanceof Error
            ? exception.message
            : "Failed to fetch runtime config.",
        );
      })
      .finally(() => {
        setLoading(false);
      });
  }, []);

  return {
    runtimeConfig,
    loading,
    error,
  };
}