"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 parse_1 = tslib_1.__importDefault(require("autosuggest-highlight/parse"));
const match_1 = tslib_1.__importDefault(require("autosuggest-highlight/match"));
const react_core_1 = require("@selfcommunity/react-core");
const styles_1 = require("@mui/material/styles");
const system_1 = require("@mui/system");
const TagChip_1 = tslib_1.__importDefault(require("../../shared/TagChip"));
const PREFIX = 'SCTagAutocomplete';
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 Tag 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 tags available in the application.
 *
 * #### Import
 *  ```jsx
 *  import {TagAutocomplete} from '@selfcommunity/react-ui';
 *  ```
 *  #### Component Name
 *  The name `SCTagAutocomplete` can be used when providing style overrides in the theme.
 *
 *  #### CSS
 *
 *  |Rule Name|Global class|Description|
 *  |---|---|---|
 *  |root|.SCTagAutocomplete-root|Styles applied to the root element.|
 *
 * @param inProps
 */
const TagAutocomplete = (inProps) => {
    const props = (0, system_1.useThemeProps)({
        props: inProps,
        name: PREFIX
    });
    // Props
    const { onChange, defaultValue = null, TextFieldProps = {
        variant: 'outlined',
        label: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.composer.layer.audience.tags.label", defaultMessage: "ui.composer.layer.audience.tags.label" })
    }, TagChipProps = {} } = props, rest = tslib_1.__rest(props, ["onChange", "defaultValue", "TextFieldProps", "TagChipProps"]);
    // State
    const [open, setOpen] = (0, react_1.useState)(false);
    const [value, setValue] = (0, react_1.useState)(typeof defaultValue === 'string' ? null : defaultValue);
    // HOOKS
    const { scAddressingTags } = (0, react_core_1.useSCFetchAddressingTagList)({ fetch: open || defaultValue });
    (0, react_1.useEffect)(() => {
        if (value === null) {
            return;
        }
        onChange && onChange(value);
    }, [value]);
    (0, react_1.useEffect)(() => {
        if ((scAddressingTags === null || scAddressingTags === void 0 ? void 0 : scAddressingTags.length) !== 0 && typeof defaultValue === 'string') {
            setValue(scAddressingTags.find((t) => t.id === Number(defaultValue)));
        }
    }, [scAddressingTags, defaultValue]);
    // Handlers
    const handleOpen = () => {
        setOpen(true);
    };
    const handleClose = () => {
        setOpen(false);
    };
    const handleChange = (event, newValue) => {
        setValue(newValue);
    };
    // Render
    return ((0, jsx_runtime_1.jsx)(Root, Object.assign({ className: classes.root, open: open, onOpen: handleOpen, onClose: handleClose, options: scAddressingTags || [], getOptionLabel: (option) => option.name || '', value: value, selectOnFocus: true, clearOnBlur: true, handleHomeEndKeys: true, clearIcon: null, noOptionsText: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.composer.layer.audience.tags.empty", defaultMessage: "ui.composer.layer.audience.tags.empty" }), onChange: handleChange, isOptionEqualToValue: (option, value) => (value === null || value === void 0 ? void 0 : value.id) === (option === null || option === void 0 ? void 0 : option.id), renderTags: (value, getTagProps) => {
            return value.map((option, index) => (0, jsx_runtime_1.jsx)(TagChip_1.default, Object.assign({ tag: option }, getTagProps({ index })), option.id));
        }, renderOption: (props, option, { selected, inputValue }) => {
            const matches = (0, match_1.default)(option.name, inputValue);
            const parts = (0, parse_1.default)(option.name, matches);
            return ((0, jsx_runtime_1.jsx)("li", Object.assign({}, props, { children: (0, jsx_runtime_1.jsx)(TagChip_1.default, Object.assign({ tag: option, label: (0, jsx_runtime_1.jsx)(react_1.default.Fragment, { children: parts.map((part, index) => ((0, jsx_runtime_1.jsx)("span", Object.assign({ style: { fontWeight: part.highlight ? 700 : 400 } }, { children: part.text }), index))) }) }, TagChipProps), option.id) })));
        }, renderInput: (params) => {
            return ((0, jsx_runtime_1.jsx)(TextField_1.default, Object.assign({}, params, TextFieldProps, { InputProps: Object.assign(Object.assign({}, params.InputProps), { autoComplete: 'addressing', endAdornment: (0, jsx_runtime_1.jsx)(react_1.default.Fragment, { children: params.InputProps.endAdornment }) }) })));
        } }, rest)));
};
exports.default = TagAutocomplete;
