export enum UserPermissions {
  // CUSTOM
  CAN_INTERACT_WITH_EXTERNAL_BOT = 'CAN_INTERACT_WITH_EXTERNAL_BOT',
  CREATE_REGULAR_GROUP_CHAT = 'CREATE_REGULAR_GROUP_CHAT',
  CREATE_POLLS = 'CREATE_POLLS',
  CREATE_POSTS = 'CREATE_POSTS',
  CREATE_COMMENT = 'CREATE_COMMENT',
  CREATE_COMMENT_IN_ARTICLES = 'CREATE_COMMENT_IN_ARTICLES',
  CREATE_COMMENT_IN_MARKETPLACE = 'CREATE_COMMENT_IN_MARKETPLACE',
  CREATE_COMMENT_IN_POSTS = 'CREATE_COMMENT_IN_POSTS',
  VIEW_MULTICOMPANY_GROUPS = 'VIEW_MULTICOMPANY_GROUPS',
  CREATE_MULTICOMPANY_GROUPS = 'CREATE_MULTICOMPANY_GROUPS',
  CAN_REACT_CONTENT = 'CAN_REACT_CONTENT',
  CAN_REACT_ACKNOWLEDGEMENTS = 'CAN_REACT_ACKNOWLEDGEMENTS',
  CAN_REACT_ARTICLES = 'CAN_REACT_ARTICLES',
  CAN_REACT_MARKETPLACE_POSTS = 'CAN_REACT_MARKETPLACE_POSTS',
  CAN_REACT_POSTS = 'CAN_REACT_POSTS',
  CREATE_ACKNOWLEDGEMENTS = 'CREATE_ACKNOWLEDGEMENTS',
  ACCESS_ACKNOWLEDGEMENTS = 'ACCESS_ACKNOWLEDGEMENTS',
  RECEIVE_ACKNOWLEDGEMENTS = 'RECEIVE_ACKNOWLEDGEMENTS',
  INVITE_USERS = 'INVITE_USERS',
  CAN_SEGMENTATE = 'CAN_SEGMENTATE',
  SEND_PUSH_NOTIFICATION = 'SEND_PUSH_NOTIFICATION',
  FILL_FORM = 'FILL_FORM',
  FILL_SURVEY = 'FILL_SURVEY',
  BLOCK_FILES_CHATS_V2 = 'BLOCK_FILES_CHATS_V2',
  // OLD MANAGEMENT
  MANAGE_POSTS = 'MANAGE_POSTS',
  MANAGE_MARKETPLACE_POSTS = 'MANAGE_MARKETPLACE_POSTS',
  MANAGE_USERS = 'MANAGE_USERS',
  MANAGE_CAPABILITIES = 'MANAGE_CAPABILITIES',
  MANAGE_SEGMENTATION = 'MANAGE_SEGMENTATION',
  MANAGE_ARTICLES = 'MANAGE_ARTICLES',
  MANAGE_EVENTS = 'MANAGE_EVENTS',
  MANAGE_VACATIONS = 'MANAGE_VACATIONS',
  MANAGE_PRODUCTS = 'MANAGE_PRODUCTS',
  MANAGE_TICKET_TOPICS = 'MANAGE_TICKET_TOPICS',
  MANAGE_INSTANCE = 'MANAGE_INSTANCE',
  MANAGE_ACKNOWLEDGEMENTS = 'MANAGE_ACKNOWLEDGEMENTS',
  MANAGE_COMMENTS = 'MANAGE_COMMENTS',
  MANAGE_ACKNOWLEDGEMENTS_COMMENTS = 'MANAGE_ACKNOWLEDGEMENTS_COMMENTS',
  MANAGE_ARTICLES_COMMENTS = 'MANAGE_ARTICLES_COMMENTS',
  MANAGE_FEED_COMMENTS = 'MANAGE_FEED_COMMENTS',
  MANAGE_MARKETPLACE_COMMENTS = 'MANAGE_MARKETPLACE_COMMENTS',
  MANAGE_REVIEWS = 'MANAGE_REVIEWS',
  // NEW MANAGEMENT
  CREATE_FILE = 'CREATE_FILE',
  EDIT_FILE = 'EDIT_FILE',
  DELETE_FILE = 'DELETE_FILE',
  CREATE_FORM = 'CREATE_FORM',
  EDIT_FORM = 'EDIT_FORM',
  DELETE_FORM = 'DELETE_FORM',
  CREATE_SURVEY = 'CREATE_SURVEY',
  EDIT_SURVEY = 'EDIT_SURVEY',
  DELETE_SURVEY = 'DELETE_SURVEY',
  CREATE_WIDGET = 'CREATE_WIDGET',
  EDIT_WIDGET = 'EDIT_WIDGET',
  DELETE_WIDGET = 'DELETE_WIDGET',
  CREATE_KNOWLEDGE_LIBRARIES = 'CREATE_KNOWLEDGE_LIBRARIES',
  EDIT_KNOWLEDGE_LIBRARIES = 'EDIT_KNOWLEDGE_LIBRARIES',
  DELETE_KNOWLEDGE_LIBRARIES = 'DELETE_KNOWLEDGE_LIBRARIES',
  CREATE_ONBOARDING = 'CREATE_ONBOARDING',
  CREATE_LIVESTREAM = 'CREATE_LIVESTREAM',
  EDIT_ONBOARDING = 'EDIT_ONBOARDING',
  DELETE_ONBOARDING = 'DELETE_ONBOARDING',
  CREATE_DOCUMENT = 'CREATE_DOCUMENT',
  EDIT_DOCUMENT = 'EDIT_DOCUMENT',
  DELETE_DOCUMENT = 'DELETE_DOCUMENT',
  MANAGE_TIME_TRACKING = 'MANAGE_TIME_TRACKING',
  CREATE_GROUPS = 'CREATE_GROUPS',
  CAN_APPROVE_FEED_POSTS = 'CAN_APPROVE_FEED_POSTS',
  REQUEST_FEED_POST_APPROVAL = 'REQUEST_FEED_POST_APPROVAL',
  CREATE_KEY_UPDATES = 'CREATE_KEY_UPDATES',
  CAN_DOWNLOAD_VIDEO = 'CAN_DOWNLOAD_VIDEO',
  MANAGE_SHIFTS = 'MANAGE_SHIFTS',
  SERVICE_MANAGER_AGENT_REPORTS = 'SERVICE_MANAGER_AGENT_REPORTS',
  // CONVERSATIONS
  CHATS_V2_PIN = 'CHATS_V2_PIN',
  CHATS_V2_PROCESS_EVENTS = 'CHATS_V2_PROCESS_EVENTS',
  CHATS_V2_MARK_UNREAD = 'CHATS_V2_MARK_UNREAD',
  CHATS_V2_CREATE_CONVERSATION = 'CHATS_V2_CREATE_CONVERSATION',
  // MODULES
  VIEW_WIDGET = 'VIEW_WIDGET',
  VIEW_EXTERNAL_APPS = 'VIEW_EXTERNAL_APPS',
  VIEW_SURVEY = 'VIEW_SURVEY',
  VIEW_FORM = 'VIEW_FORM',
  VIEW_FILE = 'VIEW_FILE',
  VIEW_POSTS = 'VIEW_POSTS',
  VIEW_GROUPS = 'VIEW_GROUPS',
  VIEW_POST_STATS = 'VIEW_POST_STATS',
  VIEW_ARTICLES = 'VIEW_ARTICLES',
  VIEW_REVIEWS = 'VIEW_REVIEWS',
  VIEW_REGULAR_CHATS = 'VIEW_REGULAR_CHATS',
  VIEW_CHATS_V2 = 'VIEW_CHATS_V2',
  VIEW_TICKETS = 'VIEW_TICKETS',
  VIEW_ACKNOWLEDGMENTS_RANKING = 'VIEW_ACKNOWLEDGMENTS_RANKING',
  VIEW_PEOPLE = 'VIEW_PEOPLE',
  VIEW_ONBOARDING = 'VIEW_ONBOARDING',
  VIEW_BIRTHDAYS = 'VIEW_BIRTHDAYS',
  VIEW_ANNIVERSARIES = 'VIEW_ANNIVERSARIES',
  VIEW_DOCUMENT = 'VIEW_DOCUMENT',
  VIEW_KNOWLEDGE_LIBRARIES = 'VIEW_KNOWLEDGE_LIBRARIES',
  VIEW_MARKETPLACE = 'VIEW_MARKETPLACE',
  VIEW_ORGANIZATION_CHARTS = 'VIEW_ORGANIZATION_CHARTS',
  VIEW_VACATIONS = 'VIEW_VACATIONS',
  VIEW_EVENTS_NEMAK = 'VIEW_EVENTS_NEMAK',
  VIEW_EVENTS = 'VIEW_EVENTS',
  VIEW_ACKNOWLEDGMENTS_NEMAK = 'VIEW_ACKNOWLEDGMENTS_NEMAK',
  VIEW_WORKFLOWS = 'VIEW_WORKFLOWS',
  VIEW_SCORMS = 'VIEW_SCORMS',
  VIEW_PEOPLE_EXPERIENCE = 'VIEW_PEOPLE_EXPERIENCE',
  VIEW_GOALS = 'VIEW_GOALS',
  VIEW_COURSES = 'VIEW_COURSES',
  VIEW_PATHS = 'VIEW_PATHS',
  VIEW_TIME_TRACKING = 'VIEW_TIME_TRACKING',
  VIEW_LIVESTREAM = 'VIEW_LIVESTREAM',
  VIEW_PAYROLL_MOBILITY_ADO = 'VIEW_PAYROLL_MOBILITY_ADO',
  VIEW_KEY_UPDATES = 'VIEW_KEY_UPDATES',
  VIEW_CHATBOT_AI = 'VIEW_CHATBOT_AI',
  VIEW_EMPLOYEE_LIFECYCLE = 'VIEW_EMPLOYEE_LIFECYCLE',
  VIEW_SHIFTS = 'VIEW_SHIFTS',
  VIEW_LEARNING_SESSIONS = 'VIEW_LEARNING_SESSIONS',
  VIEW_SPORTS_POOL = 'VIEW_SPORTS_POOL',
  VIEW_MICROLOANS = 'VIEW_MICROLOANS',
}

export enum UserBackOfficePermissions {
  // OLD MANAGEMENT
  MANAGE_USERS = 'MANAGE_USERS',
  MANAGE_CAPABILITIES = 'MANAGE_CAPABILITIES',
  MANAGE_SEGMENTATION = 'MANAGE_SEGMENTATION',
  MANAGE_ARTICLES = 'MANAGE_ARTICLES',
  MANAGE_VACATIONS = 'MANAGE_VACATIONS',
  MANAGE_PRODUCTS = 'MANAGE_PRODUCTS',
  MANAGE_TICKET_TOPICS = 'MANAGE_TICKET_TOPICS',
  MANAGE_INSTANCE = 'MANAGE_INSTANCE',
  MANAGE_ACKNOWLEDGEMENTS = 'MANAGE_ACKNOWLEDGEMENTS',
  MANAGE_COMMENTS = 'MANAGE_COMMENTS',
  // NEW MANAGEMENT
  CREATE_FILE = 'CREATE_FILE',
  EDIT_FILE = 'EDIT_FILE',
  DELETE_FILE = 'DELETE_FILE',
  CREATE_FORM = 'CREATE_FORM',
  EDIT_FORM = 'EDIT_FORM',
  DELETE_FORM = 'DELETE_FORM',
  CREATE_SURVEY = 'CREATE_SURVEY',
  EDIT_SURVEY = 'EDIT_SURVEY',
  DELETE_SURVEY = 'DELETE_SURVEY',
  CREATE_WIDGET = 'CREATE_WIDGET',
  EDIT_WIDGET = 'EDIT_WIDGET',
  DELETE_WIDGET = 'DELETE_WIDGET',
  CREATE_KNOWLEDGE_LIBRARIES = 'CREATE_KNOWLEDGE_LIBRARIES',
  EDIT_KNOWLEDGE_LIBRARIES = 'EDIT_KNOWLEDGE_LIBRARIES',
  DELETE_KNOWLEDGE_LIBRARIES = 'DELETE_KNOWLEDGE_LIBRARIES',
  CREATE_ONBOARDING = 'CREATE_ONBOARDING',
  CREATE_LIVESTREAM = 'CREATE_LIVESTREAM',
  EDIT_ONBOARDING = 'EDIT_ONBOARDING',
  DELETE_ONBOARDING = 'DELETE_ONBOARDING',
  CREATE_DOCUMENT = 'CREATE_DOCUMENT',
  EDIT_DOCUMENT = 'EDIT_DOCUMENT',
  DELETE_DOCUMENT = 'DELETE_DOCUMENT',
  MANAGE_TIME_TRACKING = 'MANAGE_TIME_TRACKING',
}

export enum UserCapabilityNames {
  CREATE_POLLS = 'CREATE_POLLS',
  CREATE_POSTS = 'CREATE_POSTS',
  CREATE_FILE = 'CREATE_FILE',
  CREATE_WIDGET = 'CREATE_WIDGET',
  CREATE_SURVEY = 'CREATE_SURVEY',
  CREATE_FORM = 'CREATE_FORM',
  CREATE_ACKNOWLEDGEMENTS = 'CREATE_ACKNOWLEDGEMENTS',
  CREATE_ONBOARDING = 'CREATE_ONBOARDING',
  CREATE_LIVESTREAM = 'CREATE_LIVESTREAM',
  CREATE_DOCUMENT = 'CREATE_DOCUMENT',
  EDIT_SURVEY = 'EDIT_SURVEY',
  EDIT_FORM = 'EDIT_FORM',
  EDIT_WIDGET = 'EDIT_WIDGET',
  EDIT_FILE = 'EDIT_FILE',
  EDIT_ONBOARDING = 'EDIT_ONBOARDING',
  EDIT_DOCUMENT = 'EDIT_DOCUMENT',
  DELETE_SURVEY = 'DELETE_SURVEY',
  DELETE_FORM = 'DELETE_FORM',
  DELETE_WIDGET = 'DELETE_WIDGET',
  DELETE_FILE = 'DELETE_FILE',
  DELETE_ONBOARDING = 'DELETE_ONBOARDING',
  DELETE_DOCUMENT = 'DELETE_DOCUMENT',
  MANAGE_POSTS = 'MANAGE_POSTS',
  MANAGE_MARKETPLACE_POSTS = 'MANAGE_MARKETPLACE_POSTS',
  MANAGE_USERS = 'MANAGE_USERS',
  MANAGE_CAPABILITIES = 'MANAGE_CAPABILITIES',
  MANAGE_SEGMENTATION = 'MANAGE_SEGMENTATION',
  MANAGE_ARTICLES = 'MANAGE_ARTICLES',
  MANAGE_VACATIONS = 'MANAGE_VACATIONS',
  MANAGE_PRODUCTS = 'MANAGE_PRODUCTS',
  MANAGE_TICKET_TOPICS = 'MANAGE_TICKET_TOPICS',
  MANAGE_ACKNOWLEDGEMENTS = 'MANAGE_ACKNOWLEDGEMENTS',
  MANAGE_COMMENTS = 'MANAGE_COMMENTS',
  MANAGE_INSTANCE = 'MANAGE_INSTANCE',
  RECEIVE_ACKNOWLEDGEMENTS = 'RECEIVE_ACKNOWLEDGEMENTS',
  CAN_SEGMENTATE = 'CAN_SEGMENTATE',
  SEND_PUSH_NOTIFICATION = 'SEND_PUSH_NOTIFICATION',
  INVITE_USERS = 'INVITE_USERS',
  CREATE_API_KEY = 'CREATE_API_KEY',
  CREATE_REGULAR_GROUP_CHAT = 'CREATE_REGULAR_GROUP_CHAT',
  DUPLICATE_INSTANCE = 'DUPLICATE_INSTANCE',
  IGNORE_SEGMENTATION = 'IGNORE_SEGMENTATION',
  MANAGE_TIME_TRACKING = 'MANAGE_TIME_TRACKING',
}

export enum InstanceCapabilityNames {
  VIEW_FILE = 'VIEW_FILE',
  VIEW_POSTS = 'VIEW_POSTS',
  VIEW_ARTICLES = 'VIEW_ARTICLES',
  VIEW_REGULAR_CHATS = 'VIEW_REGULAR_CHATS',
  VIEW_TICKETS = 'VIEW_TICKETS',
  VIEW_ACKNOWLEDGMENTS_RANKING = 'VIEW_ACKNOWLEDGMENTS_RANKING',
  VIEW_PEOPLE = 'VIEW_PEOPLE',
  ACCESS_ACKNOWLEDGEMENTS = 'ACCESS_ACKNOWLEDGEMENTS',
  VIEW_SURVEY = 'VIEW_SURVEY',
  VIEW_FORM = 'VIEW_FORM',
  VIEW_WIDGET = 'VIEW_WIDGET',
  VIEW_ONBOARDING = 'VIEW_ONBOARDING',
  CREATE_COMMENT = 'CREATE_COMMENT',
  VIEW_DOCUMENT = 'VIEW_DOCUMENT',
  VIEW_BIRTHDAYS = 'VIEW_BIRTHDAYS',
  CAN_EXCHANGE = 'CAN_EXCHANGE',
  VIEW_TIME_TRACKING = 'VIEW_TIME_TRACKING',
  VIEW_SHIFTS = 'VIEW_SHIFTS',
}

export const USER_APP_PERMISSIONS = [
  UserPermissions.CREATE_POSTS,
  UserPermissions.CREATE_POLLS,
  UserPermissions.CAN_SEGMENTATE,
  UserPermissions.RECEIVE_ACKNOWLEDGEMENTS,
  UserPermissions.CREATE_ACKNOWLEDGEMENTS,
  UserPermissions.SEND_PUSH_NOTIFICATION,
  UserPermissions.INVITE_USERS,
  UserPermissions.CREATE_MULTICOMPANY_GROUPS,
];

export const ROUTE_PERMISSIONS = {
  FEED: [UserPermissions.VIEW_POSTS],
  KEY_UPDATES: [UserPermissions.VIEW_KEY_UPDATES],
  GROUPS: [UserPermissions.VIEW_GROUPS],
  NEWS: [UserPermissions.VIEW_ARTICLES],
  PROFILE: [],
  USER_FEED: [UserPermissions.VIEW_POSTS],
  USER_ACKNOWLEDGEMENTS: [UserPermissions.ACCESS_ACKNOWLEDGEMENTS],
  CHATS: [UserPermissions.VIEW_REGULAR_CHATS, UserPermissions.VIEW_TICKETS],
  CONVERSATIONS: [UserPermissions.VIEW_CHATS_V2],
  TICKETS: [UserPermissions.VIEW_TICKETS],
  REGULAR_CHATS: [UserPermissions.VIEW_REGULAR_CHATS],
  NEW_ONE_TO_ONE: [UserPermissions.VIEW_REGULAR_CHATS],
  NEW_TICKET: [UserPermissions.VIEW_TICKETS],
  NEW_GROUP: [
    UserPermissions.VIEW_REGULAR_CHATS,
    UserPermissions.CREATE_REGULAR_GROUP_CHAT,
  ],
  FILES: [UserPermissions.VIEW_FILE],
  PEOPLE: [UserPermissions.VIEW_PEOPLE],
  PERFORMANCE: [UserPermissions.VIEW_REVIEWS],
  FORMS: [UserPermissions.VIEW_FORM],
  SURVEYS: [UserPermissions.VIEW_SURVEY],
  KNOWLEDGE_LIBRARY: [UserPermissions.VIEW_KNOWLEDGE_LIBRARIES],
  ONBOARDING: [UserPermissions.VIEW_ONBOARDING],
  DOCUMENTS: [UserPermissions.VIEW_DOCUMENT],
  MARKETPLACE: [UserPermissions.VIEW_MARKETPLACE],
  WIDGETS: [UserPermissions.VIEW_WIDGET],
  ORG_CHART: [UserPermissions.VIEW_ORGANIZATION_CHARTS],
  VACATIONS: [UserPermissions.VIEW_VACATIONS],
  EVENTS_NEMAK: [UserPermissions.VIEW_EVENTS_NEMAK],
  EVENTS: [UserPermissions.VIEW_EVENTS],
  MANAGE_EVENTS: [UserPermissions.MANAGE_EVENTS],
  RECOGNITIONS_NEMAK: [UserPermissions.VIEW_ACKNOWLEDGMENTS_NEMAK],
  WORKFLOWS: [UserPermissions.VIEW_WORKFLOWS],
  SCORMS: [UserPermissions.VIEW_SCORMS],
  PEOPLE_EXPERIENCE: [UserPermissions.VIEW_PEOPLE_EXPERIENCE],
  GOALS: [UserPermissions.VIEW_GOALS],
  // Learnings
  COURSES: [UserPermissions.VIEW_COURSES],
  PATHS: [UserPermissions.VIEW_PATHS],
  SESSIONS: [UserPermissions.VIEW_LEARNING_SESSIONS],
  TIME_TRACKING: [
    UserPermissions.VIEW_TIME_TRACKING,
    UserPermissions.MANAGE_TIME_TRACKING,
  ],
  TIME_TRACKING_BOSS: [UserPermissions.VIEW_TIME_TRACKING], // Permission applies to user with subordinates
  LIVESTREAM: [UserPermissions.VIEW_LIVESTREAM],
  PAYROLL_MOBILITY_ADO: [UserPermissions.VIEW_PAYROLL_MOBILITY_ADO],

  CREATE_GROUPS: [UserPermissions.CREATE_GROUPS],
  FEED_PENDING_POSTS: [UserPermissions.CAN_APPROVE_FEED_POSTS],
  EMPLOYEE_LIFECYCLE: [UserPermissions.VIEW_EMPLOYEE_LIFECYCLE],
  SHIFTS: [UserPermissions.VIEW_SHIFTS, UserPermissions.MANAGE_SHIFTS],
  SHIFTS_BOSS: [UserPermissions.VIEW_SHIFTS], // Permission applies to user with subordinates
  SPORTS_POOL: [UserPermissions.VIEW_SPORTS_POOL],
  MICROLOANS: [UserPermissions.VIEW_MICROLOANS],
};

export const hasAnyPermissions = (
  userPermissions: string[],
  requiredPermissions: string[],
) =>
  !requiredPermissions ||
  requiredPermissions.length === 0 ||
  requiredPermissions.some(requiredPermission =>
    userPermissions.includes(requiredPermission),
  );

export const hasAllPermissions = (
  userPermissions: string[],
  requiredPermissions: string[],
) =>
  !requiredPermissions ||
  requiredPermissions.length === 0 ||
  requiredPermissions.every(requiredPermission =>
    userPermissions.includes(requiredPermission),
  );

const MINIMUM_REQUIRED_PERMISSIONS: string[] = [];

const ADMIN_MINIMUM_REQUIRED_PERMISSIONS = [
  UserPermissions.MANAGE_USERS,
  UserPermissions.MANAGE_CAPABILITIES,
  UserPermissions.MANAGE_SEGMENTATION,
  UserPermissions.MANAGE_ARTICLES,
  UserPermissions.MANAGE_PRODUCTS,
  UserPermissions.MANAGE_TICKET_TOPICS,
  UserPermissions.MANAGE_ACKNOWLEDGEMENTS,
  UserPermissions.MANAGE_INSTANCE,
  UserPermissions.CREATE_FILE,
  UserPermissions.EDIT_FILE,
  UserPermissions.DELETE_FILE,
  UserPermissions.CREATE_SURVEY,
  UserPermissions.EDIT_SURVEY,
  UserPermissions.DELETE_SURVEY,
  UserPermissions.CREATE_FORM,
  UserPermissions.EDIT_FORM,
  UserPermissions.DELETE_FORM,
  UserPermissions.CREATE_WIDGET,
  UserPermissions.EDIT_WIDGET,
  UserPermissions.DELETE_WIDGET,
  UserPermissions.CREATE_KNOWLEDGE_LIBRARIES,
  UserPermissions.EDIT_KNOWLEDGE_LIBRARIES,
  UserPermissions.DELETE_KNOWLEDGE_LIBRARIES,
  UserPermissions.CREATE_ONBOARDING,
  UserPermissions.EDIT_ONBOARDING,
  UserPermissions.DELETE_ONBOARDING,
  UserPermissions.CREATE_DOCUMENT,
  UserPermissions.EDIT_DOCUMENT,
  UserPermissions.DELETE_DOCUMENT,
  UserPermissions.CREATE_KNOWLEDGE_LIBRARIES,
  UserPermissions.EDIT_KNOWLEDGE_LIBRARIES,
  UserPermissions.DELETE_KNOWLEDGE_LIBRARIES,
  UserPermissions.MANAGE_VACATIONS,
];

export const canLogin = (userPermissions: string[]) =>
  hasAnyPermissions(userPermissions, MINIMUM_REQUIRED_PERMISSIONS);

export const isAdmin = (userPermissions: string[]) =>
  hasAnyPermissions(userPermissions, ADMIN_MINIMUM_REQUIRED_PERMISSIONS);

export const insertIf = (
  permissions: string[],
  requiredPermissions: string[],
  value: any,
): any[] =>
  hasAnyPermissions(permissions, requiredPermissions) ? [value] : [];

export const guardFilter = (items: any[], permissions: string[]): any[] =>
  items.filter(item =>
    hasAnyPermissions(permissions, item.requiredPermissions),
  );

/*
  Future improvement:
  capabilities: Record<UserCapabilityNames | InstanceCapabilityNames, boolean>

  NOTE: This change will need to update the functions updateUserPermissions() and updateInstancePermissions() in JWTContext.tsx
  and for that we need to also fix all the TS errors on that file
*/
export const updatePermissions = (
  permissions: Record<UserPermissions, boolean>,
  capabilities:
    | Record<UserCapabilityNames, boolean>
    | Record<InstanceCapabilityNames, boolean>,
): Record<UserPermissions, boolean> => {
  if (!permissions) return permissions;

  const newPermissions = { ...permissions };

  // For reference UserPermissions = UserCapabilityNames + InstanceCapabilityNames
  Object.keys(capabilities).forEach(capability => {
    newPermissions[capability as keyof typeof permissions] =
      capabilities[capability as keyof typeof capabilities];
  });

  return newPermissions;
};
