"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 Box_1 = tslib_1.__importDefault(require("@mui/material/Box"));
const system_1 = require("@mui/system");
const classnames_1 = tslib_1.__importDefault(require("classnames"));
const constants_1 = require("../../constants");
const material_1 = require("@mui/material");
const react_intl_1 = require("react-intl");
const PublicInfo_1 = tslib_1.__importDefault(require("../../../UserProfileEdit/Section/PublicInfo"));
const react_core_1 = require("@selfcommunity/react-core");
const UserAvatar_1 = tslib_1.__importDefault(require("../../../../shared/UserAvatar"));
const ChangePicture_1 = tslib_1.__importDefault(require("../../../ChangePicture"));
const ChangeCover_1 = tslib_1.__importDefault(require("../../../ChangeCover"));
const UserProfile_1 = require("../../../../constants/UserProfile");
const Icon_1 = tslib_1.__importDefault(require("@mui/material/Icon"));
const classes = {
    root: `${constants_1.PREFIX}-profile-root`,
    title: `${constants_1.PREFIX}-profile-title`,
    icon: `${constants_1.PREFIX}-profile-icon`,
    cover: `${constants_1.PREFIX}-profile-cover`,
    avatar: `${constants_1.PREFIX}-profile-avatar`,
    changePicture: `${constants_1.PREFIX}-profile-change-picture`,
    changeCover: `${constants_1.PREFIX}-profile-change-cover`,
    publicInfo: `${constants_1.PREFIX}-profile-public-info`
};
const Root = (0, styles_1.styled)(Box_1.default, {
    name: constants_1.PREFIX,
    slot: 'ProfileRoot'
})(() => ({}));
const DrawerRoot = (0, styles_1.styled)(material_1.Drawer, {
    name: constants_1.PREFIX,
    slot: 'ProfileDrawerRoot'
})(() => ({}));
function Profile(inProps) {
    var _a;
    // PROPS
    const props = (0, system_1.useThemeProps)({
        props: inProps,
        name: constants_1.PREFIX
    });
    const { className, fields = [...UserProfile_1.DEFAULT_FIELDS], user = null, onCompleteAction } = props, rest = tslib_1.__rest(props, ["className", "fields", "user", "onCompleteAction"]);
    // STATE
    const [anchorEl, setAnchorEl] = (0, react_1.useState)(null);
    // 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: (_a = scUserContext === null || scUserContext === void 0 ? void 0 : scUserContext.user) === null || _a === void 0 ? void 0 : _a.id, user });
    const hasBadge = scUser && scUser.community_badge;
    // HANDLERS
    const handleOpen = (0, react_1.useCallback)((event) => {
        setAnchorEl(event.currentTarget);
    }, []);
    const handleClose = (0, react_1.useCallback)(() => {
        setAnchorEl(null);
    }, []);
    /**
     * Handles Change Avatar
     * If avatar === null reset the avatar with auto-generated image
     * @param avatar
     */
    function handleChangeAvatar(avatar) {
        if (avatar) {
            setSCUser(Object.assign({}, scUser, { avatar: avatar.avatar }));
        }
        else {
            setSCUser(Object.assign({}, scUser, { avatar: `${scContext.settings.portal}/api/v2/avatar/${scUser.id}` }));
        }
        onCompleteAction();
    }
    const _backgroundCover = Object.assign({}, ((scUser === null || scUser === void 0 ? void 0 : scUser.cover)
        ? { background: `url('${scUser.cover}') center / cover` }
        : { background: `url('${scPreferences.preferences[react_core_1.SCPreferences.IMAGES_USER_DEFAULT_COVER].value}') center / cover` }));
    /**
     * Handles Change Cover
     * Only if scUser.id === scUserContext.user.id
     * @param cover
     */
    function handleChangeCover(cover) {
        setSCUser(Object.assign({}, scUser, { cover: cover }));
        onCompleteAction();
    }
    return ((0, jsx_runtime_1.jsxs)(Root, Object.assign({ className: (0, classnames_1.default)(classes.root, className) }, rest, { children: [(0, jsx_runtime_1.jsx)(material_1.Typography, Object.assign({ variant: "h4", className: classes.title, alignSelf: "self-start" }, { children: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.onBoardingWidget.profile", defaultMessage: "ui.onBoardingWidget.profile" }) })), (0, jsx_runtime_1.jsx)(material_1.Typography, Object.assign({ alignSelf: "self-start" }, { children: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.onBoardingWidget.step.profile.summary", defaultMessage: "ui.onBoardingWidget.step.profile.summary" }) })), (0, jsx_runtime_1.jsx)(material_1.Button, Object.assign({ variant: "outlined", size: "small", color: "primary", onClick: handleOpen }, { children: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.onBoardingWidget.step.profile.button", defaultMessage: "ui.onBoardingWidget.step.profile.button" }) })), (0, jsx_runtime_1.jsxs)(DrawerRoot, Object.assign({ anchor: "right", open: Boolean(anchorEl), onClose: handleClose }, { children: [(0, jsx_runtime_1.jsx)(material_1.IconButton, Object.assign({ className: classes.icon, onClick: handleClose }, { children: (0, jsx_runtime_1.jsx)(Icon_1.default, { children: "close" }) })), (0, jsx_runtime_1.jsxs)(material_1.Paper, Object.assign({ style: _backgroundCover, classes: { root: classes.cover } }, { children: [(0, jsx_runtime_1.jsx)(Box_1.default, 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 === null || scUser === void 0 ? void 0 : scUser.avatar) ? scUser.avatar : '' }) })) })), (0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(ChangePicture_1.default, { iconButton: true, onChange: handleChangeAvatar, className: classes.changePicture }), (0, jsx_runtime_1.jsx)("div", Object.assign({ className: classes.changeCover }, { children: (0, jsx_runtime_1.jsx)(ChangeCover_1.default, { onChange: handleChangeCover }) }))] })] })), (0, jsx_runtime_1.jsx)(Box_1.default, Object.assign({ className: classes.publicInfo }, { children: (0, jsx_runtime_1.jsx)(PublicInfo_1.default, { fields: fields, onEditSuccess: onCompleteAction }) }))] }))] })));
}
exports.default = Profile;
