import { useCallback, useRef } from "react";
import { useConfigStore } from "../stores/config-store";

export function useDebouncedSave(delayMs = 500, showToast = false) {
  const timer = useRef<ReturnType<typeof setTimeout> | null>(null);
  const saveConfig = useConfigStore((s) => s.saveConfig);

  const debouncedSave = useCallback(() => {
    if (timer.current) clearTimeout(timer.current);
    timer.current = setTimeout(() => saveConfig(showToast), delayMs);
  }, [saveConfig, delayMs, showToast]);

  const flush = useCallback(() => {
    if (timer.current) clearTimeout(timer.current);
    saveConfig(showToast);
  }, [saveConfig, showToast]);

  return { debouncedSave, flush };
}
