import React, { forwardRef, useCallback } from 'react';
import { Button, CardContent, makeStyles, Typography } from '@material-ui/core';
import UserAvatar from './UserAvatar';
import { clearStoredPath } from '../../../utilities/redirect/LocalRedirectUrlStorage';
import { clearStoredExternalPath } from '../../../utilities/redirect/ExternalRedirectUrlStorage';
import { clearStoredTenant, storeTenant } from '../../../utilities/storage/UnitySessionStorage';
import { useTranslation } from 'react-i18next';
import { useUserActions } from '../../../utilities/auth/users';
import { useHistory as useHistoryNoType } from 'react-router-dom';
import { TenantSelect } from '../../../utilities/tenant';
const useHistory = useHistoryNoType;
const useStyles = makeStyles((theme) => ({
    root: {
        '& > *': {
            margin: theme.spacing(1)
        },
        '&:last-child': {
            paddingBottom: theme.spacing(2)
        }
    },
    cardHeader: {
        display: 'flex',
        marginBottom: theme.spacing(3)
    },
    currentTenant: {
        margin: 'auto',
        width: '85%',
        marginBottom: theme.spacing(4)
    },
    buttonGroup: {
        display: 'flex',
        flexDirection: 'row',
        alignItems: 'center'
    },
    basicInfoContainer: {
        marginLeft: theme.spacing(1)
    },
    basicInfo: {
        paddingLeft: theme.spacing(1)
    },
    divider: {
        height: theme.spacing(3)
    },
    avatar: {
        fontSize: 40
    }
}));
export const FluentProfileMenuCard = forwardRef((props, ref) => {
    const classes = useStyles();
    const devUnityUrl = 'https://unity-dev.univerus.com/';
    const { userTenants, currentTenant, user, logout, unityUrl = devUnityUrl, } = props;
    const history = useHistory();
    const redirectToHome = useCallback(() => history.push('/'), [history]);
    // eslint-disable-next-line
    const { t } = useTranslation();
    const { changeTenant } = useUserActions();
    const handleTenantChange = useCallback((tenant) => {
        storeTenant(tenant.tenantId);
        changeTenant(tenant.tenantId);
        redirectToHome();
    }, [changeTenant, redirectToHome]);
    // const handleProfileClick = () => {
    //   // if this is unity (productid is 1), go to the profile slug, otherwise open the profile page in a new tab
    //   if(ConfigService.config.UNITY_PRODUCT_ID === '1') history.push('/profile')
    //   else window.open(unityUrl + '/profile');
    // }
    if (!user)
        return null;
    return (React.createElement(CardContent, { ref: ref, className: classes.root }, React.createElement("div", { className: classes.cardHeader }, React.createElement(UserAvatar, { name: user === null || user === void 0 ? void 0 : user.name, email: user === null || user === void 0 ? void 0 : user.email, size: "large", className: classes.avatar }), React.createElement("div", { className: classes.basicInfoContainer }, React.createElement(Typography, { className: classes.basicInfo, variant: "subtitle2" }, user === null || user === void 0 ? void 0 : user.name), React.createElement(Typography, { className: classes.basicInfo, variant: "body2" }, user === null || user === void 0 ? void 0 : user.email))), userTenants && user && changeTenant && (React.createElement(TenantSelect, { className: classes.currentTenant, id: "current-tenant", tenants: userTenants, value: currentTenant, onTenantChange: handleTenantChange, label: t('Current Tenant'), recordIdPrefix: "udpRecord-FluentProfileMenuCard" })), React.createElement(Button, { id: "udpRecord-FluentProfileMenuCard-SignOut", 
        // @ts-expect-error Custom automation attribute not typed by MUI
        udprecordid: "udpRecord-FluentProfileMenuCard-SignOut", onClick: () => {
            clearStoredPath();
            clearStoredExternalPath();
            clearStoredTenant();
            logout();
        }, variant: "outlined", color: "secondary" }, t('Sign out'))));
});
FluentProfileMenuCard.displayName = 'FluentProfileMenuCard';
//# sourceMappingURL=FluentProfileMenuCard.js.map