import { useState, useCallback } from "react";

interface UseFormStateOptions<T> {
  initialData: T;
  onReset?: () => void;
}

export function useFormState<T>({ initialData, onReset }: UseFormStateOptions<T>) {
  const [formData, setFormData] = useState<T>(initialData);
  const [isEditing, setIsEditing] = useState(false);
  const [editingId, setEditingId] = useState<number | null>(null);

  const startEdit = useCallback((item: T & { id?: number }) => {
    setFormData(item);
    setEditingId(item.id ?? null);
    setIsEditing(true);
  }, []);

  const resetForm = useCallback(() => {
    setFormData(initialData);
    setEditingId(null);
    setIsEditing(false);
    onReset?.();
  }, [initialData, onReset]);

  const updateFormData = useCallback((updates: Partial<T>) => {
    setFormData((prev) => ({ ...prev, ...updates }));
  }, []);

  return {
    formData,
    setFormData,
    isEditing,
    editingId,
    startEdit,
    resetForm,
    updateFormData,
  };
}

