"use strict";
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Button = exports.ButtonBase = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const context_1 = require("../buttonGroup/context");
const core_1 = require("../core");
const utils_1 = require("../utils");
const buttonIcon_1 = __importDefault(require("./buttonIcon"));
const buttonText_1 = __importDefault(require("./buttonText"));
const consts_1 = require("./consts");
const context_2 = require("./context");
const ContentWrapper = core_1.styled.span `
  visibility: hidden;
`;
const StateWrapper = core_1.styled.span `
  bottom: 0;
  display: flex;
  left: 0;
  position: absolute;
  right: 0;
  top: 0;
`;
exports.ButtonBase = core_1.styled.buttonBox `
  align-items: center;
  display: inline-flex;
  flex-shrink: 0;
  font-weight: medium;
  justify-content: center;
  line-height: normal;
  overflow: hidden;
  position: relative;
  transition-duration: fast;
  user-select: none;
  width: fit-content;
`;
/**
 * Triggers given actions on click. Supports multiple states, sizes and variants.
 *
 * Comes with additional props for extra behavior, like loading, disabled or elevated.
 *
 * Exposes some props to the children via context.
 */
exports.Button = (0, core_1.vui)((props, ref) => {
    var _a, _b, _c, _d, _e, _f, _g, _h;
    const buttonGroupProps = (0, context_1.useButtonGroupContext)();
    const mergedProps = Object.assign(Object.assign({}, buttonGroupProps), props);
    const { borderWidth: borderWidthProp, children, className, colorScheme, disabled, dropDownIcon = 'falAngleDown', icon, iconLeft, iconRight: iconRightProp, isActive, isDropDown, isElevated, isFullWidth, isLoading, // eslint-disable-line
    isRound, isSplit, isTruncated, size, state = '', stateMapping, text, title, variant: variantProp } = mergedProps, rest = __rest(mergedProps, ["borderWidth", "children", "className", "colorScheme", "disabled", "dropDownIcon", "icon", "iconLeft", "iconRight", "isActive", "isDropDown", "isElevated", "isFullWidth", "isLoading", "isRound", "isSplit", "isTruncated", "size", "state", "stateMapping", "text", "title", "variant"]);
    const hasState = Boolean(state) && state !== 'idle';
    const states = Object.assign(Object.assign({}, consts_1.buttonStateMapping), stateMapping);
    const _j = (_a = states[state]) !== null && _a !== void 0 ? _a : {}, { iconProps: stateIconProps } = _j, stateProps = __rest(_j, ["iconProps"]);
    const iconRight = isDropDown ? 'falAngleDown' : iconRightProp;
    const variant = (0, utils_1.filterUndefined)({
        variant: (_c = (_b = states[state]) === null || _b === void 0 ? void 0 : _b.variant) !== null && _c !== void 0 ? _c : variantProp,
    });
    const styles = (0, core_1.useStyleConfig)('Button', Object.assign(Object.assign({}, mergedProps), variant));
    const _k = styles.container, { borderWidth, h, spaceX = 0 } = _k, buttonStyles = __rest(_k, ["borderWidth", "h", "spaceX"]);
    const context = (0, react_1.useMemo)(() => (0, utils_1.filterUndefined)({ disabled, size, variant }), [disabled, size, variant]);
    const Content = hasState ? ContentWrapper : react_1.Fragment;
    // Apply more space for text and less space for icons
    const border = (_d = borderWidthProp !== null && borderWidthProp !== void 0 ? borderWidthProp : borderWidth) !== null && _d !== void 0 ? _d : 1;
    const spaceXItem = spaceX - border;
    const spaceXText = spaceX * 2 - border;
    const iconLeftMr = spaceXItem + 1 + 'px';
    const iconRightMl = isSplit ? 'auto' : spaceXItem + 1 + 'px';
    const pl = (icon || iconLeft || isLoading ? spaceXItem : spaceXText) + 'px';
    const pr = (icon || iconRight ? spaceXItem : spaceXText) + 'px';
    let ariaLabel = '';
    if (title)
        ariaLabel = title;
    const isActiveProps = isActive
        ? (0, utils_1.filterUndefined)({
            bg: (_e = styles === null || styles === void 0 ? void 0 : styles.container) === null || _e === void 0 ? void 0 : _e.activeBg,
            borderBg: (_f = styles === null || styles === void 0 ? void 0 : styles.container) === null || _f === void 0 ? void 0 : _f.activeBorderBg,
            color: (_g = styles === null || styles === void 0 ? void 0 : styles.container) === null || _g === void 0 ? void 0 : _g.activeColor,
        })
        : {};
    const disabledProps = disabled
        ? {
            'aria-disabled': disabled,
            disabledBg: 'bg',
            disabledBorderColor: 'borderColor',
            disabledColor: 'color',
            hoverBg: 'bg',
            hoverBorderColor: 'borderColor',
            hoverColor: 'color',
            activeBg: 'bg',
            cursor: 'not-allowed',
            opacity: 0.5,
        }
        : {};
    const aliasedProps = (0, utils_1.filterUndefined)({
        borderRadius: isRound ? h / 2 : ((_h = styles === null || styles === void 0 ? void 0 : styles.container) === null || _h === void 0 ? void 0 : _h.borderRadius) || 0,
        boxShadow: isElevated ? '2' : undefined,
        minW: icon ? 0 : undefined,
        pointerEvents: hasState ? 'none' : undefined,
        w: isFullWidth ? '100%' : undefined,
    });
    return ((0, jsx_runtime_1.jsx)(context_2.ButtonProvider, { value: context, children: (0, jsx_runtime_1.jsxs)(exports.ButtonBase, Object.assign({ borderWidth: border, className: (0, utils_1.cs)('vui-button', className, isActive && utils_1.activeClassName, variantProp ? `vui-button-variant-${variantProp}` : undefined), disabled: disabled, focusRing: 3, h: h, pl: pl, pr: pr, ref: ref, title: title || ariaLabel, type: "button", variant: variant }, buttonStyles, isActiveProps, disabledProps, aliasedProps, rest, { children: [hasState && ((0, jsx_runtime_1.jsx)(StateWrapper, Object.assign({ onClick: (e) => e.stopPropagation() }, stateProps, { children: (0, jsx_runtime_1.jsx)(buttonIcon_1.default, Object.assign({ m: "auto" }, stateIconProps)) }))), (0, jsx_runtime_1.jsxs)(Content, { children: [(0, utils_1.isString)(icon) ? (0, jsx_runtime_1.jsx)(buttonIcon_1.default, { name: icon }) : icon, !icon && ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [isLoading ? ((0, jsx_runtime_1.jsx)(buttonIcon_1.default, Object.assign({ mr: iconLeftMr }, consts_1.loadingIconProps))) : (0, utils_1.isString)(iconLeft) ? ((0, jsx_runtime_1.jsx)(buttonIcon_1.default, { mr: iconLeftMr, name: iconLeft })) : (iconLeft), children !== null && children !== void 0 ? children : ((0, utils_1.isReactText)(text) ? (0, jsx_runtime_1.jsx)(buttonText_1.default, { isTruncated, text }) : text), (0, utils_1.isString)(iconRight) ? ((0, jsx_runtime_1.jsx)(buttonIcon_1.default, { ml: !children && !text ? 0 : iconRightMl, name: iconRight })) : (iconRight)] }))] })] })) }));
});
exports.Button.Icon = buttonIcon_1.default;
exports.Button.Text = buttonText_1.default;
exports.Button.displayName = 'Button';
exports.default = exports.Button;
//# sourceMappingURL=button.js.map