import { useState, useCallback } from "react";

interface WebLLMState {
  isLoaded: boolean;
  loading: boolean;
  error: string | null;
}

export function useWebLLM() {
  const [state, setState] = useState<WebLLMState>({
    isLoaded: false,
    loading: false,
    error: null,
  });

  const isWebGPUSupported = useCallback(() => {
    if (typeof navigator === "undefined") return false;
    return "gpu" in navigator;
  }, []);

  const translate = useCallback(
    async (
      _text: string,
      _targetLang: string,
      _systemPrompt?: string,
      _glossary?: string,
    ): Promise<string> => {
      setState((s) => ({ ...s, loading: true, error: null }));
      try {
        throw new Error("WebLLM is not available in this environment");
      } catch (err) {
        const message =
          err instanceof Error ? err.message : "WebLLM translation failed";
        setState((s) => ({ ...s, loading: false, error: message }));
        throw err;
      }
    },
    [],
  );

  return {
    ...state,
    isWebGPUSupported,
    translate,
  };
}
