import React, { useContext } from "react";
import {
  getAccessibleEntities,
  getEntityDescription,
  getEntityLabelHeading,
  getEntityUrl,
} from "../../../services/entityService";
import { hasAccess } from "../../../services/permissionService";
import {
  entityNames,
  accessTypes,
  moduleNames,
} from "../../../constants/enums";
import { UserContext } from "../../../context/user/userContext";

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


  
  const getEntityNames = () => {
    return getAccessibleEntities(state.user.privileges, selectedModuleName);
  };

  let applicableEntityNames = getEntityNames();

  const getEntityLink = (value, index) => {
    const privileges = state?.user?.privileges;
    let hasEntityAccess = hasAccess(privileges, value, accessTypes.READ);
    let description = getEntityDescription(privileges, value);
    switch (selectedModuleName) {
      case moduleNames.Locations: {
        break;
      }
      case moduleNames.RevenueManagement: {
        description = "Manage " + description; //TODO
        break;
      }
      case moduleNames.ManageRolesAndUsersModule:
      default: {
        description = "Manage " + description;
        break;
      }
    }

    let url = getEntityUrl(value);
    let className = "nav-link submenu";
    if (selectedEntityName === value) {
      className += " w--current";
    }

    if (hasEntityAccess) {
      return (
        <a key={index} className={className} href={url}>
          {description}
        </a>
      );
    }
    return <React.Fragment></React.Fragment>;
  };

  return (
    <>
      <div className="sub-header">
        <div className="container menu">
          <div className="subheader-mainlink">{getEntityLabelHeading(selectedEntityName)}</div>
          <div className="menu-link-wrapper">
            {applicableEntityNames.length > 0 &&
              applicableEntityNames.map((value, index) => {
                return getEntityLink(value, index);
              })}
          </div>
        </div>
      </div>
    </>
  );
};
