import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { getSecureRelForTarget } from '../../services';
import { SoftIcon, } from '../Icon';
import { SIZES as ICON_SIZES, TYPES as ICON_TYPES } from '../Icon/SoftIcon';
const accessibleButtonIcon = (props, propName, componentName) => {
    if (props['aria-label']) {
        return;
    }
    if (props['aria-labelledby']) {
        return;
    }
    throw new Error(`${componentName} requires aria-label or aria-labelledby to be specified because icon-only
    buttons are screen-reader-inaccessible without them.`);
};
const colorToClassNameMap = {
    danger: 'softButtonIcon--danger',
    disabled: 'softButtonIcon--disabled',
    ghost: 'softButtonIcon--ghost',
    primary: 'softButtonIcon--primary',
    subdued: 'softButtonIcon--subdued',
    success: 'softButtonIcon--success',
    text: 'softButtonIcon--text',
    warning: 'softButtonIcon--warning',
};
export const COLORS = Object.keys(colorToClassNameMap);
export const SoftButtonIcon = ({ className, iconType, iconSize, color, isDisabled, href, type, target, rel, buttonRef, ...rest }) => {
    const classes = classNames('softButtonIcon', colorToClassNameMap[color], className);
    // Add an icon to the button if one exists.
    let buttonIcon;
    if (iconType) {
        buttonIcon = (React.createElement(SoftIcon, { className: "softButtonIcon__icon", type: iconType, size: iconSize, "aria-hidden": "true" }));
    }
    // <a> elements don't respect the `disabled` attribute. So if we're disabled, we'll just pretend
    // this is a button and piggyback off its disabled styles.
    if (href && !isDisabled) {
        const secureRel = getSecureRelForTarget({ href, target, rel });
        return (React.createElement("a", Object.assign({ className: classes, href: href, target: target, rel: secureRel, ref: buttonRef }, rest), buttonIcon));
    }
    else {
        return (React.createElement("button", Object.assign({ disabled: isDisabled, className: classes, type: type, ref: buttonRef }, rest), buttonIcon));
    }
};
// @ts-ignore
SoftButtonIcon.propTypes = {
    children: PropTypes.node,
    className: PropTypes.string,
    iconType: PropTypes.oneOf(ICON_TYPES),
    iconSize: PropTypes.oneOf(ICON_SIZES),
    color: PropTypes.oneOf(COLORS),
    isDisabled: PropTypes.bool,
    'aria-label': accessibleButtonIcon,
    href: PropTypes.string,
    target: PropTypes.string,
    rel: PropTypes.string,
    onClick: PropTypes.func,
    type: PropTypes.string,
    buttonRef: PropTypes.func,
};
// @ts-ignore
SoftButtonIcon.defaultProps = {
    type: 'button',
    color: 'primary',
    iconSize: 'm',
};
//# sourceMappingURL=SoftButtonIcon.js.map