import { useMutation } from "@tanstack/react-query";
import { setReadOnly as setReadOnlyApi } from "@/lib/tauri";
import { useAppStore } from "@/stores/app-store";

export function useToggleReadOnly() {
  const setReadOnly = useAppStore((s) => s.setReadOnly);

  return useMutation({
    mutationFn: ({
      connectionId,
      readOnly,
    }: {
      connectionId: string;
      readOnly: boolean;
    }) => setReadOnlyApi(connectionId, readOnly),
    onSuccess: (_data, { connectionId, readOnly }) => {
      setReadOnly(connectionId, readOnly);
    },
  });
}
