import React, { useContext } from "react";
import NoAccess from "../noAccess";
import { getEntityDescription } from "../../../services/entityService";
import { accessTypes } from "../../../constants/enums";
import { hasAccess } from "../../../services/permissionService";
import { UserContext } from "../../../context/user/userContext";

export const Management = ({
  brandCode,
  selectedModuleName,
  selectedEntityName,
  children,
}) => {
  const { state } = useContext(UserContext);

  const renderModule = () => {
    const privileges = state?.user?.privileges;
    const result = hasAccess(privileges, selectedEntityName, accessTypes.READ);
    const entityDisplayName = getEntityDescription(
      privileges,
      selectedEntityName
    );
    if (result === true) {
      return children;
    } else {
      return <NoAccess title={entityDisplayName}></NoAccess>;
    }
  };
  return <React.Fragment>{renderModule()}</React.Fragment>;
};


