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

import normalizeRoles from './normalizeRoles';

const loggedUser = () => {
  let user = JSON.parse(window.localStorage.getItem('loggedUser'));

  const anyOf = (...profiles) => {
    const userRole = user.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;
  }

  const update = () => {
    user = JSON.parse(window.localStorage.getItem('loggedUser'));
    return user;
  }

  return {
    user,
    anyOf: anyOf,
    update: update,
  }
}

export default loggedUser();
