import { __rest } from "tslib";
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { useMemo, useState } from 'react';
import { styled } from '@mui/material/styles';
import { Avatar, Button, Icon, Stack, useMediaQuery, useTheme } from '@mui/material';
import { SCGroupPrivacyType, SCGroupSubscriptionStatusType } from '@selfcommunity/types';
import { Link, SCRoutes, useSCFetchGroup, useSCRouting, useSCUser } from '@selfcommunity/react-core';
import { defineMessages, FormattedMessage, useIntl } from 'react-intl';
import classNames from 'classnames';
import { useThemeProps } from '@mui/system';
import BaseItemButton from '../../shared/BaseItemButton';
import UserDeletedSnackBar from '../../shared/UserDeletedSnackBar';
import { PREFIX } from './constants';
import GroupSkeleton from './Skeleton';
import GroupSubscribeButton from '../GroupSubscribeButton';
import { formatCroppedName } from '../../utils/string';
import { GROUP_NAME_MAX_LENGTH_DESKTOP, GROUP_NAME_MAX_LENGTH_MOBILE } from '../../constants/Group';
const messages = defineMessages({
    groupMembers: {
        id: 'ui.group.members',
        defaultMessage: 'ui.group.members'
    }
});
const classes = {
    root: `${PREFIX}-root`,
    avatar: `${PREFIX}-avatar`,
    actions: `${PREFIX}-actions`,
    icon: `${PREFIX}-icon`
};
const Root = styled(BaseItemButton, {
    name: 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
 */
export default function Group(inProps) {
    var _a;
    // PROPS
    const props = useThemeProps({
        props: inProps,
        name: PREFIX
    });
    const { groupId = null, group = null, className = null, elevation, hideActions = false, actionRedirect = false, groupSubscribeButtonProps = {} } = props, rest = __rest(props, ["groupId", "group", "className", "elevation", "hideActions", "actionRedirect", "groupSubscribeButtonProps"]);
    // STATE
    const { scGroup } = useSCFetchGroup({ id: groupId, group });
    // CONTEXT
    const scRoutingContext = useSCRouting();
    const scUserContext = useSCUser();
    // CONST
    const isGroupAdmin = 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] = useState(false);
    // HOOKS
    const intl = useIntl();
    const theme = useTheme();
    const isMobile = useMediaQuery(theme.breakpoints.down('md'));
    /**
     * Render authenticated actions
     * @return {JSX.Element}
     */
    function renderAuthenticatedActions() {
        return (_jsxs(Stack, Object.assign({ className: classes.actions, direction: "row", alignItems: "center", justifyContent: "center", spacing: 2 }, { children: [isGroupAdmin && _jsx(Icon, { children: "face" }), actionRedirect ? (_jsx(Button, Object.assign({ size: "small", variant: "outlined", component: Link, to: scRoutingContext.url(SCRoutes.GROUP_ROUTE_NAME, scGroup) }, { children: scGroup.subscription_status === SCGroupSubscriptionStatusType.SUBSCRIBED ? (_jsx(FormattedMessage, { defaultMessage: "ui.group.status.enter", id: "ui.group.status.enter" })) : (_jsx(FormattedMessage, { defaultMessage: "ui.group.status.discover", id: "ui.group.status.discover" })) }))) : (_jsx(GroupSubscribeButton, Object.assign({ group: group, groupId: groupId }, groupSubscribeButtonProps)))] })));
    }
    /**
     * Renders group object
     */
    if (!scGroup) {
        return _jsx(GroupSkeleton, { elevation: elevation });
    }
    /**
     * Renders root object
     */
    return (_jsxs(_Fragment, { children: [_jsx(Root, Object.assign({ elevation: elevation }, rest, { className: classNames(classes.root, className), ButtonBaseProps: { component: Link, to: scRoutingContext.url(SCRoutes.GROUP_ROUTE_NAME, scGroup) }, image: _jsx(Avatar, { alt: scGroup.name, src: scGroup.image_medium, className: classes.avatar }), primary: _jsxs(_Fragment, { children: [isMobile
                            ? formatCroppedName(scGroup.name, GROUP_NAME_MAX_LENGTH_MOBILE)
                            : formatCroppedName(scGroup.name, GROUP_NAME_MAX_LENGTH_DESKTOP), ' ', _jsx(Icon, Object.assign({ className: classes.icon }, { children: (group === null || group === void 0 ? void 0 : group.privacy) === SCGroupPrivacyType.PRIVATE ? 'private' : 'public' }))] }), secondary: `${intl.formatMessage(messages.groupMembers, { total: scGroup.subscribers_counter })}`, actions: hideActions ? null : renderAuthenticatedActions() })), openAlert && _jsx(UserDeletedSnackBar, { open: openAlert, handleClose: () => setOpenAlert(false) })] }));
}
