import { use$ as useSelector } from "../../../_vendor/@legendapp/state/react.js";
import { useRef, useEffect } from "react";
import { modelStore$, modelActions } from "../../../stores/model-store.js";
function useModelSelection(enableModalSelection = true) {
  const availableModels = useSelector(modelStore$.models);
  const currentModel = useSelector(modelStore$.selectedModel);
  const isLoading = useSelector(modelStore$.isLoading);
  const error = useSelector(modelStore$.error);
  const hasRequestedRef = useRef(false);
  useEffect(() => {
    if (!enableModalSelection) return;
    if (hasRequestedRef.current) return;
    if (availableModels.length === 0 && !isLoading && !error) {
      hasRequestedRef.current = true;
      modelActions.fetchAvailableModels();
    }
  }, [availableModels.length, isLoading, error, enableModalSelection]);
  const selectModel = (model) => {
    modelActions.selectModel(model);
  };
  const selectModelById = (modelId) => {
    modelActions.selectModelById(modelId);
  };
  const getCurrentModelDisplayName = () => {
    if (isLoading) return "Loading...";
    if (error) return "Error";
    return currentModel?.name || "Claude 3.5 Sonnet";
  };
  const getCurrentModelId = () => {
    return currentModel?.id || "anthropic/claude-3.5-sonnet";
  };
  const getAvailableModels = () => {
    return availableModels;
  };
  const refreshModels = () => {
    hasRequestedRef.current = false;
    modelActions.fetchAvailableModels();
  };
  const clearError = () => {
    modelActions.clearError();
  };
  return {
    // 状态
    availableModels: getAvailableModels(),
    currentModel,
    isLoading,
    error,
    // 方法
    selectModel,
    selectModelById,
    getCurrentModelDisplayName,
    getCurrentModelId,
    refreshModels,
    clearError
  };
}
export {
  useModelSelection
};
//# sourceMappingURL=use-model-selection.js.map
