import { __rest } from "tslib";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import React, { useEffect, useState } from 'react';
import { FormattedMessage } from 'react-intl';
import Autocomplete from '@mui/material/Autocomplete';
import TextField from '@mui/material/TextField';
import CircularProgress from '@mui/material/CircularProgress';
import parse from 'autosuggest-highlight/parse';
import match from 'autosuggest-highlight/match';
import { Avatar, Box, Typography } from '@mui/material';
import { useSCFetchEvents } from '@selfcommunity/react-core';
import { styled } from '@mui/material/styles';
import { useThemeProps } from '@mui/system';
const PREFIX = 'SCEventAutocomplete';
const classes = {
    root: `${PREFIX}-root`
};
const Root = styled(Autocomplete, {
    name: PREFIX,
    slot: 'Root',
    overridesResolver: (props, styles) => styles.root
})(() => ({}));
/**
 * > API documentation for the Community-JS Event 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 events available in the application.
 *
 * #### Import
 *  ```jsx
 *  import {EventAutocomplete} from '@selfcommunity/react-ui';
 *  ```
 *  #### Component Name
 *  The name `SCEventAutocomplete` can be used when providing style overrides in the theme.
 *
 *  #### CSS
 *
 *  |Rule Name|Global class|Description|
 *  |---|---|---|
 *  |root|.SCEventAutocomplete-root|Styles applied to the root element.|
 *
 * @param inProps
 */
const EventAutocomplete = (inProps) => {
    const props = useThemeProps({
        props: inProps,
        name: PREFIX
    });
    // Props
    const { onChange, defaultValue = null, disabled = false, TextFieldProps = {
        variant: 'outlined',
        label: _jsx(FormattedMessage, { id: "ui.eventAutocomplete.label", defaultMessage: "ui.eventAutocomplete.label" })
    } } = props, rest = __rest(props, ["onChange", "defaultValue", "disabled", "TextFieldProps"]);
    // State
    const [open, setOpen] = useState(false);
    const [value, setValue] = useState(typeof defaultValue === 'string' ? null : defaultValue);
    // HOOKS
    const { events, isLoading } = useSCFetchEvents();
    useEffect(() => {
        if (value === null) {
            return;
        }
        onChange && onChange(value);
    }, [value]);
    useEffect(() => {
        if (!isLoading && typeof defaultValue === 'string') {
            setValue(events.find((e) => e.id === Number(defaultValue)));
        }
    }, [isLoading]);
    // Handlers
    const handleOpen = () => {
        setOpen(true);
    };
    const handleClose = () => {
        setOpen(false);
    };
    const handleChange = (e, value) => {
        setValue(value);
    };
    // Render
    return (_jsx(Root, Object.assign({ className: classes.root, open: open, onOpen: handleOpen, onClose: handleClose, options: events || [], getOptionLabel: (option) => option.name || '', value: value, selectOnFocus: true, clearOnBlur: true, blurOnSelect: true, handleHomeEndKeys: true, clearIcon: null, disabled: disabled || isLoading, noOptionsText: _jsx(FormattedMessage, { id: "ui.eventAutocomplete.empty", defaultMessage: "ui.eventAutocomplete.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 = match(option.name, inputValue);
            const parts = parse(option.name, matches);
            return (_jsxs(Box, Object.assign({ component: "li" }, props, { children: [_jsx(Avatar, { alt: option.name, src: option.image_small, sx: { marginRight: 1 } }), _jsx(React.Fragment, { children: parts.map((part, index) => (_jsx(Typography, Object.assign({ sx: { fontWeight: part.highlight ? 700 : 400, marginRight: 0.2 } }, { children: part.text }), index))) })] })));
        }, renderInput: (params) => {
            return (_jsx(TextField, Object.assign({}, params, TextFieldProps, { margin: "dense", InputProps: Object.assign(Object.assign({}, params.InputProps), { autoComplete: 'events', endAdornment: (_jsxs(React.Fragment, { children: [isLoading ? _jsx(CircularProgress, { color: "inherit", size: 20 }) : null, params.InputProps.endAdornment] })) }) })));
        } }, rest)));
};
export default EventAutocomplete;
