import React from "react";

import {getGroups, getProviders} from "../../../utils/roles";

let token = null;
let groups = null;
let providers = null;
let fetching = false;
let error = null;
let signInResp = {};
let authorizeURL =
  "https://cognito.com/authorize?redirect=https://example.com/callback";
let logoutURL =
  "https://cognito.com/logout?redirect=https://example.com/callback";

export const UserProvider = ({ children }) => {
  return <div data-testid="mocked-userprovider">{children}</div>;
};

export const useUserContext = () => ({
  error,
  fetching,
  groups,
  providers,
  token,
  getAuthorizeURL: jest.fn(() => authorizeURL),
  getLogoutURL: jest.fn(() => logoutURL),
  setError: jest.fn((value) => {
    error = value;
  }),
  signIn: jest.fn(() => signInResp),
  signOut: jest.fn(),
  refresh: jest.fn(),
});

export const setToken = (val) => {
  token = val;
  if (!val || !val.idToken || !val.idToken.jwtToken) {
    groups = null;
    providers = null
  } else {
    groups = getGroups(val.idToken.jwtToken);
    providers = getProviders(val.idToken.jwtToken);
  }
};

export const setFetching = (val) => {
  fetching = val;
};

export const setError = (val) => {
  error = val;
};
