import React from "react";

export interface AppState {
  openSidebar: boolean;
}

export interface IAppContextType {
  appState: AppState;
  updateOpenSidebar: (b: boolean) => void;
  getOpenSidebar: () => boolean;
}

const defaultValue: IAppContextType = {
  appState: {
    openSidebar: true,
  },
  updateOpenSidebar: () => false,
  getOpenSidebar: () => false,
};

export const AppStateContext = React.createContext<IAppContextType>(
  defaultValue as IAppContextType,
);

const AppStateProvider: React.FC<React.ReactNode> = ({ children }) => {
  const [appState, setAppState] = React.useState<AppState>({} as AppState);

  const updateOpenSidebar = (b: boolean) => {
    setAppState({ ...appState, openSidebar: b });
  };

  const getOpenSidebar = () => {
    return appState.openSidebar;
  };

  return (
    <AppStateContext.Provider
      value={{ appState, updateOpenSidebar, getOpenSidebar }}
    >
      {children}
    </AppStateContext.Provider>
  );
};

export default AppStateProvider;
