/**
 * React hook for loading and saving a single raw filter file.
 */
import { useCallback } from "react";
import { fetchFilterFile, updateFilterFile } from "../api/file_config";

export interface UseFilterRawFileResult {
  fetchRawContent: () => Promise<string>;
  saveRawContent: (content: string) => Promise<void>;
}

/**
 * Return raw config file operations for a filter file.
 */
export function useFilterRawFile(name: string): UseFilterRawFileResult {
  const fetchRawContent = useCallback(async (): Promise<string> => {
    const result = await fetchFilterFile(name);
    return result.content;
  }, [name]);

  const saveRawContent = useCallback(
    async (content: string): Promise<void> => {
      await updateFilterFile(name, { content });
    },
    [name],
  );

  return {
    fetchRawContent,
    saveRawContent,
  };
}
