/**
 * React hook for loading and updating a single parsed action config.
 */

import { useCallback } from "react";
import { useConfigItem } from "./useConfigItem";
import { fetchAction, updateAction } from "../api/config";
import type { ActionConfig, ActionConfigUpdate } from "../types/config";

export interface UseActionConfigResult {
  config: ActionConfig | null;
  loading: boolean;
  error: string | null;
  saving: boolean;
  saveError: string | null;
  refresh: () => void;
  save: (update: ActionConfigUpdate) => Promise<void>;
}

/**
 * Load one action config by name and expose a ``save`` callback for partial
 * updates.
 *
 * @param name - Action base name (e.g. ``"iptables"``).
 */
export function useActionConfig(name: string): UseActionConfigResult {
  const fetchFn = useCallback(
    (signal: AbortSignal) => fetchAction(name, signal),
    [name],
  );
  const saveFn = useCallback(
    (update: ActionConfigUpdate) => updateAction(name, update),
    [name],
  );

  const { data, loading, error, saving, saveError, refresh, save } = useConfigItem<
    ActionConfig,
    ActionConfigUpdate
  >({
    fetchFn,
    saveFn,
    mergeOnSave: (prev, update) =>
      prev
        ? {
            ...prev,
            ...Object.fromEntries(Object.entries(update).filter(([, v]) => v != null)),
          }
        : prev,
  });

  return {
    config: data,
    loading,
    error,
    saving,
    saveError,
    refresh,
    save,
  };
}
