import { endPoints, moduleNames } from "../constants/enums";
import { entityNames, TriStateValue } from "../constants/enums";
import { getUrl } from "./wrapperService";
const baseUrl = process.env.REACT_APP_BASE_URL;
/*
 current module name Admin
   current entity name Roles
    entity Name Users
      localUrl + entityUrl

    entity Name Roles
      baseUrl + entityUrl
      
    current entity name Users
       entity Name Roles
  
    current entity Name Dashboard
*/
export const getEntityUrl = (entityName, selectedModuleName, selectedEntityName) => {
  let entityUrl = undefined;
  //let localUrl = getUrl("/manage");
  let isReactUrl = true;
  let completeUrl = "";
  switch (entityName) {
    case entityNames.DASHBOARD: {
      entityUrl = endPoints.CALL_BACK_DASHBOARD_URL;
      isReactUrl = false;
      break;
    }
    case entityNames.OASTATUS: {
      entityUrl = endPoints.CALL_BACK_OASTATUS_URL;
      isReactUrl = false;
      break;
    }
    case entityNames.OAMENUSTATUS: {
      entityUrl = endPoints.CALL_BACK_OAMENUSTATUS_URL;
      isReactUrl = false;
      break;
    }
    case entityNames.OAREFUNDS: {
      entityUrl = endPoints.CALL_BACK_OAREFUNDS_URL;
      isReactUrl = false;
      break;
    }
    case entityNames.PRICINGGROUP: {
      entityUrl = endPoints.CALL_BACK_BRIDGE_PRICINGGROUPS_URL;
      break;
    }
    case entityNames.MYRESTAURANTS: {
      entityUrl = endPoints.CALL_BACK_BRIDGE_MYRESTAURANTS_URL;
      break;
    }
    case entityNames.PRICEENTRY: {
      entityUrl = endPoints.CALL_BACK_BRIDGE_PRICEENTRY_URL;
      break;
    }
    case entityNames.SMARTSELLDMALEADERBOARD: {
      entityUrl = endPoints.CALL_BACK_SMARTSELLDMALEADERBOARD_URL;
      break;
    }
    case entityNames.SMARTSELLENTITYLEADERBOARD: {
      entityUrl = endPoints.CALL_BACK_SMARTSELLENTITYLEADERBOARD_URL;
      break;
    }
    case entityNames.ROLEMANAGEMENT: {
      entityUrl = endPoints.CALL_BACK_BRIDGE_ROLES_URL;
      break;
    }
    case entityNames.USERMANAGEMENT: {
      entityUrl = endPoints.CALL_BACK_BRIDGE_USERS_URL;
      break;
    }
    case "Groups":
    case entityNames.GROUPMANAGEMENT: {
      entityUrl = endPoints.CALL_BACK_BRIDGE_GROUPS_URL;
      break;
    }
    case entityNames.HOURSOPERATION: {
      entityUrl = endPoints.CALL_BACK_BRIDGE_HOOPS_URL;
      break;
    }

    default: {
      break;
    }
  }

  completeUrl = baseUrl + entityUrl;

  return completeUrl;
};

export const getModuleEntities = (privileges, mName) => {
  let values = privileges.filter(
    (x) => x.moduleName === mName && x.showInMenu === true
  );
  values = values.sort((a, b) => a.order - b.order);
  return values;
};

export const getEntityLabelHeading = (entityName) => {
  switch (entityName) {
    case entityNames.USERMANAGEMENT:
      return "Restaurant Users Management";
    case entityNames.HOURSOPERATION:
      return "Hours of Operation";
    case entityNames.MYRESTAURANTS:
      return "Revenue Management";
    case entityNames.PRICEENTRY:
    case entityNames.GROUPMANAGEMENT:
      return "Restaurant Groups Management";
    case entityNames.ROLEMANAGEMENT:
    default:
      return "Restaurant Roles Management";
  }
};

export const getAccessibleEntities = (privileges, mName) =>{
  let moduleEntities = getModuleEntities(privileges, mName);
  let accessibleEntities = moduleEntities.filter(
    (x) => x.showInMenu === true && x.canRead === true
  );
  return accessibleEntities;
}


export const getEntityDescription = (privileges, entityName) => {
  let description = "";
  if (
    privileges !== undefined &&
    privileges !== null &&
    privileges.length > 0
  ) {
    let found = privileges.find((x) => x.entityName === entityName);
    if (found !== null && found !== undefined) {
      description = found.entityDescription;
    }
  }
  return description;
};

const getOldState = (arr) => {
  let value = TriStateValue.UNCHECKED;
  let foundValue = false;
  if (arr !== null && arr !== undefined && arr.length > 0) {
    for (let c = 0; c < arr.length; c++) {
      let obj = arr[c];
      if (
        obj.Value !== TriStateValue.CHECKED &&
        obj.Value !== TriStateValue.PARTIAL
      ) {
      } else if (c === arr.length - 1) {
        value = TriStateValue.UNCHECKED;
        foundValue = true;
      }
    }
    if (foundValue === false) {
      for (let c = 0; c < arr.length; c++) {
        let obj = arr[c];
        if (
          obj.Value !== TriStateValue.UNCHECKED &&
          obj.Value !== TriStateValue.CHECKED
        ) {
        } else if (c === arr.length - 1) {
          value = TriStateValue.PARTIAL;
          foundValue = true;
        }
      }
    }
    if (foundValue === false) {
      for (let c = 0; c < arr.length; c++) {
        let obj = arr[c];
        if (
          obj.Value !== TriStateValue.PARTIAL &&
          obj.Value !== TriStateValue.UNCHECKED
        ) {
        } else if (c === arr.length - 1) {
          value = TriStateValue.CHECKED;
          foundValue = true;
        }
      }
    }
  }
  return value;
};
