var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
import * as _ from "lodash";
import * as classnames from "classnames";
import * as React from "react";
import * as PropTypes from "prop-types";
import "./Button.less";
var Size = {
    S: "small",
    M: "regular",
    L: "large",
};
var Type = {
    PRIMARY: "primary",
    SECONDARY: "secondary",
    DESTRUCTIVE: "destructive",
    SECONDARY_DESTRUCTIVE: "secondaryDestructive",
    LINK: "link",
    LINK_PLAIN: "linkPlain",
    PLAIN: "plain",
};
var propTypes = {
    children: PropTypes.node,
    className: PropTypes.string,
    type: PropTypes.oneOf(_.values(Type)),
    size: PropTypes.oneOf(_.values(Size)),
    value: PropTypes.node,
    href: PropTypes.string,
    target: PropTypes.oneOf(["_self", "_blank"]),
    disabled: PropTypes.bool,
    onClick: PropTypes.func,
    submit: PropTypes.bool,
    style: PropTypes.object,
    underlined: PropTypes.bool,
    ariaLabel: PropTypes.string,
    dataTestID: PropTypes.string,
};
var defaultProps = {
    type: Type.SECONDARY,
    size: Size.M,
    target: "_blank",
};
var Button = /** @class */ (function (_super) {
    __extends(Button, _super);
    function Button() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    Button.prototype.focus = function () {
        if (this._buttonRef) {
            this._buttonRef.focus();
        }
    };
    Button.prototype.blur = function () {
        if (this._buttonRef) {
            this._buttonRef.blur();
        }
    };
    Button.prototype.render = function () {
        var _this = this;
        var _a = this.props, ariaLabel = _a.ariaLabel, dataTestID = _a.dataTestID, children = _a.children, className = _a.className, disabled = _a.disabled, href = _a.href, onClick = _a.onClick, size = _a.size, style = _a.style, submit = _a.submit, target = _a.target, type = _a.type, value = _a.value, underlined = _a.underlined;
        var additionalProps = _.omit(this.props, Object.keys(propTypes));
        if ((type === Type.DESTRUCTIVE || type === Type.SECONDARY_DESTRUCTIVE) && size === Size.S) {
            throw new Error("Small destructive buttons are not supported");
        }
        if (href && submit) {
            throw new Error("Buttons with href do not support the submit option");
        }
        if (type !== Type.LINK_PLAIN && underlined) {
            throw new Error("Underlined " + type + " button not supported");
        }
        var classes = classnames("Button Button--" + type, "Button--" + size, className, underlined ? "Button--linkUnderlined" : "");
        var content = children || value;
        var aria = ariaLabel || (typeof content === "string" ? content : null);
        if (href == null || disabled) {
            // use <button>s for all disabled links and things with no href prop (buttons)
            return (React.createElement("button", __assign({}, additionalProps, { "aria-label": aria, "data-testid": dataTestID, className: classes, disabled: disabled, onClick: onClick, ref: function (ref) {
                    _this._buttonRef = ref;
                }, style: style, type: submit ? "submit" : "button" }), content));
        }
        return (React.createElement("a", __assign({}, additionalProps, { "aria-label": aria, className: classes, href: href, onClick: onClick, ref: function (ref) {
                _this._buttonRef = ref;
            }, style: style, target: target }), content));
    };
    Button.propTypes = propTypes;
    Button.defaultProps = defaultProps;
    Button.Size = Size;
    Button.Type = Type;
    return Button;
}(React.PureComponent));
export { Button };
