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

type TUpload =
  | 'featured_image'
  | 'ads'
  | 'content'
  | 'profile_picture'
  | undefined

type AdminContextProperties = {
  isUploadOpen: TUpload
  setIsUploadOpen: Dispatch<SetStateAction<TUpload>>
}

const AdminContext = createContext<AdminContextProperties | undefined>(
  undefined,
)

export const AdminProvider = ({ children }: PropsWithChildren) => {
  const [isUploadOpen, setIsUploadOpen] = useState<TUpload>()

  return (
    <AdminContext.Provider
      value={{
        isUploadOpen,
        setIsUploadOpen,
      }}
    >
      {children}
    </AdminContext.Provider>
  )
}

export const useAdminContext = (): AdminContextProperties => {
  const context = useContext(AdminContext)
  if (!context) {
    throw new Error('useAdminContext must be used within a AdminProvider')
  }
  return context
}
