import { useState, useCallback } from 'react';

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

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

  const reset = useCallback(() => {
    setValue(initialValue);
    setIsDirty(false);
  }, [initialValue]);

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