"use strict";
/* @flow */
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const React = __importStar(require("react"));
{
    ButtonProps;
}
from;
'./types';
const prop_types_1 = __importDefault(require("prop-types"));
const react_router_dom_1 = require("react-router-dom");
const classnames_1 = __importDefault(require("classnames"));
const withStyles_1 = __importDefault(require("components/hoc/withStyles"));
const ErrorBoundary_1 = __importDefault(require("components/hoc/ErrorBoundary"));
const ButtonBase_1 = __importDefault(require("@material-ui/core/ButtonBase"));
const helpers_1 = require("components/helpers");
const styles_1 = __importDefault(require("./styles"));
class Button extends React.Component {
    constructor() {
        super(...arguments);
        this._renderAfterIcon = () => {
            let { iconAfter, classes, loading, loadingIcon } = this.props;
            let AfterIconComponent = null;
            if (loading) {
                if (loadingIcon) {
                    AfterIconComponent = loadingIcon;
                }
                else {
                    AfterIconComponent = React.createElement("i", { className: "fa fa-pulse fa-spinner" });
                }
            }
            else if (iconAfter) {
                AfterIconComponent = iconAfter;
            }
            else {
                return null;
            }
            return (React.createElement("div", { className: classnames_1.default(classes.icon, classes.afterIcon) }, AfterIconComponent));
        };
    }
    _renderChildren() {
        let { iconBefore, title, classes } = this.props;
        return (React.createElement(React.Fragment, null,
            iconBefore && (React.createElement("div", { className: classnames_1.default(classes.icon, classes.beforeIcon) }, iconBefore)),
            title && React.createElement("div", { className: classnames_1.default(classes.text) }, title),
            this._renderAfterIcon()));
    }
    _renderContainer() {
        let { id, onClick, isDisabled, type, href, classes, target, loading, } = this.props;
        isDisabled = isDisabled || loading;
        let buttonClass = classnames_1.default(classes.component, {
            disabled: isDisabled,
        });
        if (href) {
            let isExternal = helpers_1.isExternalUrl(href);
            if (isExternal) {
                // render <a> tag
                return (React.createElement(ButtonBase_1.default, { component: 'a', classes: { root: buttonClass }, target: target ? target : '_blank', id: id, href: href }, this._renderChildren()));
            }
            return (React.createElement(ButtonBase_1.default, { component: () => (React.createElement(react_router_dom_1.Link, { to: href, className: buttonClass, target: target, id: id }, this._renderChildren())) }));
        }
        return (React.createElement(ButtonBase_1.default, { component: "button", classes: { root: buttonClass }, id: id, type: type, onClick: onClick }, this._renderChildren()));
    }
    render() {
        return React.createElement(ErrorBoundary_1.default, null, this._renderContainer());
    }
}
Button.propTypes = {
    isDisabled: prop_types_1.default.bool,
    title: prop_types_1.default.node,
    type: prop_types_1.default.string,
    iconBefore: prop_types_1.default.node,
    iconAfter: prop_types_1.default.node,
    onClick: prop_types_1.default.func,
    id: prop_types_1.default.string,
    height: prop_types_1.default.oneOfType([prop_types_1.default.string, prop_types_1.default.number]),
    width: prop_types_1.default.oneOfType([prop_types_1.default.string, prop_types_1.default.number]),
    maxWidth: prop_types_1.default.oneOfType([prop_types_1.default.string, prop_types_1.default.number]),
    buttonColor: prop_types_1.default.string,
    href: prop_types_1.default.string,
    applyBorderRadius: prop_types_1.default.bool,
    compactMode: prop_types_1.default.bool,
    withoutBackground: prop_types_1.default.bool,
    target: prop_types_1.default.string,
    loading: prop_types_1.default.bool,
    loadingIcon: prop_types_1.default.node,
    classes: prop_types_1.default.object,
};
Button.defaultProps = {
    onClick: () => { },
    isDisabled: false,
    iconBefore: null,
    iconAfter: null,
    title: null,
    id: undefined,
    target: '_self',
    applyBorderRadius: false,
    compactMode: false,
    loading: false,
    loadingIcon: null,
    withoutBackground: false,
};
exports.default = withStyles_1.default(styles_1.default)(Button);
//# sourceMappingURL=index.js.map