import { Corporate } from '@/@types/corporates';
import axios from '@/utils/axios';
import { ReactNode, createContext, useState, useEffect } from 'react';
import { useParams } from 'react-router';
// hooks
import useResponsive from '../hooks/useResponsive';

// ----------------------------------------------------------------------

export type ConfiguredCorporateContextProps = {
  currentCorporate?: Corporate | null;
};

const initialState: ConfiguredCorporateContextProps = {
  currentCorporate: null,
};

const ConfiguredCorporateContext = createContext(initialState);

type ConfiguredCorporateProviderProps = {
  children: ReactNode;
};

function ConfiguredCorporateProvider({ children }: ConfiguredCorporateProviderProps) {
  const { corporate_id } = useParams();
  const [corporate, setCorporate] = useState(null);

  useEffect(() => {
    axios.get(`corporates/${corporate_id}`)
    .then((res) => {
      setCorporate(res.data)
    })
  }, []);

  return (
    <ConfiguredCorporateContext.Provider
      value={{
        currentCorporate: corporate,
      }}
    >
      {children}
    </ConfiguredCorporateContext.Provider>
  );
}

export { ConfiguredCorporateProvider, ConfiguredCorporateContext };
