import { __awaiter, __rest } from "tslib";
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import { useCallback, useContext, useEffect, useRef, useState } from 'react';
import { styled } from '@mui/material/styles';
import { Box, Grid, TextField, Typography } from '@mui/material';
import { CategoryService, Endpoints } from '@selfcommunity/api-services';
import { SCPreferences, SCPreferencesContext, SCUserContext, useIsComponentMountedRef } from '@selfcommunity/react-core';
import CategoriesSkeleton from './Skeleton';
import Category from '../Category';
import { FormattedMessage } from 'react-intl';
import classNames from 'classnames';
import { useThemeProps } from '@mui/system';
import { SCOPE_SC_UI } from '../../constants/Errors';
import { Logger, sortByAttr } from '@selfcommunity/utils';
import HiddenPlaceholder from '../../shared/HiddenPlaceholder';
import { PREFIX } from './constants';
import PubSub from 'pubsub-js';
import { SCCategoryEventType, SCTopicType } from '../../constants/PubSub';
const classes = {
    root: `${PREFIX}-root`,
    filters: `${PREFIX}-filter`,
    categories: `${PREFIX}-categories`,
    category: `${PREFIX}-category`,
    noResults: `${PREFIX}-no-results`
};
const Root = styled(Box, {
    name: PREFIX,
    slot: 'Root'
})(() => ({}));
/**
 * > API documentation for the Community-JS Categories component. Learn about the available props and the CSS API.
 *
 *
 * The Categories component renders the list of all available categories.
 * Take a look at our <strong>demo</strong> component [here](/docs/sdk/community-js/react-ui/Components/AccountRecover)

 #### Import
 ```jsx
 import {Categories} from '@selfcommunity/react-ui';
 ```
 #### Component Name
 The name `SCCategories` can be used when providing style overrides in the theme.

 #### CSS

 |Rule Name|Global class|Description|
 |---|---|---|
 |root|.SCCategories-root|Styles applied to the root element.|
 |filters|.SCCategories-filter|Styles applied to the filter.|
 |categories|.SCCategories-categories|Styles applied to the list of categories.|
 |category|.SCCategories-category|Styles applied to the of category element.|
 |noResults|.SCCategories-no-results|Styles applied to no results section.|

 * @param inProps
 */
export default function Categories(inProps) {
    // PROPS
    const props = useThemeProps({
        props: inProps,
        name: PREFIX
    });
    const { className, CategoryComponent = Category, CategoryComponentProps = { variant: 'outlined', ButtonBaseProps: { disableRipple: true, component: Box } }, CategoriesSkeletonComponent = CategoriesSkeleton, CategoriesSkeletonProps = {}, showFilters = false, filters, handleFilterCategories, prefetchedCategories = [] } = props, rest = __rest(props, ["className", "CategoryComponent", "CategoryComponentProps", "CategoriesSkeletonComponent", "CategoriesSkeletonProps", "showFilters", "filters", "handleFilterCategories", "prefetchedCategories"]);
    // STATE
    const [categories, setCategories] = useState([]);
    const [loading, setLoading] = useState(true);
    const [filterName, setFilterName] = useState('');
    // CONTEXT
    const scUserContext = useContext(SCUserContext);
    const scPreferencesContext = useContext(SCPreferencesContext);
    const contentAvailability = SCPreferences.CONFIGURATIONS_CONTENT_AVAILABILITY in scPreferencesContext.preferences &&
        scPreferencesContext.preferences[SCPreferences.CONFIGURATIONS_CONTENT_AVAILABILITY].value;
    // CONST
    const authUserId = scUserContext.user ? scUserContext.user.id : null;
    // REFS
    const isMountedRef = useIsComponentMountedRef();
    const updatesSubscription = useRef(null);
    /**
     * Fetches categories list
     */
    const fetchCategories = (next = Endpoints.CategoryList.url({})) => __awaiter(this, void 0, void 0, function* () {
        const data = yield CategoryService.getAllCategories({ active: true }, { url: next });
        return data.next ? data.results.concat(yield fetchCategories(data.next)) : data.results;
    });
    /**
     * On mount, fetches categories list
     */
    useEffect(() => {
        if (!contentAvailability && !authUserId) {
            return;
        }
        else if (prefetchedCategories.length) {
            setCategories(prefetchedCategories);
            setLoading(false);
        }
        else {
            fetchCategories()
                .then((data) => {
                if (isMountedRef.current) {
                    setCategories(data);
                    setLoading(false);
                }
            })
                .catch((error) => {
                Logger.error(SCOPE_SC_UI, error);
            });
        }
    }, [contentAvailability, authUserId, prefetchedCategories.length]);
    /**
     * Subscriber for pubsub callback
     */
    const onEditCategoryHandler = useCallback((_msg, edited) => {
        setCategories((prev) => {
            return prev.map((c) => (c.id === edited.id ? Object.assign(Object.assign({}, c), edited) : c));
        });
    }, [categories]);
    /**
     * On mount, subscribe to receive event updates (only edit)
     */
    useEffect(() => {
        if (categories) {
            updatesSubscription.current = PubSub.subscribe(`${SCTopicType.CATEGORY}.${SCCategoryEventType.EDIT}`, onEditCategoryHandler);
        }
        return () => {
            updatesSubscription.current && PubSub.unsubscribe(updatesSubscription.current);
        };
    }, [categories]);
    /**
     * Get categories filtered
     */
    const getFilteredCategories = () => {
        if (handleFilterCategories) {
            return handleFilterCategories(categories);
        }
        if (filterName) {
            return categories.filter((c) => c.name.toLowerCase().includes(filterName.toLowerCase()));
        }
        return categories;
    };
    /**
     * Handle change filter name
     * @param event
     */
    const handleOnChangeFilterName = (event) => {
        setFilterName(event.target.value);
    };
    /**
     * Renders categories list
     */
    const filteredCategories = sortByAttr(getFilteredCategories(), 'order');
    const c = (_jsxs(_Fragment, { children: [showFilters && (_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: filterName, label: _jsx(FormattedMessage, { id: "ui.categories.filterByName", defaultMessage: "ui.categories.filterByName" }), variant: "outlined", onChange: handleOnChangeFilterName, disabled: loading }) }))) }))), loading ? (_jsx(CategoriesSkeletonComponent, Object.assign({}, CategoriesSkeletonProps))) : (_jsx(Grid, Object.assign({ container: true, spacing: { xs: 3 }, className: classes.categories }, { children: !filteredCategories.length ? (_jsx(Grid, Object.assign({ item: true }, { children: _jsx(Typography, Object.assign({ className: classes.noResults, variant: "body2" }, { children: _jsx(FormattedMessage, { id: "ui.categories.noResults", defaultMessage: "ui.categories.noResults" }) })) }))) : (_jsx(_Fragment, { children: filteredCategories.map((category) => (_jsx(Grid, Object.assign({ item: true, xs: 12, sm: 6, md: 6, lg: 4 }, { children: _jsx(CategoryComponent, Object.assign({ category: category }, CategoryComponentProps, { showTooltip: true, className: classes.category })) }), category.id))) })) })))] }));
    /**
     * Renders root object (if content availability community option is false and user is anonymous, component is hidden)
     */
    if (!contentAvailability && !scUserContext.user) {
        return _jsx(HiddenPlaceholder, {});
    }
    return (_jsx(Root, Object.assign({ className: classNames(classes.root, className) }, rest, { children: c })));
}
