/*
 * Copyright 2016 Palantir Technologies, Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { __assign } from "tslib";
import classNames from "classnames";
import * as React from "react";
import { useInteractiveAttributes, } from "../../accessibility/useInteractiveAttributes";
import { Classes, Utils } from "../../common";
import { DISPLAYNAME_PREFIX, removeNonHTMLProps } from "../../common/props";
import { Icon } from "../icon/icon";
import { Spinner, SpinnerSize } from "../spinner/spinner";
import { Text } from "../text/text";
/**
 * Button component.
 *
 * @see https://blueprintjs.com/docs/#core/components/button
 */
export var Button = React.forwardRef(function (props, ref) {
    var commonAttributes = useSharedButtonAttributes(props, ref);
    return (React.createElement("button", __assign({ type: "button" }, removeNonHTMLProps(props), commonAttributes), renderButtonContents(props)));
});
Button.displayName = "".concat(DISPLAYNAME_PREFIX, ".Button");
/**
 * AnchorButton component.
 *
 * @see https://blueprintjs.com/docs/#core/components/button
 */
export var AnchorButton = React.forwardRef(function (props, ref) {
    var href = props.href;
    var commonProps = useSharedButtonAttributes(props, ref, {
        defaultTabIndex: 0,
        disabledTabIndex: -1,
    });
    return (React.createElement("a", __assign({ role: "button" }, removeNonHTMLProps(props), commonProps, { "aria-disabled": commonProps.disabled, href: commonProps.disabled ? undefined : href }), renderButtonContents(props)));
});
AnchorButton.displayName = "".concat(DISPLAYNAME_PREFIX, ".AnchorButton");
/**
 * Most of the button logic lives in this shared hook.
 */
function useSharedButtonAttributes(props, ref, options) {
    var _a;
    var alignText = props.alignText, fill = props.fill, large = props.large, _b = props.loading, loading = _b === void 0 ? false : _b, minimal = props.minimal, outlined = props.outlined, small = props.small;
    var disabled = props.disabled || loading;
    var _c = useInteractiveAttributes(!disabled, props, ref, options), active = _c[0], interactiveProps = _c[1];
    var className = classNames(Classes.BUTTON, (_a = {},
        _a[Classes.ACTIVE] = active,
        _a[Classes.DISABLED] = disabled,
        _a[Classes.FILL] = fill,
        _a[Classes.LARGE] = large,
        _a[Classes.LOADING] = loading,
        _a[Classes.MINIMAL] = minimal,
        _a[Classes.OUTLINED] = outlined,
        _a[Classes.SMALL] = small,
        _a), Classes.alignmentClass(alignText), Classes.intentClass(props.intent), props.className);
    return __assign(__assign({}, interactiveProps), { className: className, disabled: disabled });
}
/**
 * Shared rendering code for button contents.
 */
function renderButtonContents(props) {
    var children = props.children, ellipsizeText = props.ellipsizeText, icon = props.icon, loading = props.loading, rightIcon = props.rightIcon, text = props.text, textClassName = props.textClassName;
    var hasTextContent = !Utils.isReactNodeEmpty(text) || !Utils.isReactNodeEmpty(children);
    return (React.createElement(React.Fragment, null,
        loading && React.createElement(Spinner, { key: "loading", className: Classes.BUTTON_SPINNER, size: SpinnerSize.SMALL }),
        React.createElement(Icon, { key: "leftIcon", icon: icon }),
        hasTextContent && (React.createElement(Text, { key: "text", className: classNames(Classes.BUTTON_TEXT, textClassName), ellipsize: ellipsizeText, tagName: "span" },
            text,
            children)),
        React.createElement(Icon, { key: "rightIcon", icon: rightIcon })));
}
//# sourceMappingURL=buttons.js.map