import { __rest } from "tslib";
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { styled } from '@mui/material/styles';
import { Box, Button, Grid, TextField, Typography, useMediaQuery, useTheme } from '@mui/material';
import { Endpoints, GroupService, http } from '@selfcommunity/api-services';
import { Logger, sortByAttr } from '@selfcommunity/utils';
import { SCPreferences, useSCPreferences, useSCUser } from '@selfcommunity/react-core';
import Skeleton from './Skeleton';
import { FormattedMessage } from 'react-intl';
import classNames from 'classnames';
import { SCOPE_SC_UI } from '../../constants/Errors';
import { useThemeProps } from '@mui/system';
import HiddenPlaceholder from '../../shared/HiddenPlaceholder';
import { PREFIX } from './constants';
import Group, { GroupSkeleton } from '../Group';
import { DEFAULT_PAGINATION_OFFSET } from '../../constants/Pagination';
import InfiniteScroll from '../../shared/InfiniteScroll';
import PubSub from 'pubsub-js';
import { SCGroupEventType, SCTopicType } from '../../constants/PubSub';
const classes = {
    root: `${PREFIX}-root`,
    filters: `${PREFIX}-filter`,
    groups: `${PREFIX}-groups`,
    item: `${PREFIX}-item`,
    noResults: `${PREFIX}-no-results`,
    showMore: `${PREFIX}-show-more`,
    endMessage: `${PREFIX}-end-message`
};
const Root = styled(Box, {
    name: PREFIX,
    slot: 'Root'
})(() => ({}));
/**
 * > API documentation for the Community-JS Groups component. Learn about the available props and the CSS API.
 *
 *
 * This component renders the list of the follows of the given group.
 * Take a look at our <strong>demo</strong> component [here](/docs/sdk/community-js/react-ui/Components/Groups)

 #### Import

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

 #### Component Name

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


 #### CSS

 |Rule Name|Global class|Description|
 |---|---|---|
 |root|.SCGroups-root|Styles applied to the root element.|
 |title|.SCGroups-title|Styles applied to the title element.|
 |noResults|.SCGroups-no-results|Styles applied to no results section.|
 |showMore|.SCGroups-show-more|Styles applied to show more button element.|
 |dialogRoot|.SCGroups-dialog-root|Styles applied to the dialog root element.|
 |endMessage|.SCGroups-end-message|Styles applied to the end message element.|

 * @param inProps
 */
export default function Groups(inProps) {
    // PROPS
    const props = useThemeProps({
        props: inProps,
        name: PREFIX
    });
    const { endpointQueryParams = { limit: 20, offset: DEFAULT_PAGINATION_OFFSET }, className, GroupComponentProps = { variant: 'outlined', ButtonBaseProps: { disableRipple: true, component: Box } }, showFilters = true, filters, general = true } = props, rest = __rest(props, ["endpointQueryParams", "className", "GroupComponentProps", "showFilters", "filters", "general"]);
    // STATE
    const [groups, setGroups] = useState([]);
    const [loading, setLoading] = useState(true);
    const [next, setNext] = useState(null);
    const [search, setSearch] = useState('');
    const theme = useTheme();
    const isMobile = useMediaQuery(theme.breakpoints.down('md'));
    // CONTEXT
    const scUserContext = useSCUser();
    const scPreferencesContext = useSCPreferences();
    const onlyStaffEnabled = useMemo(() => scPreferencesContext.preferences[SCPreferences.CONFIGURATIONS_GROUPS_ONLY_STAFF_ENABLED].value, [scPreferencesContext.preferences]);
    // MEMO
    const contentAvailability = useMemo(() => SCPreferences.CONFIGURATIONS_CONTENT_AVAILABILITY in scPreferencesContext.preferences &&
        scPreferencesContext.preferences[SCPreferences.CONFIGURATIONS_CONTENT_AVAILABILITY].value, [scPreferencesContext.preferences]);
    // CONST
    const authUserId = scUserContext.user ? scUserContext.user.id : null;
    // REFS
    const updatesSubscription = useRef(null);
    // HANDLERS
    const handleScrollUp = () => {
        window.scrollTo({ left: 0, top: 0, behavior: 'smooth' });
    };
    /**
     * Fetches groups list
     */
    const fetchGroups = () => {
        let groupService;
        if (general) {
            groupService = GroupService.searchGroups(Object.assign(Object.assign({}, endpointQueryParams), (search !== '' && { search: search })));
        }
        else {
            groupService = GroupService.getUserGroups(Object.assign(Object.assign({}, endpointQueryParams), (search !== '' && { search: search })));
        }
        groupService
            .then((res) => {
            setGroups(res.results);
            setNext(res.next);
            setLoading(false);
        })
            .catch((error) => {
            Logger.error(SCOPE_SC_UI, error);
        });
    };
    /**
     * On mount, fetches groups list
     */
    useEffect(() => {
        if (!contentAvailability && !authUserId) {
            return;
        }
        else {
            fetchGroups();
        }
    }, [contentAvailability, authUserId, search]);
    /**
     * Subscriber for pubsub callback
     */
    const onDeleteGroupHandler = useCallback((_msg, deleted) => {
        setGroups((prev) => {
            if (prev.some((e) => e.id === deleted)) {
                return prev.filter((e) => e.id !== deleted);
            }
            return prev;
        });
    }, [groups]);
    /**
     * On mount, subscribe to receive event updates (only delete)
     */
    useEffect(() => {
        if (groups) {
            updatesSubscription.current = PubSub.subscribe(`${SCTopicType.GROUP}.${SCGroupEventType.DELETE}`, onDeleteGroupHandler);
        }
        return () => {
            updatesSubscription.current && PubSub.unsubscribe(updatesSubscription.current);
        };
    }, [groups]);
    const handleNext = useMemo(() => () => {
        if (!next) {
            return;
        }
        return http
            .request({
            url: next,
            method: general ? Endpoints.SearchGroups.method : Endpoints.GetUserGroups.method
        })
            .then((res) => {
            setGroups([...groups, ...res.data.results]);
            setNext(res.data.next);
        })
            .catch((error) => console.log(error))
            .then(() => setLoading(false));
    }, [next]);
    /**
     * Get groups filtered
     */
    const getFilteredGroups = () => {
        if (search) {
            return groups.filter((g) => g.name.toLowerCase().includes(search.toLowerCase()));
        }
        return groups;
    };
    /**
     * Handle change filter name
     * @param event
     */
    const handleOnChangeFilterName = (event) => {
        setSearch(event.target.value);
    };
    /**
     * Renders groups list
     */
    const filteredGroups = sortByAttr(getFilteredGroups(), 'order');
    const content = (_jsxs(_Fragment, { children: [showFilters && groups.length !== 0 && (_jsx(Grid, Object.assign({ container: true, direction: "row", justifyContent: "center", alignItems: "center", className: classes.filters }, { children: filters ? (filters) : (_jsx(Grid, Object.assign({ item: true, xs: 12, md: 6 }, { children: _jsx(TextField, { fullWidth: true, value: search, label: _jsx(FormattedMessage, { id: "ui.groups.filterByName", defaultMessage: "ui.groups.filterByName" }), variant: "outlined", onChange: handleOnChangeFilterName, disabled: loading }) }))) }))), _jsx(_Fragment, { children: !groups.length ? (_jsx(Box, Object.assign({ className: classes.noResults }, { children: !onlyStaffEnabled ? (_jsxs(_Fragment, { children: [_jsx(Typography, Object.assign({ variant: "h4" }, { children: _jsx(FormattedMessage, { id: "ui.groups.noGroups.title", defaultMessage: "ui.groups.noGroups.title" }) })), _jsx(Typography, Object.assign({ variant: "body1" }, { children: _jsx(FormattedMessage, { id: "ui.groups.noGroups.subtitle", defaultMessage: "ui.groups.noGroups.subtitle" }) }))] })) : (_jsxs(_Fragment, { children: [_jsx(Typography, Object.assign({ variant: "h4" }, { children: _jsx(FormattedMessage, { id: "ui.groups.noGroups.title.onlyStaff", defaultMessage: "ui.groups.noGroups.title.onlyStaff" }) })), _jsx(Typography, Object.assign({ variant: "body1" }, { children: _jsx(FormattedMessage, { id: "ui.groups.noGroups.subtitle.onlyStaff", defaultMessage: "ui.groups.noGroups.subtitle.onlyStaff" }) }))] })) }))) : (_jsx(InfiniteScroll, Object.assign({ dataLength: groups.length, next: handleNext, hasMoreNext: Boolean(next), loaderNext: isMobile ? _jsx(GroupSkeleton, {}) : _jsx(Skeleton, { groupsNumber: 2 }), endMessage: _jsx(Typography, Object.assign({ component: "div", className: classes.endMessage }, { children: _jsx(FormattedMessage, { id: "ui.groups.endMessage", defaultMessage: "ui.groups.endMessage", values: {
                                // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
                                // @ts-ignore
                                button: (chunk) => (_jsx(Button, Object.assign({ color: "secondary", variant: "text", onClick: handleScrollUp }, { children: chunk })))
                            } }) })) }, { children: _jsx(Grid, Object.assign({ container: true, spacing: { xs: 2 }, className: classes.groups }, { children: filteredGroups.map((group) => (_jsx(Grid, Object.assign({ item: true, xs: 12, sm: 8, md: 6, className: classes.item }, { children: _jsx(Group, Object.assign({ group: group, groupId: group.id, actionRedirect: true }, GroupComponentProps)) }), group.id))) })) }))) })] }));
    // RENDER
    if (!contentAvailability && !scUserContext.user) {
        return _jsx(HiddenPlaceholder, {});
    }
    if (loading) {
        return _jsx(Skeleton, {});
    }
    return (_jsx(Root, Object.assign({ className: classNames(classes.root, className) }, rest, { children: content })));
}
