"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TagsComponentType = void 0;
const tslib_1 = require("tslib");
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = tslib_1.__importStar(require("react"));
const styles_1 = require("@mui/material/styles");
const ClickAwayListener_1 = tslib_1.__importDefault(require("@mui/material/ClickAwayListener"));
const material_1 = require("@mui/material");
const Grow_1 = tslib_1.__importDefault(require("@mui/material/Grow"));
const Paper_1 = tslib_1.__importDefault(require("@mui/material/Paper"));
const Popper_1 = tslib_1.__importDefault(require("@mui/material/Popper"));
const Icon_1 = tslib_1.__importDefault(require("@mui/material/Icon"));
const TagChip_1 = tslib_1.__importDefault(require("../TagChip"));
const PREFIX = 'SCTags';
const TagsPopperRoot = (0, styles_1.styled)(material_1.Box, {
    name: `${PREFIX}`,
    slot: 'Root',
    overridesResolver: (props, styles) => styles.root
})(() => ({
    cursor: 'pointer'
}));
/**
 * Custom Popper
 */
const classes = {
    paper: `${PREFIX}-paper`
};
const TagsPopper = (0, styles_1.styled)(Popper_1.default, {
    name: `${PREFIX}Popper`,
    slot: 'Root',
    overridesResolver: (props, styles) => styles.root
})(() => ({
    zIndex: 2,
    [`& .${classes.paper}`]: {
        padding: '10px 5px'
    }
}));
/**
 * Custom StackList and ItemList
 */
const ListRoot = (0, styles_1.styled)(material_1.Stack, {
    name: `${PREFIX}List`,
    slot: 'Root',
    overridesResolver: (props, styles) => styles.root
})({});
const StackList = (0, styles_1.styled)(material_1.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 = (0, styles_1.styled)(material_1.Box, {
    name: `${PREFIX}ItemList`,
    slot: 'Root',
    overridesResolver: (props, styles) => styles.root
})(({ theme }) => ({
    padding: theme.spacing(0.2),
    textAlign: 'center',
    color: theme.palette.text.secondary
}));
var TagsComponentType;
(function (TagsComponentType) {
    TagsComponentType["LIST"] = "list";
    TagsComponentType["POPPER"] = "popper";
})(TagsComponentType = exports.TagsComponentType || (exports.TagsComponentType = {}));
function Tags(props) {
    // PROPS
    const { tags = [], title = null, type = TagsComponentType.POPPER, onOpen = null, onClose = null, onClickTag = null, TagChipProps = {} } = props, rest = tslib_1.__rest(props, ["tags", "title", "type", "onOpen", "onClose", "onClickTag", "TagChipProps"]);
    // STATE
    const [open, setOpen] = (0, react_1.useState)(false);
    let popperRef = (0, react_1.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 ((0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: title && ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(material_1.Typography, Object.assign({ sx: { padding: 1, fontWeight: 600 } }, { children: title })), (0, jsx_runtime_1.jsx)(material_1.Divider, {})] })) }));
    }
    /**
     * Returns focus to the button when we transitioned from !open -> open
     */
    const prevOpen = (0, react_1.useRef)(open);
    react_1.default.useEffect(() => {
        if (prevOpen.current === true && open === false) {
            popperRef.current.focus();
        }
        prevOpen.current = open;
    }, [open]);
    /**
     * Renders component
     */
    return ((0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: tags.length && ((0, jsx_runtime_1.jsx)(react_1.default.Fragment, { children: type === TagsComponentType.POPPER ? ((0, jsx_runtime_1.jsxs)(TagsPopperRoot, Object.assign({}, rest, { children: [(0, jsx_runtime_1.jsx)(Icon_1.default, Object.assign({ ref: popperRef, fontSize: "small", onClick: handleToggle, "aria-haspopup": "true" }, { children: "label" })), (0, jsx_runtime_1.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 = tslib_1.__rest(_a, ["TransitionProps", "placement"]);
                            return ((0, jsx_runtime_1.jsx)(Grow_1.default, Object.assign({}, TransitionProps, { style: { transformOrigin: placement === 'bottom' ? 'center top' : 'center bottom' } }, { children: (0, jsx_runtime_1.jsx)(Paper_1.default, Object.assign({ variant: 'outlined', className: classes.paper }, { children: (0, jsx_runtime_1.jsx)(ClickAwayListener_1.default, Object.assign({ onClickAway: handleClose }, { children: (0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [renderTitle(), (0, jsx_runtime_1.jsx)(StackList, Object.assign({ spacing: 2 }, rest, { children: tags.map((tag) => ((0, jsx_runtime_1.jsx)(ItemList, { children: (0, jsx_runtime_1.jsx)(TagChip_1.default, Object.assign({ tag: tag, onClick: onClickTag }, TagChipProps)) }, tag.id))) }))] }) })) })) })));
                        } }))] }))) : ((0, jsx_runtime_1.jsxs)(ListRoot, { children: [renderTitle(), (0, jsx_runtime_1.jsx)(StackList, Object.assign({ spacing: 1, direction: rest.direction ? rest.direction : 'column' }, { children: tags.map((tag) => ((0, jsx_runtime_1.jsx)(ItemList, { children: (0, jsx_runtime_1.jsx)(TagChip_1.default, Object.assign({ tag: tag, onClick: onClickTag }, TagChipProps)) }, tag.id))) }))] })) })) }));
}
exports.default = Tags;
