import { useState } from "react";

/**
 * Custom hook for managing dialog/modal state
 * @returns {Object} Dialog state and controls
 */
export const useDialog = () => {
  const [isOpen, setIsOpen] = useState(false);
  const [item, setItem] = useState(null);

  const open = (itemData = null) => {
    setItem(itemData);
    setIsOpen(true);
  };

  const close = () => {
    setIsOpen(false);
    setItem(null);
  };

  return {
    isOpen,
    item,
    open,
    close,
  };
};

/**
 * Custom hook for managing form state
 * @param {Object} initialState - Initial form values
 * @returns {Object} Form state and controls
 */
export const useFormState = (initialState) => {
  const [form, setForm] = useState(initialState);

  const updateField = (field, value) => {
    setForm((prev) => ({ ...prev, [field]: value }));
  };

  const updateForm = (newForm) => {
    setForm(newForm);
  };

  const resetForm = () => {
    setForm(initialState);
  };

  return {
    form,
    updateField,
    updateForm,
    resetForm,
  };
};
