"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 types_1 = require("@selfcommunity/types");
const react_core_1 = require("@selfcommunity/react-core");
const react_intl_1 = require("react-intl");
const classnames_1 = tslib_1.__importDefault(require("classnames"));
const system_1 = require("@mui/system");
const BaseItemButton_1 = tslib_1.__importDefault(require("../../shared/BaseItemButton"));
const UserDeletedSnackBar_1 = tslib_1.__importDefault(require("../../shared/UserDeletedSnackBar"));
const constants_1 = require("./constants");
const Skeleton_1 = tslib_1.__importDefault(require("./Skeleton"));
const GroupSubscribeButton_1 = tslib_1.__importDefault(require("../GroupSubscribeButton"));
const string_1 = require("../../utils/string");
const Group_1 = require("../../constants/Group");
const messages = (0, react_intl_1.defineMessages)({
    groupMembers: {
        id: 'ui.group.members',
        defaultMessage: 'ui.group.members'
    }
});
const classes = {
    root: `${constants_1.PREFIX}-root`,
    avatar: `${constants_1.PREFIX}-avatar`,
    actions: `${constants_1.PREFIX}-actions`,
    icon: `${constants_1.PREFIX}-icon`
};
const Root = (0, styles_1.styled)(BaseItemButton_1.default, {
    name: constants_1.PREFIX,
    slot: 'Root',
    overridesResolver: (props, styles) => styles.root
})(({ theme }) => ({}));
/**
 * > API documentation for the Community-JS Group component. Learn about the available props and the CSS API.
 *
 *
 * This component renders a group item.
 * Take a look at our <strong>demo</strong> component [here](/docs/sdk/community-js/react-ui/Components/Group)

 #### Import

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

 #### Component Name

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


 #### CSS

 |Rule Name|Global class|Description|
 |---|---|---|
 |root|.SCGroup-root|Styles applied to the root element.|
 |avatar|.SCGroup-avatar|Styles applied to the avatar element.|
 |actions|.SCGroup-actions|Styles applied to the actions section.|
 |icon|.SCGroup-icon|Styles applied to the group privacy icon element.|


 *
 * @param inProps
 */
function Group(inProps) {
    var _a;
    // PROPS
    const props = (0, system_1.useThemeProps)({
        props: inProps,
        name: constants_1.PREFIX
    });
    const { groupId = null, group = null, className = null, elevation, hideActions = false, actionRedirect = false, groupSubscribeButtonProps = {} } = props, rest = tslib_1.__rest(props, ["groupId", "group", "className", "elevation", "hideActions", "actionRedirect", "groupSubscribeButtonProps"]);
    // STATE
    const { scGroup } = (0, react_core_1.useSCFetchGroup)({ id: groupId, group });
    // CONTEXT
    const scRoutingContext = (0, react_core_1.useSCRouting)();
    const scUserContext = (0, react_core_1.useSCUser)();
    // CONST
    const isGroupAdmin = (0, react_1.useMemo)(() => { var _a; return scUserContext.user && ((_a = scGroup === null || scGroup === void 0 ? void 0 : scGroup.managed_by) === null || _a === void 0 ? void 0 : _a.id) === scUserContext.user.id; }, [scUserContext.user, (_a = scGroup === null || scGroup === void 0 ? void 0 : scGroup.managed_by) === null || _a === void 0 ? void 0 : _a.id]);
    const [openAlert, setOpenAlert] = (0, react_1.useState)(false);
    // HOOKS
    const intl = (0, react_intl_1.useIntl)();
    const theme = (0, material_1.useTheme)();
    const isMobile = (0, material_1.useMediaQuery)(theme.breakpoints.down('md'));
    /**
     * Render authenticated actions
     * @return {JSX.Element}
     */
    function renderAuthenticatedActions() {
        return ((0, jsx_runtime_1.jsxs)(material_1.Stack, Object.assign({ className: classes.actions, direction: "row", alignItems: "center", justifyContent: "center", spacing: 2 }, { children: [isGroupAdmin && (0, jsx_runtime_1.jsx)(material_1.Icon, { children: "face" }), actionRedirect ? ((0, jsx_runtime_1.jsx)(material_1.Button, Object.assign({ size: "small", variant: "outlined", component: react_core_1.Link, to: scRoutingContext.url(react_core_1.SCRoutes.GROUP_ROUTE_NAME, scGroup) }, { children: scGroup.subscription_status === types_1.SCGroupSubscriptionStatusType.SUBSCRIBED ? ((0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { defaultMessage: "ui.group.status.enter", id: "ui.group.status.enter" })) : ((0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { defaultMessage: "ui.group.status.discover", id: "ui.group.status.discover" })) }))) : ((0, jsx_runtime_1.jsx)(GroupSubscribeButton_1.default, Object.assign({ group: group, groupId: groupId }, groupSubscribeButtonProps)))] })));
    }
    /**
     * Renders group object
     */
    if (!scGroup) {
        return (0, jsx_runtime_1.jsx)(Skeleton_1.default, { elevation: elevation });
    }
    /**
     * Renders root object
     */
    return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(Root, Object.assign({ elevation: elevation }, rest, { className: (0, classnames_1.default)(classes.root, className), ButtonBaseProps: { component: react_core_1.Link, to: scRoutingContext.url(react_core_1.SCRoutes.GROUP_ROUTE_NAME, scGroup) }, image: (0, jsx_runtime_1.jsx)(material_1.Avatar, { alt: scGroup.name, src: scGroup.image_medium, className: classes.avatar }), primary: (0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [isMobile
                            ? (0, string_1.formatCroppedName)(scGroup.name, Group_1.GROUP_NAME_MAX_LENGTH_MOBILE)
                            : (0, string_1.formatCroppedName)(scGroup.name, Group_1.GROUP_NAME_MAX_LENGTH_DESKTOP), ' ', (0, jsx_runtime_1.jsx)(material_1.Icon, Object.assign({ className: classes.icon }, { children: (group === null || group === void 0 ? void 0 : group.privacy) === types_1.SCGroupPrivacyType.PRIVATE ? 'private' : 'public' }))] }), secondary: `${intl.formatMessage(messages.groupMembers, { total: scGroup.subscribers_counter })}`, actions: hideActions ? null : renderAuthenticatedActions() })), openAlert && (0, jsx_runtime_1.jsx)(UserDeletedSnackBar_1.default, { open: openAlert, handleClose: () => setOpenAlert(false) })] }));
}
exports.default = Group;
