import React, { useContext, useMemo } from "react";

import {
  InventoryManager,
  getInventoryManager,
} from "@dndbeyond/character-rules-engine/es";

interface InventoryManagerContextValue {
  inventoryManager: InventoryManager;
}
const initContext: InventoryManagerContextValue = {
  inventoryManager: null!,
};
export const InventoryManagerContext =
  React.createContext<InventoryManagerContextValue>(initContext);

export function InventoryManagerProvider({ children }) {
  const inventoryManager: InventoryManager = useMemo(
    () => getInventoryManager(),
    []
  );
  return (
    <InventoryManagerContext.Provider value={{ inventoryManager }}>
      {children}
    </InventoryManagerContext.Provider>
  );
}

export const useInventoryManager = () => {
  return useContext(InventoryManagerContext);
};
