import React, { createContext, useContext, useState, useCallback } from 'react';

const ProfileActionsContext = createContext(null);

export const ProfileActionsProvider = ({ children }) => {
  const [shouldOpenEditModal, setShouldOpenEditModal] = useState(false);

  const triggerEditModal = useCallback(() => {
    setShouldOpenEditModal(true);
  }, []);

  const resetEditModalTrigger = useCallback(() => {
    setShouldOpenEditModal(false);
  }, []);

  return (
    <ProfileActionsContext.Provider value={{ shouldOpenEditModal, triggerEditModal, resetEditModalTrigger }}>
      {children}
    </ProfileActionsContext.Provider>
  );
};

export const useProfileActions = () => {
  const context = useContext(ProfileActionsContext);
  if (!context) {
    // This check is important. If useProfileActions is called outside the provider,
    // it indicates a structural issue in the component tree.
    // However, in this specific case, the Header is outside the ProfilePage route,
    // so we need to handle the case where the context is null gracefully in the Header.
    // We'll return null or undefined from the hook if context is not available.
    // The components using the hook must check for null/undefined.
    // console.warn('useProfileActions must be used within a ProfileActionsProvider');
    return null; // Return null if context is not available
  }
  return context;
};
