import { useState, useCallback } from 'react';

export function useDirtyState<T>(initialValue: T) {
  const [value, setValue] = useState<T>(initialValue);
  const [isDirty, setIsDirty] = useState(false);

  const setValueDirty = useCallback((newValue: T | ((prev: T) => T)) => {
    setValue(newValue);
    setIsDirty(true);
  }, []);

  const reset = useCallback((newValue?: T) => {
    if (newValue !== undefined) {
      setValue(newValue);
    }
    setIsDirty(false);
  }, []);

  return { value, setValue: setValueDirty, isDirty, reset };
}
