import React, { Fragment } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { SoftButtonIcon } from '../ButtonIcon';
import { SoftIcon } from '../Icon';
import { TYPES as ICON_TYPES } from '../Icon/SoftIcon';
import { SoftToolTip } from '../Tooltip';
const sizeToClassNameMap = {
    xs: 'softListGroupItem--xSmall',
    s: 'softListGroupItem--small',
    m: 'softListGroupItem--medium',
    l: 'softListGroupItem--large',
};
export const SIZES = Object.keys(sizeToClassNameMap);
export const SoftListGroupItem = ({ label, isActive, isDisabled, href, className, iconType, icon, extraAction, onClick, size, showToolTip, wrapText, ...rest }) => {
    const classes = classNames('softListGroupItem', sizeToClassNameMap[size], {
        'softListGroupItem-isActive': isActive,
        'softListGroupItem-isDisabled': isDisabled,
        'softListGroupItem-isClickable': href || onClick,
        'softListGroupItem-hasExtraAction': extraAction,
        'softListGroupItem--wrapText': wrapText,
    }, className);
    let iconNode;
    if (iconType) {
        iconNode = (React.createElement(SoftIcon, { className: "softListGroupItem__icon", type: iconType }));
        if (icon) {
            console.warn('Both `iconType` and `icon` were passed to SoftListGroupItem but only one can exist. The `iconType` was used.');
        }
    }
    else if (icon) {
        iconNode = React.cloneElement(icon, { className: classNames('softListGroupItem__icon', icon.props.className) });
    }
    let extraActionNode;
    if (extraAction) {
        const { iconType, alwaysShow, ...rest } = extraAction;
        const extraActionClasses = classNames('softListGroupItem__extraAction', { 'softListGroupItem__extraAction-alwaysShow': alwaysShow });
        extraActionNode = (React.createElement(SoftButtonIcon, Object.assign({ className: extraActionClasses, iconType: iconType }, rest, { disabled: isDisabled })));
    }
    // Only add the label as the title attribute if it's possibly truncated
    const labelContent = (React.createElement("span", { className: "softListGroupItem__label", title: wrapText ? undefined : label }, label));
    // Handle the variety of interaction behavior
    let itemContent;
    if (href && !isDisabled) {
        itemContent = (React.createElement("a", Object.assign({ href: href, className: "softListGroupItem__button" }, rest),
            iconNode,
            labelContent));
        if (onClick) {
            console.warn('Both `href` and `onClick` were passed to SoftListGroupItem but only one can exist. The `href` was used.');
        }
    }
    else if ((href && isDisabled) || onClick) {
        itemContent = (React.createElement("button", Object.assign({ type: "button", className: "softListGroupItem__button", disabled: isDisabled, onClick: onClick }, rest),
            iconNode,
            labelContent));
    }
    else {
        itemContent = (React.createElement("span", Object.assign({ className: "softListGroupItem__text" }, rest),
            iconNode,
            labelContent));
    }
    if (showToolTip) {
        itemContent = (React.createElement("li", { className: classes },
            React.createElement(SoftToolTip, { anchorClassName: "softListGroupItem__tooltip", content: label, position: "right", delay: "long", size: "s" }, itemContent)));
    }
    else {
        itemContent = (React.createElement("li", { className: classes },
            itemContent,
            extraActionNode));
    }
    return (React.createElement(Fragment, null, itemContent));
};
// @ts-ignore
SoftListGroupItem.propTypes = {
    className: PropTypes.string,
    /**
     * Set the size of the label text
     */
    size: PropTypes.oneOf(SIZES),
    /**
     * Content to be displyed in the list item
     */
    label: PropTypes.node.isRequired,
    /**
     * Apply styles indicating an item is active
     */
    isActive: PropTypes.bool,
    /**
     * Apply styles indicating an item is disabled
     */
    isDisabled: PropTypes.bool,
    /**
     * Make the list item label a link
     */
    href: PropTypes.string,
    /**
     * Adds `SoftIcon` of `SoftIcon.type`
     */
    iconType: PropTypes.oneOf(ICON_TYPES),
    /**
     * Custom node to pass as the icon. Cannot be used in conjunction
     * with `iconType`.
     */
    icon: PropTypes.element,
    /**
     * Display tooltip on list item
     */
    showToolTip: PropTypes.bool,
    /**
     * Adds an `SoftButtonIcon` to the right side of the item; `iconType` is required;
     * pass `alwaysShow` if you don't want the default behavior of only showing on hover
     */
    extraAction: PropTypes.shape({
        iconType: PropTypes.oneOf(ICON_TYPES).isRequired,
        alwaysShow: PropTypes.bool,
    }),
    onClick: PropTypes.func,
    /**
     * Allow link text to wrap
     */
    wrapText: PropTypes.bool,
};
// @ts-ignore
SoftListGroupItem.defaultProps = {
    isActive: false,
    isDisabled: false,
    size: 'm',
    showToolTip: false,
};
//# sourceMappingURL=SoftListGroupItem.js.map