'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _tslib = require('../../_virtual/_tslib.js');
var React = require('react');
var CloseCircleFilled = require('../../icons/CloseCircleFilled.js');
var index = require('../../utils/index.js');
var index$1 = require('../Spinner/index.js');
var styles = require('./styles.js');
var utils = require('./utils.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var React__default = /*#__PURE__*/_interopDefaultLegacy(React);

function Chip(_a) {
    var { id, className, label, value, variant, icon, disabled = false, isLoading = false, selected = false, showClose = false, onClick, onRemove } = _a, props = _tslib.__rest(_a, ["id", "className", "label", "value", "variant", "icon", "disabled", "isLoading", "selected", "showClose", "onClick", "onRemove"]);
    const ChipComponent = utils.getChipVariant(variant);
    const [width, setWidth] = React.useState(0);
    const ref = React.useRef(null);
    React.useEffect(() => {
        if (ref.current)
            setWidth(ref.current.offsetWidth);
    }, [ref, variant, selected, label]);
    function handleCloseClick(event) {
        event.stopPropagation();
        if (!disabled && !isLoading && onRemove) {
            onRemove(value || label);
        }
        else {
            onClick(value || label);
        }
    }
    function handleClick() {
        if (selected && showClose)
            return;
        if (!isLoading && !disabled)
            onClick(value || label);
    }
    const appendTestId = index.getId(props['data-testid']);
    return (React__default['default'].createElement(ChipComponent, { id: id || label, className: className || label, ref: ref, onClick: handleClick, disabled: disabled, selected: selected, showClose: showClose, isLoading: isLoading, "data-testid": appendTestId(), "data-trackid": props['data-trackid'], "aria-label": props['aria-label'] || label, "aria-selected": selected }, isLoading && !disabled ? (React__default['default'].createElement(styles.SpinnerWrapper, { width: width, selected: selected },
        React__default['default'].createElement(index$1.Spinner, { size: "XS", variant: selected ? 'inverse' : 'secondary' }))) : (React__default['default'].createElement(React__default['default'].Fragment, null,
        icon && React__default['default'].createElement(styles.IconWrapper, null, icon),
        label,
        selected && showClose && (React__default['default'].createElement(styles.CloseButton, { "aria-label": "close-button", selected: selected, disabled: disabled, onClick: handleCloseClick, "data-testid": appendTestId('close-button') },
            React__default['default'].createElement(CloseCircleFilled['default'], null)))))));
}

exports.Chip = Chip;
