import { __rest } from "tslib";
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import { useMemo } from 'react';
import { styled } from '@mui/material/styles';
import { Box, Paper, Typography } from '@mui/material';
import ChangeCover from '../ChangeCover';
import ChangePicture from '../ChangePicture';
import { Link, SCPreferences, useSCContext, useSCFetchUser, useSCPreferences, useSCUser } from '@selfcommunity/react-core';
import UserProfileHeaderSkeleton from './Skeleton';
import classNames from 'classnames';
import { useThemeProps } from '@mui/system';
import { PREFIX } from './constants';
import UserAvatar from '../../shared/UserAvatar';
const classes = {
    root: `${PREFIX}-root`,
    cover: `${PREFIX}-cover`,
    avatar: `${PREFIX}-avatar`,
    info: `${PREFIX}-info`,
    infOpsSection: `${PREFIX}-infops-section`,
    username: `${PREFIX}-username`,
    realname: `${PREFIX}-realname`,
    bio: `${PREFIX}-bio`,
    website: `${PREFIX}-website`,
    changePicture: `${PREFIX}-change-picture`,
    changeCover: `${PREFIX}-change-cover`
};
const Root = styled(Box, {
    name: PREFIX,
    slot: 'Root'
})(() => ({}));
/**
 * > API documentation for the Community-JS User Profile AppBar component. Learn about the available props and the CSS API.
 *
 *
 * This component renders the user profile's top section.
 * Take a look at our <strong>demo</strong> component [here](/docs/sdk/community-js/react-ui/Components/UserProfileHeader)

 #### Import

 ```jsx
 import {UserProfileHeader} from '@selfcommunity/react-ui';
 ```

 #### Component Name

 The name `SCUserProfileHeader` can be used when providing style overrides in the theme.


 #### CSS

 |Rule Name|Global class|Description|
 |---|---|---|
 |root|.SCUserProfileHeader-root|Styles applied to the root element.|
 |cover|.SCUserProfileHeader-cover|Styles applied to the cover element.|
 |avatar|.SCUserProfileHeader-avatar|Styles applied to the avatar element.|
 |infOpsSection|SCUserProfileHeader-infops-section|Styles applied to the section including info and actions.|
 |info|SCUserProfileHeader-info|Styles applied to the info section.|
 |username|SCUserProfileHeader-username|Styles applied to the username element.|
 |realname|SCUserProfileHeader-realname|Styles applied to the realname element.|
 |changePicture|.SCUserProfileHeader-change-picture|Styles applied to changePicture element.|
 |changeCover|.SCUserProfileHeader-change-cover`|Styles applied to changeCover element.|

 * @param inProps
 */
export default function UserProfileHeader(inProps) {
    // PROPS
    const props = useThemeProps({
        props: inProps,
        name: PREFIX
    });
    const { id = null, className = null, userId = null, user = null, ChangePictureProps = {}, ChangeCoverProps = {}, actions } = props, rest = __rest(props, ["id", "className", "userId", "user", "ChangePictureProps", "ChangeCoverProps", "actions"]);
    // PREFERENCES
    const scPreferences = useSCPreferences();
    // CONTEXT
    const scContext = useSCContext();
    const scUserContext = useSCUser();
    // HOOKS
    const { scUser, setSCUser } = useSCFetchUser({ id: userId, user });
    // CONST
    const isMe = useMemo(() => scUserContext.user && (scUser === null || scUser === void 0 ? void 0 : scUser.id) === scUserContext.user.id, [scUserContext.user, scUser]);
    /**
     * Handles Change Avatar
     * Only if scUser.id === scUserContext.user.id
     * If avatar === null reset the avatar with auto-generated image
     * @param avatar
     */
    function handleChangeAvatar(avatar) {
        if (scUser.id === scUserContext.user.id && avatar) {
            setSCUser(Object.assign({}, scUser, { avatar: avatar.avatar }));
        }
        else {
            setSCUser(Object.assign({}, scUser, { avatar: `${scContext.settings.portal}/api/v2/avatar/${scUser.id}` }));
        }
    }
    /**
     * Handles Change Cover
     * Only if scUser.id === scUserContext.user.id
     * @param cover
     */
    function handleChangeCover(cover) {
        if (scUser.id === scUserContext.user.id) {
            setSCUser(Object.assign({}, scUser, { cover: cover }));
        }
    }
    // RENDER
    if (!scUser) {
        return _jsx(UserProfileHeaderSkeleton, {});
    }
    const _backgroundCover = Object.assign({}, (scUser.cover
        ? { background: `url('${scUser.cover}') center / cover` }
        : { background: `url('${scPreferences.preferences[SCPreferences.IMAGES_USER_DEFAULT_COVER].value}') center / cover` }));
    const realName = (isMe && scUserContext.user.real_name) || scUser.real_name;
    const hasBadge = scUser && scUser.community_badge;
    return (_jsxs(Root, Object.assign({ id: id, className: classNames(classes.root, className) }, rest, { children: [_jsxs(Paper, Object.assign({ style: _backgroundCover, classes: { root: classes.cover } }, { children: [_jsx(Box, Object.assign({ className: classes.avatar }, { children: _jsx(UserAvatar, Object.assign({ hide: !hasBadge }, { children: _jsx("img", { src: scUser.avatar ? scUser.avatar : '' }) })) })), isMe && (_jsxs(_Fragment, { children: [_jsx(ChangePicture, Object.assign({ iconButton: true, onChange: handleChangeAvatar, className: classes.changePicture }, ChangePictureProps)), _jsx("div", Object.assign({ className: classes.changeCover }, { children: _jsx(ChangeCover, Object.assign({ onChange: handleChangeCover }, ChangeCoverProps)) }))] }))] })), _jsxs(Box, Object.assign({ className: classes.infOpsSection }, { children: [_jsxs(Box, Object.assign({ className: classes.info }, { children: [_jsxs(Typography, Object.assign({ variant: "h5", className: classes.username }, { children: ["@", isMe ? scUserContext.user.username : scUser.username] })), realName && (_jsx(Typography, Object.assign({ variant: "subtitle1", className: classes.realname }, { children: realName }))), scUser.bio && (_jsx(Typography, Object.assign({ variant: "subtitle2", className: classes.bio }, { children: scUser.bio }))), scUser.website && (_jsx(Link, Object.assign({ className: classes.website, target: "blank", to: scUser.website }, { children: _jsx(Typography, Object.assign({ variant: "body2" }, { children: scUser.website })) })))] })), actions && actions] }))] })));
}
