import { useCallback, useRef } from "react";

export type DeltaWatermarkStore = {
  getDeltaWatermark: (key: string) => string | null;
  setDeltaWatermark: (key: string, watermark: string | null | undefined) => void;
  resetDeltaWatermark: (key?: string) => void;
};

export function useDeltaWatermarks(): DeltaWatermarkStore {
  const watermarksRef = useRef<Record<string, string | null>>({});

  const getDeltaWatermark = useCallback((key: string) => watermarksRef.current[key] ?? null, []);

  const setDeltaWatermark = useCallback((key: string, watermark: string | null | undefined) => {
    watermarksRef.current[key] = watermark ?? null;
  }, []);

  const resetDeltaWatermark = useCallback((key?: string) => {
    if (key) {
      delete watermarksRef.current[key];
      return;
    }
    watermarksRef.current = {};
  }, []);

  return { getDeltaWatermark, setDeltaWatermark, resetDeltaWatermark };
}
