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

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

/**
 * Return raw config file operations for an action file.
 */
export function useActionRawFile(name: string): UseActionRawFileResult {
  const fetchRawContent = useCallback(async (): Promise<string> => {
    const result = await fetchActionFile(name);
    return result.content;
  }, [name]);

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

  return {
    fetchRawContent,
    saveRawContent,
  };
}
