"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.transformAssignedPermissions = exports.transformAssignablePermissions = exports.hasAccess = exports.getAssignablePermissions = exports.getAccessName = void 0;
var _enums = require("../constants/enums");
var _axiosAPIClient = _interopRequireDefault(require("../api/axiosAPIClient"));
var _wrapperService = require("./wrapperService");
const baseAPIUrl = process.env.REACT_APP_AUTHAPI_URL;
const getAccessName = type => {
  switch (type) {
    case _enums.accessTypes.UPDATE:
      {
        return "Update";
      }
    case _enums.accessTypes.CREATE:
      {
        return "Create";
      }
    case _enums.accessTypes.DELETE:
      {
        return "Delete";
      }
    case _enums.accessTypes.READ:
    default:
      {
        return "Read";
      }
  }
};
exports.getAccessName = getAccessName;
const hasAccess = (privileges, entityName, accessType) => {
  let result = false;
  if (privileges !== undefined && privileges !== null && privileges.length > 0) {
    let found = privileges.find(x => x.entityName === entityName);
    if (found !== null && found !== undefined) {
      switch (accessType) {
        case _enums.accessTypes.CREATE:
          {
            result = found.canCreate === true;
            break;
          }
        case _enums.accessTypes.UPDATE:
          {
            result = found.canUpdate === true;
            break;
          }
        case _enums.accessTypes.DELETE:
          {
            result = found.canDelete === true;
            break;
          }
        default:
        case _enums.accessTypes.READ:
          {
            result = found.canRead === true;
            break;
          }
      }
    }
  }
  return result;
};
exports.hasAccess = hasAccess;
const getAssignablePermissions = async brandCode => {
  console.log(baseAPIUrl);
  console.log((0, _wrapperService.getAPIPrefix)());
  let url = "".concat(baseAPIUrl).concat((0, _wrapperService.getAPIPrefix)(), "/RolesManagement/GetAssignablePermissions?brandCode=").concat(brandCode);
  const permissionsResponse = await _axiosAPIClient.default.get(url);
  let data = await permissionsResponse.data;
  return data;
};
exports.getAssignablePermissions = getAssignablePermissions;
const transformAssignablePermissions = assignablePermissions => {
  let transformedObject = {};
  transformedObject.headings = getArrUnchecked();
  let modules = [];
  for (let k = 0; k < assignablePermissions.length; k++) {
    let permission = assignablePermissions[k];
    let module = {};
    module.name = permission.moduleName;
    module.description = permission.moduleDescription;
    module.expanded = true;
    module.accessTypes = getArrUnchecked();
    let children = [];
    let sortedEntities = permission.entities.sort((a, b) => {
      return a.displayIndex < b.displayIndex ? -1 : 1;
    });
    for (let a = 0; a < sortedEntities.length; a++) {
      let entity = sortedEntities[a];
      let child = {};
      let v = shouldConfigureNumberOfDays(entity);
      child.configureNumberOfDays = v;
      child.name = entity.entityName;
      child.id = entity.entityId;
      child.description = entity.entityDescription;
      child.accessTypes = getArrUnchecked();
      child.isAutoAccept = entity.isAutoAccept;
      child.autoAcceptType = entity.autoAcceptType;
      child.isCorporateOnly = entity.isCorporateOnly;
      child.numberOfDays = entity.numberOfDays;
      child.indentLevel = entity.indentLevel;
      children.push(child);
    }
    module.children = children;
    modules.push(module);
  }
  transformedObject.modules = modules;
  return transformedObject;
};
exports.transformAssignablePermissions = transformAssignablePermissions;
const transformAssignedPermissions = function (assignablePermissions, assignedPermissions) {
  let isFranchisee = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
  let transformedObject = {};
  transformedObject.headings = getArr(assignedPermissions, 2);
  let modules = [];
  const getMatchedModule = module => {
    let assignedPermissionsModule = assignedPermissions.modulePermissions;
    if (assignedPermissionsModule !== null && assignedPermissionsModule !== undefined) {
      let matchedModule = assignedPermissions.modulePermissions.find(x => x.moduleName === module.name);
      return matchedModule;
    }
    return undefined;
  };
  const getMatchedEntity = (module, entity) => {
    let matchedModule = getMatchedModule(module);
    if (matchedModule === undefined || matchedModule === null) {
      return undefined;
    }
    let moduleEntityPermissions = matchedModule.entityPermissions;
    if (moduleEntityPermissions !== null && moduleEntityPermissions !== undefined) {
      return matchedModule.entityPermissions.find(x => x.entityName === entity.name);
    }
    return undefined;
  };
  if (isFranchisee === false) {
    let sortedModules = assignablePermissions.modules.sort((a, b) => {
      return a.displayOrder < b.displayOrder ? -1 : 1;
    });
    for (let k = 0; k < sortedModules.length; k++) {
      let permission = sortedModules[k];
      let module = {};
      module.name = permission.name;
      module.description = permission.description;
      module.expanded = true;
      let matchedModule = getMatchedModule(module);
      if (matchedModule === undefined || matchedModule === null) {
        module.accessTypes = getArrUnchecked();
      } else {
        module.accessTypes = getArr(matchedModule, 1);
      }
      let children = [];
      let sortedEntities = permission.children.sort((a, b) => {
        return a.displayIndex < b.displayIndex ? -1 : 1;
      });
      for (let a = 0; a < sortedEntities.length; a++) {
        let entity = sortedEntities[a];
        let child = {};
        child.configureNumberOfDays = shouldConfigureNumberOfDays(entity); //  entity.configureNumberOfDays;
        child.id = entity.id;
        child.name = entity.name;
        child.description = entity.description;
        child.displayIndex = entity.displayIndex;
        child.indentLevel = entity.indentLevel;
        let matchedEntity = getMatchedEntity(module, child);
        if (matchedEntity === undefined || matchedEntity === null) {
          child.accessTypes = getArrUnchecked();
          child.numberOfDays = 0;
          child.isAutoAccept = false;
          child.autoAcceptType = null;
        } else {
          child.accessTypes = getArr(matchedEntity, 0);
          child.numberOfDays = matchedEntity.numberOfDays;
          child.isAutoAccept = matchedEntity.isAutoAccept;
          child.autoAcceptType = matchedEntity.autoAcceptType;
        }
        children.push(child);
      }
      module.children = children;
      modules.push(module);
    }
  } else {
    let sortedModules = assignedPermissions.modulePermissions;
    for (let k = 0; k < sortedModules.length; k++) {
      let permission = sortedModules[k];
      let module = {};
      module.name = permission.moduleName;
      module.description = permission.moduleDescription;
      module.expanded = true;
      let matchedModule = getMatchedModule(module);
      if (matchedModule === undefined || matchedModule === null) {
        module.accessTypes = getArrUnchecked();
      } else {
        module.accessTypes = getArr(matchedModule, 1);
      }
      let children = [];
      let sortedEntities = permission.entityPermissions.sort((a, b) => {
        return a.displayIndex < b.displayIndex ? -1 : 1;
      });
      for (let a = 0; a < sortedEntities.length; a++) {
        let entity = sortedEntities[a];
        if (entity.isCorporateOnly === false) {
          let child = {};
          child.configureNumberOfDays = shouldConfigureNumberOfDays(entity, assignablePermissions);
          child.id = entity.entityId;
          child.name = entity.entityName;
          child.description = entity.entityDescription;
          child.displayIndex = entity.displayIndex;
          child.indentLevel = entity.indentLevel;
          let matchedEntity = getMatchedEntity(module, child);
          if (matchedEntity === undefined || matchedEntity === null) {
            child.accessTypes = getArrUnchecked();
            child.numberOfDays = null;
            child.isAutoAccept = false;
            child.autoAcceptType = null;
          } else {
            child.accessTypes = getArr(matchedEntity, 0);
            if (child.configureNumberOfDays) {
              if (matchedEntity.numberOfDays > 0) {
                child.numberOfDays = matchedEntity.numberOfDays;
              } else {
                child.numberOfDays = 1;
              }
            } else {
              child.numberOfDays = null;
            }
            child.isAutoAccept = matchedEntity.isAutoAccept;
            child.autoAcceptType = matchedEntity.autoAcceptType;
          }
          children.push(child);
        }
      }
      module.children = children;
      modules.push(module);
    }
  }
  transformedObject.modules = modules;
  return transformedObject;
};
exports.transformAssignedPermissions = transformAssignedPermissions;
const shouldConfigureNumberOfDays = function (entityObject) {
  let aP = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined;
  /*const eName = entityObject.entityName?.toUpperCase();
  if(eName === 'OAREFUNDS' || eName === 'OASTATUS'){
    return true;
  }*/
  const configureNumberOfDays = entityObject === null || entityObject === void 0 ? void 0 : entityObject.configureNumberOfDays;
  if (configureNumberOfDays !== undefined && configureNumberOfDays !== null && configureNumberOfDays === true) {
    return true;
  }
  if (aP !== undefined) {
    let found = null;
    for (let c = 0; c < (aP === null || aP === void 0 ? void 0 : aP.modules.length); c++) {
      let module = aP.modules[c];
      for (let k = 0; k < module.children.length; k++) {
        let f = module.children[k];
        if (f.id === entityObject.entityId) {
          found = f;
          break;
        }
      }
    }
    if (found !== null) {
      var _found$configureNumbe;
      return (_found$configureNumbe = found.configureNumberOfDays) !== null && _found$configureNumbe !== void 0 ? _found$configureNumbe : false;
    }
  }
  const numberOfDays = entityObject === null || entityObject === void 0 ? void 0 : entityObject.numberOfDays;
  if (numberOfDays !== undefined && numberOfDays !== null && numberOfDays > 0) {
    return true;
  }
  return false;
};
const getArrUnchecked = () => {
  let defaultValue = _enums.TriStateValue.UNCHECKED;
  let arrValue = [];
  for (let prop in _enums.accessTypes) {
    arrValue.push({
      name: prop,
      value: defaultValue,
      percent: 0
    });
  }
  return arrValue;
};
const getArr = function (obj) {
  let type = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
  let arrValue = [];
  for (let prop in _enums.accessTypes) {
    let stateValue = _enums.TriStateValue.UNCHECKED;
    let percent = 0;
    if (type === 0) {
      stateValue = obj[_enums.accessTypes[prop]] === true ? _enums.TriStateValue.CHECKED : _enums.TriStateValue.UNCHECKED;
    } else if (type === 1 || type === 2) {
      let percentPropName = "percent" + getAccessName(_enums.accessTypes[prop]);
      percent = Number(parseFloat(obj[percentPropName]).toFixed(2));
      if (percent === 100) {
        stateValue = _enums.TriStateValue.CHECKED;
      } else if (percent === 0) {
        stateValue = _enums.TriStateValue.UNCHECKED;
      } else {
        stateValue = _enums.TriStateValue.PARTIAL;
      }
    }
    arrValue.push({
      name: prop,
      value: stateValue,
      percent: percent
    });
  }
  return arrValue;
};