"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const styles_1 = require("@mui/material/styles");
const material_1 = require("@mui/material");
const ChangeCover_1 = tslib_1.__importDefault(require("../ChangeCover"));
const ChangePicture_1 = tslib_1.__importDefault(require("../ChangePicture"));
const react_core_1 = require("@selfcommunity/react-core");
const Skeleton_1 = tslib_1.__importDefault(require("./Skeleton"));
const classnames_1 = tslib_1.__importDefault(require("classnames"));
const system_1 = require("@mui/system");
const constants_1 = require("./constants");
const UserAvatar_1 = tslib_1.__importDefault(require("../../shared/UserAvatar"));
const classes = {
    root: `${constants_1.PREFIX}-root`,
    cover: `${constants_1.PREFIX}-cover`,
    avatar: `${constants_1.PREFIX}-avatar`,
    info: `${constants_1.PREFIX}-info`,
    infOpsSection: `${constants_1.PREFIX}-infops-section`,
    username: `${constants_1.PREFIX}-username`,
    realname: `${constants_1.PREFIX}-realname`,
    bio: `${constants_1.PREFIX}-bio`,
    website: `${constants_1.PREFIX}-website`,
    changePicture: `${constants_1.PREFIX}-change-picture`,
    changeCover: `${constants_1.PREFIX}-change-cover`
};
const Root = (0, styles_1.styled)(material_1.Box, {
    name: constants_1.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
 */
function UserProfileHeader(inProps) {
    // PROPS
    const props = (0, system_1.useThemeProps)({
        props: inProps,
        name: constants_1.PREFIX
    });
    const { id = null, className = null, userId = null, user = null, ChangePictureProps = {}, ChangeCoverProps = {}, actions } = props, rest = tslib_1.__rest(props, ["id", "className", "userId", "user", "ChangePictureProps", "ChangeCoverProps", "actions"]);
    // PREFERENCES
    const scPreferences = (0, react_core_1.useSCPreferences)();
    // CONTEXT
    const scContext = (0, react_core_1.useSCContext)();
    const scUserContext = (0, react_core_1.useSCUser)();
    // HOOKS
    const { scUser, setSCUser } = (0, react_core_1.useSCFetchUser)({ id: userId, user });
    // CONST
    const isMe = (0, react_1.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 (0, jsx_runtime_1.jsx)(Skeleton_1.default, {});
    }
    const _backgroundCover = Object.assign({}, (scUser.cover
        ? { background: `url('${scUser.cover}') center / cover` }
        : { background: `url('${scPreferences.preferences[react_core_1.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 ((0, jsx_runtime_1.jsxs)(Root, Object.assign({ id: id, className: (0, classnames_1.default)(classes.root, className) }, rest, { children: [(0, jsx_runtime_1.jsxs)(material_1.Paper, Object.assign({ style: _backgroundCover, classes: { root: classes.cover } }, { children: [(0, jsx_runtime_1.jsx)(material_1.Box, Object.assign({ className: classes.avatar }, { children: (0, jsx_runtime_1.jsx)(UserAvatar_1.default, Object.assign({ hide: !hasBadge }, { children: (0, jsx_runtime_1.jsx)("img", { src: scUser.avatar ? scUser.avatar : '' }) })) })), isMe && ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(ChangePicture_1.default, Object.assign({ iconButton: true, onChange: handleChangeAvatar, className: classes.changePicture }, ChangePictureProps)), (0, jsx_runtime_1.jsx)("div", Object.assign({ className: classes.changeCover }, { children: (0, jsx_runtime_1.jsx)(ChangeCover_1.default, Object.assign({ onChange: handleChangeCover }, ChangeCoverProps)) }))] }))] })), (0, jsx_runtime_1.jsxs)(material_1.Box, Object.assign({ className: classes.infOpsSection }, { children: [(0, jsx_runtime_1.jsxs)(material_1.Box, Object.assign({ className: classes.info }, { children: [(0, jsx_runtime_1.jsxs)(material_1.Typography, Object.assign({ variant: "h5", className: classes.username }, { children: ["@", isMe ? scUserContext.user.username : scUser.username] })), realName && ((0, jsx_runtime_1.jsx)(material_1.Typography, Object.assign({ variant: "subtitle1", className: classes.realname }, { children: realName }))), scUser.bio && ((0, jsx_runtime_1.jsx)(material_1.Typography, Object.assign({ variant: "subtitle2", className: classes.bio }, { children: scUser.bio }))), scUser.website && ((0, jsx_runtime_1.jsx)(react_core_1.Link, Object.assign({ className: classes.website, target: "blank", to: scUser.website }, { children: (0, jsx_runtime_1.jsx)(material_1.Typography, Object.assign({ variant: "body2" }, { children: scUser.website })) })))] })), actions && actions] }))] })));
}
exports.default = UserProfileHeader;
