import React, { createContext, PropsWithChildren } from 'react';
import { IFirm } from "../interfaces";
import { useParams } from "react-router";

type FirmContextType = {
  currentFirm: IFirm,
}

export const FirmContext = createContext<FirmContextType>(
  {} as FirmContextType
);


export const FirmContextProvider: React.FC<PropsWithChildren> = ({ children }: PropsWithChildren) => {
  const { instance, firm } = useParams<IFirm>()

  if (instance === undefined || firm === undefined) {
    return "Error"
  }
  return (
    <FirmContext.Provider value={{currentFirm: {instance, firm}}} >
      { children }
    </FirmContext.Provider>
  );
}
