import React from 'react';
import { Button, makeStyles, IconButton } from '@material-ui/core';
import { Link } from 'react-router-dom';
import MenuOpener from './MenuOpener';
import UserAvatar from './UserAvatar';
import { FluentProfileMenuCard } from './FluentProfileMenuCard';
import { ConfigService } from '../../../configService';
import { useSingleLogout } from '../../../utilities/auth';
import { useUser } from '../../../utilities/auth/useUser';
import { useTenant } from '../../../utilities/tenant/useTenant';
const useStyles = makeStyles((theme) => ({
    root: {},
    avatar: {
        background: theme.palette.secondary.main
    }
}));
export const UserProfileMenuWrapper = ({ returnTo }) => {
    var _a, _b, _c;
    const classes = useStyles();
    const user = useUser();
    const singleLogout = useSingleLogout();
    const tenantContext = useTenant();
    const currentTenant = (_a = tenantContext === null || tenantContext === void 0 ? void 0 : tenantContext.activeTenant) !== null && _a !== void 0 ? _a : null;
    const availableTenants = (_c = (_b = tenantContext === null || tenantContext === void 0 ? void 0 : tenantContext.filteredTenantsList) !== null && _b !== void 0 ? _b : tenantContext === null || tenantContext === void 0 ? void 0 : tenantContext.tenantsList) !== null && _c !== void 0 ? _c : [];
    const enforcedTenantId = ConfigService.config.UNITY_TENANT_ID;
    const profileMenuTenants = !enforcedTenantId || enforcedTenantId === ''
        ? availableTenants
        : availableTenants.filter((option) => option.tenantId === enforcedTenantId);
    if (!user) {
        const fallbackPath = returnTo !== null && returnTo !== void 0 ? returnTo : (typeof window !== 'undefined' ? window.location.pathname : '/');
        return (React.createElement("div", { className: classes.root }, React.createElement(Button, { component: Link, to: {
                pathname: '/login',
                state: { returnTo: fallbackPath !== null && fallbackPath !== void 0 ? fallbackPath : '/' }
            }, color: "inherit" }, "Sign i")));
    }
    return (React.createElement("div", { className: classes.root }, React.createElement(MenuOpener, { name: "udp-user-profile-popover", button: React.createElement(IconButton, null, React.createElement(UserAvatar, { name: user === null || user === void 0 ? void 0 : user.name, email: user === null || user === void 0 ? void 0 : user.email, size: "toolbar", className: classes.avatar })), menuProps: {
            keepMounted: true,
            getContentAnchorEl: null,
            variant: 'menu',
            anchorOrigin: { vertical: 'bottom', horizontal: 'right' },
            transformOrigin: { vertical: 'top', horizontal: 'right' }
        } }, React.createElement(FluentProfileMenuCard, { userTenants: profileMenuTenants, currentTenant: currentTenant, user: user, logout: singleLogout, unityUrl: ConfigService.config.UNITY_URL }))));
};
export default UserProfileMenuWrapper;
//# sourceMappingURL=UserProfileMenuWrapper.js.map