import { __rest } from "tslib";
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import React, { useRef, useState } from 'react';
import { styled } from '@mui/material/styles';
import ClickAwayListener from '@mui/material/ClickAwayListener';
import { Box, Divider, Stack, Typography } from '@mui/material';
import Grow from '@mui/material/Grow';
import Paper from '@mui/material/Paper';
import Popper from '@mui/material/Popper';
import Icon from '@mui/material/Icon';
import TagChip from '../TagChip';
const PREFIX = 'SCTags';
const TagsPopperRoot = styled(Box, {
    name: `${PREFIX}`,
    slot: 'Root',
    overridesResolver: (props, styles) => styles.root
})(() => ({
    cursor: 'pointer'
}));
/**
 * Custom Popper
 */
const classes = {
    paper: `${PREFIX}-paper`
};
const TagsPopper = styled(Popper, {
    name: `${PREFIX}Popper`,
    slot: 'Root',
    overridesResolver: (props, styles) => styles.root
})(() => ({
    zIndex: 2,
    [`& .${classes.paper}`]: {
        padding: '10px 5px'
    }
}));
/**
 * Custom StackList and ItemList
 */
const ListRoot = styled(Stack, {
    name: `${PREFIX}List`,
    slot: 'Root',
    overridesResolver: (props, styles) => styles.root
})({});
const StackList = styled(Stack, {
    name: `${PREFIX}Stack`,
    slot: 'Root',
    overridesResolver: (props, styles) => styles.root
})({
    flexWrap: 'wrap',
    justifyContent: 'flex-start',
    alignItems: 'flex-start',
    '& > div:not(style)+:not(style)': {
        margin: 0
    }
});
const ItemList = styled(Box, {
    name: `${PREFIX}ItemList`,
    slot: 'Root',
    overridesResolver: (props, styles) => styles.root
})(({ theme }) => ({
    padding: theme.spacing(0.2),
    textAlign: 'center',
    color: theme.palette.text.secondary
}));
export var TagsComponentType;
(function (TagsComponentType) {
    TagsComponentType["LIST"] = "list";
    TagsComponentType["POPPER"] = "popper";
})(TagsComponentType || (TagsComponentType = {}));
export default function Tags(props) {
    // PROPS
    const { tags = [], title = null, type = TagsComponentType.POPPER, onOpen = null, onClose = null, onClickTag = null, TagChipProps = {} } = props, rest = __rest(props, ["tags", "title", "type", "onOpen", "onClose", "onClickTag", "TagChipProps"]);
    // STATE
    const [open, setOpen] = useState(false);
    let popperRef = useRef(null);
    // HANDLERS
    function handleToggle() {
        setOpen((prevOpen) => !prevOpen);
        if (rest.onOpen) {
            rest.onOpen(!open);
        }
    }
    function handleClose() {
        if (popperRef.current && popperRef.current.contains(event.target)) {
            return;
        }
        setOpen(false);
        if (rest.onClose) {
            rest.onClose();
        }
    }
    /**
     * Renders tag title
     */
    function renderTitle() {
        return (_jsx(_Fragment, { children: title && (_jsxs(_Fragment, { children: [_jsx(Typography, Object.assign({ sx: { padding: 1, fontWeight: 600 } }, { children: title })), _jsx(Divider, {})] })) }));
    }
    /**
     * Returns focus to the button when we transitioned from !open -> open
     */
    const prevOpen = useRef(open);
    React.useEffect(() => {
        if (prevOpen.current === true && open === false) {
            popperRef.current.focus();
        }
        prevOpen.current = open;
    }, [open]);
    /**
     * Renders component
     */
    return (_jsx(_Fragment, { children: tags.length && (_jsx(React.Fragment, { children: type === TagsComponentType.POPPER ? (_jsxs(TagsPopperRoot, Object.assign({}, rest, { children: [_jsx(Icon, Object.assign({ ref: popperRef, fontSize: "small", onClick: handleToggle, "aria-haspopup": "true" }, { children: "label" })), _jsx(TagsPopper, Object.assign({ open: open, anchorEl: popperRef.current, transition: true, placement: "bottom-start", modifiers: [
                            {
                                name: 'preventOverflow',
                                enabled: true
                            }
                        ] }, { children: (_a) => {
                            var { TransitionProps, placement } = _a, rest = __rest(_a, ["TransitionProps", "placement"]);
                            return (_jsx(Grow, Object.assign({}, TransitionProps, { style: { transformOrigin: placement === 'bottom' ? 'center top' : 'center bottom' } }, { children: _jsx(Paper, Object.assign({ variant: 'outlined', className: classes.paper }, { children: _jsx(ClickAwayListener, Object.assign({ onClickAway: handleClose }, { children: _jsxs(_Fragment, { children: [renderTitle(), _jsx(StackList, Object.assign({ spacing: 2 }, rest, { children: tags.map((tag) => (_jsx(ItemList, { children: _jsx(TagChip, Object.assign({ tag: tag, onClick: onClickTag }, TagChipProps)) }, tag.id))) }))] }) })) })) })));
                        } }))] }))) : (_jsxs(ListRoot, { children: [renderTitle(), _jsx(StackList, Object.assign({ spacing: 1, direction: rest.direction ? rest.direction : 'column' }, { children: tags.map((tag) => (_jsx(ItemList, { children: _jsx(TagChip, Object.assign({ tag: tag, onClick: onClickTag }, TagChipProps)) }, tag.id))) }))] })) })) }));
}
