"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = tslib_1.__importStar(require("react"));
const react_intl_1 = require("react-intl");
const Autocomplete_1 = tslib_1.__importDefault(require("@mui/material/Autocomplete"));
const TextField_1 = tslib_1.__importDefault(require("@mui/material/TextField"));
const CircularProgress_1 = tslib_1.__importDefault(require("@mui/material/CircularProgress"));
const parse_1 = tslib_1.__importDefault(require("autosuggest-highlight/parse"));
const match_1 = tslib_1.__importDefault(require("autosuggest-highlight/match"));
const material_1 = require("@mui/material");
const react_core_1 = require("@selfcommunity/react-core");
const styles_1 = require("@mui/material/styles");
const system_1 = require("@mui/system");
const PREFIX = 'SCGroupAutocomplete';
const classes = {
    root: `${PREFIX}-root`
};
const Root = (0, styles_1.styled)(Autocomplete_1.default, {
    name: PREFIX,
    slot: 'Root',
    overridesResolver: (props, styles) => styles.root
})(() => ({}));
/**
 * > API documentation for the Community-JS Group Autocomplete component. Learn about the available props and the CSS API.
 *
 *
 * This component renders a bar that allows users to search (with autocomplete) for all the groups available in the application.
 *
 * #### Import
 *  ```jsx
 *  import {GroupAutocomplete} from '@selfcommunity/react-ui';
 *  ```
 *  #### Component Name
 *  The name `SCGroupAutocomplete` can be used when providing style overrides in the theme.
 *
 *  #### CSS
 *
 *  |Rule Name|Global class|Description|
 *  |---|---|---|
 *  |root|.SCGroupAutocomplete-root|Styles applied to the root element.|
 *
 * @param inProps
 */
const GroupAutocomplete = (inProps) => {
    const props = (0, system_1.useThemeProps)({
        props: inProps,
        name: PREFIX
    });
    // Props
    const { onChange, defaultValue = null, disabled = false, TextFieldProps = {
        variant: 'outlined',
        label: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.groupAutocomplete.label", defaultMessage: "ui.groupAutocomplete.label" })
    } } = props, rest = tslib_1.__rest(props, ["onChange", "defaultValue", "disabled", "TextFieldProps"]);
    // State
    const [open, setOpen] = (0, react_1.useState)(false);
    const [value, setValue] = (0, react_1.useState)(typeof defaultValue === 'string' ? null : defaultValue);
    // HOOKS
    const { groups, isLoading } = (0, react_core_1.useSCFetchGroups)();
    (0, react_1.useEffect)(() => {
        if (value === null) {
            return;
        }
        onChange && onChange(value);
    }, [value]);
    (0, react_1.useEffect)(() => {
        if (!isLoading && typeof defaultValue === 'string') {
            setValue(groups.find((g) => g.id === Number(defaultValue)));
        }
    }, [isLoading]);
    // Handlers
    const handleOpen = () => {
        setOpen(true);
    };
    const handleClose = () => {
        setOpen(false);
    };
    const handleChange = (event, value) => {
        setValue(value);
    };
    // Render
    return ((0, jsx_runtime_1.jsx)(Root, Object.assign({ className: classes.root, open: open, onOpen: handleOpen, onClose: handleClose, options: groups || [], getOptionLabel: (option) => option.name || '', value: value, selectOnFocus: true, clearOnBlur: true, blurOnSelect: true, handleHomeEndKeys: true, clearIcon: null, disabled: disabled || isLoading, noOptionsText: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.groupAutocomplete.empty", defaultMessage: "ui.groupAutocomplete.empty" }), onChange: handleChange, isOptionEqualToValue: (option, value) => value.id === option.id, 
        // renderTags={(value, getTagProps) => {
        //   return value.map((option: any, index) => (
        //     <Chip key={option.id} id={option.id} label={option.name} color={option.color} {...getTagProps({index})} />
        //   ));
        // }}
        renderOption: (props, option, { inputValue }) => {
            const matches = (0, match_1.default)(option.name, inputValue);
            const parts = (0, parse_1.default)(option.name, matches);
            return ((0, jsx_runtime_1.jsxs)(material_1.Box, Object.assign({ component: "li" }, props, { children: [(0, jsx_runtime_1.jsx)(material_1.Avatar, { alt: option.name, src: option.image_small, sx: { marginRight: 1 } }), (0, jsx_runtime_1.jsx)(react_1.default.Fragment, { children: parts.map((part, index) => ((0, jsx_runtime_1.jsx)(material_1.Typography, Object.assign({ sx: { fontWeight: part.highlight ? 700 : 400, marginRight: 0.2 } }, { children: part.text }), index))) })] })));
        }, renderInput: (params) => {
            return ((0, jsx_runtime_1.jsx)(TextField_1.default, Object.assign({}, params, TextFieldProps, { margin: "dense", InputProps: Object.assign(Object.assign({}, params.InputProps), { autoComplete: 'groups', endAdornment: ((0, jsx_runtime_1.jsxs)(react_1.default.Fragment, { children: [isLoading ? (0, jsx_runtime_1.jsx)(CircularProgress_1.default, { color: "inherit", size: 20 }) : null, params.InputProps.endAdornment] })) }) })));
        } }, rest)));
};
exports.default = GroupAutocomplete;
