import React, { createContext, useContext } from 'react';

import {
  ActiveNavElement,
  CurrentOrganizationInterface,
  CurrentProjectInterface,
  MongoNavInterface,
  Product,
} from './types';

// TODO: Add additional props to context
/* e.g. {
  urls: URLS;
  hosts: Required<HostsInterface>;
  activePlatform: Platform;
  activeNav?: ActiveNavElement;
  isAdmin?: boolean;
  mode?: ProductionMode;
  environment?: Environment;
}
*/
export interface MongoNavContextProps
  extends Pick<
    MongoNavInterface,
    'useAppServicesBranding' | 'showUnifiedAccessManagementNav'
  > {
  isLoading: boolean;
  activeNav?: ActiveNavElement;
  activeProduct?: Product;
  currentOrg?: CurrentOrganizationInterface | null;
  currentProject?: CurrentProjectInterface;
}

export const defaultMongoNavContext = {
  // TODO: Rewrite tests so default isLoading: true, doesn't break tests
  isLoading: false,
  useAppServicesBranding: true,
  showUnifiedAccessManagementNav: false,
} as const;

const MongoNavContext = createContext<MongoNavContextProps>(
  defaultMongoNavContext,
);

export const MongoNavProvider = ({
  children,
  context,
}: {
  children: React.ReactNode;
  context: MongoNavContextProps;
}) => {
  return (
    <MongoNavContext.Provider value={context}>
      {children}
    </MongoNavContext.Provider>
  );
};

export const useMongoNavContext = () => {
  return useContext(MongoNavContext);
};
