import React, {
  createContext,
  useState,
  useContext,
  type PropsWithChildren,
} from 'react'

type NewsContextProperties = {
  isLoginOpen: boolean
  setIsLoginOpen: React.Dispatch<React.SetStateAction<boolean>>
  isRegisterOpen: boolean
  setIsRegisterOpen: React.Dispatch<React.SetStateAction<boolean>>
  isForgetOpen: boolean
  setForgetOpen: React.Dispatch<React.SetStateAction<boolean>>
}

const NewsContext = createContext<NewsContextProperties | undefined>(undefined)

export const NewsProvider = ({ children }: PropsWithChildren) => {
  const [isLoginOpen, setIsLoginOpen] = useState(false)
  const [isRegisterOpen, setIsRegisterOpen] = useState(false)
  const [isForgetOpen, setForgetOpen] = useState(false)

  return (
    <NewsContext.Provider
      value={{
        isLoginOpen,
        setIsLoginOpen,
        isRegisterOpen,
        setIsRegisterOpen,
        isForgetOpen,
        setForgetOpen,
      }}
    >
      {children}
    </NewsContext.Provider>
  )
}

export const useNewsContext = (): NewsContextProperties => {
  const context = useContext(NewsContext)
  if (!context) {
    throw new Error('useNewsContext must be used within a NewsProvider')
  }
  return context
}
