import {
  createContext,
  Dispatch,
  SetStateAction,
  useContext,
  useState,
} from 'react';

export type PostContextValue = {
  isPosting: boolean;
  setIsPosting: Dispatch<SetStateAction<boolean>>;
  isGroupsPosting: boolean;
  setIsGroupsPosting: Dispatch<SetStateAction<boolean>>;
};

const PostContext = createContext<PostContextValue>({
  isPosting: false,
  setIsPosting: () => null,
  isGroupsPosting: false,
  setIsGroupsPosting: () => null,
});

export const PostProvider = ({ children }) => {
  const [isPosting, setIsPosting] = useState(false);
  const [isGroupsPosting, setIsGroupsPosting] = useState(false);

  return (
    <PostContext.Provider
      value={{
        isPosting,
        setIsPosting,
        isGroupsPosting,
        setIsGroupsPosting,
      }}
    >
      {children}
    </PostContext.Provider>
  );
};

export const usePosts = () => useContext(PostContext);
