/* global window */
import { contains } from 'ramda';

import normalizeRoles from './normalizeRoles';

const loggedUser = JSON.parse(window.localStorage.getItem('loggedUser'));

if (loggedUser) {
  loggedUser.anyOf = (...profiles) => {
    const userRole = loggedUser.profiles[0];
    const userOrgType = userRole.organization.type.key;
    const roles = normalizeRoles(profiles);

    if (!roles) {
      return true;
    }

    const role = roles[userRole.key];

    if (role && contains(userOrgType, role)) {
      return true;
    }

    return false;
  };
}

export default loggedUser;
