function _define_property(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        });
    } else {
        obj[key] = value;
    }
    return obj;
}
function _object_spread(target) {
    for(var i = 1; i < arguments.length; i++){
        var source = arguments[i] != null ? arguments[i] : {};
        var ownKeys = Object.keys(source);
        if (typeof Object.getOwnPropertySymbols === "function") {
            ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
                return Object.getOwnPropertyDescriptor(source, sym).enumerable;
            }));
        }
        ownKeys.forEach(function(key) {
            _define_property(target, key, source[key]);
        });
    }
    return target;
}
function ownKeys(object, enumerableOnly) {
    var keys = Object.keys(object);
    if (Object.getOwnPropertySymbols) {
        var symbols = Object.getOwnPropertySymbols(object);
        if (enumerableOnly) {
            symbols = symbols.filter(function(sym) {
                return Object.getOwnPropertyDescriptor(object, sym).enumerable;
            });
        }
        keys.push.apply(keys, symbols);
    }
    return keys;
}
function _object_spread_props(target, source) {
    source = source != null ? source : {};
    if (Object.getOwnPropertyDescriptors) {
        Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
    } else {
        ownKeys(Object(source)).forEach(function(key) {
            Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
        });
    }
    return target;
}
function _object_without_properties(source, excluded) {
    if (source == null) return {};
    var target = {}, sourceKeys, key, i;
    if (typeof Reflect !== "undefined" && Reflect.ownKeys) {
        sourceKeys = Reflect.ownKeys(source);
        for(i = 0; i < sourceKeys.length; i++){
            key = sourceKeys[i];
            if (excluded.indexOf(key) >= 0) continue;
            if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
            target[key] = source[key];
        }
        return target;
    }
    target = _object_without_properties_loose(source, excluded);
    if (Object.getOwnPropertySymbols) {
        sourceKeys = Object.getOwnPropertySymbols(source);
        for(i = 0; i < sourceKeys.length; i++){
            key = sourceKeys[i];
            if (excluded.indexOf(key) >= 0) continue;
            if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
            target[key] = source[key];
        }
    }
    return target;
}
function _object_without_properties_loose(source, excluded) {
    if (source == null) return {};
    var target = {}, sourceKeys = Object.getOwnPropertyNames(source), key, i;
    for(i = 0; i < sourceKeys.length; i++){
        key = sourceKeys[i];
        if (excluded.indexOf(key) >= 0) continue;
        if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
        target[key] = source[key];
    }
    return target;
}
import React, { forwardRef } from "react";
import cls from "classnames";
import { convertRoundnessMatrix } from "../../utils/roundness";
import { cx } from "../../utils";
import { base as viewCSS } from "./variations/_view/base";
import { base as sizeCSS } from "./variations/_size/base";
import { base as disabledCSS } from "./variations/_disabled/base";
import { base as focusedCSS } from "./variations/_focused/base";
import { base as stretchingCSS } from "./variations/_stretching/base";
import { base as blurredCSS } from "./variations/_blurred/base";
import { base, ButtonText, ButtonValue, Loader, LoadWrap, StyledAdditionalContent, StyledContentLeft, StyledContentRight, StyledSpinner } from "./Button.styles";
import { classes, tokens } from "./Button.tokens";
export var buttonRoot = function buttonRoot(Root) {
    return /*#__PURE__*/ forwardRef(function(props, ref) {
        var children = props.children, view = props.view, size = props.size, text = props.text, value = props.value, contentLeft = props.contentLeft, contentRight = props.contentRight, _props_contentPlacing = props.contentPlacing, contentPlacing = _props_contentPlacing === void 0 ? 'default' : _props_contentPlacing, isLoading = props.isLoading, loader = props.loader, stretch = props.stretch, square = props.square, pin = props.pin, disabled = props.disabled, focused = props.focused, outlined = props.outlined, className = props.className, blur = props.blur, style = props.style, _props_stretching = props.stretching, stretching = _props_stretching === void 0 ? 'auto' : _props_stretching, additionalContent = props.additionalContent, rest = _object_without_properties(props, [
            "children",
            "view",
            "size",
            "text",
            "value",
            "contentLeft",
            "contentRight",
            "contentPlacing",
            "isLoading",
            "loader",
            "stretch",
            "square",
            "pin",
            "disabled",
            "focused",
            "outlined",
            "className",
            "blur",
            "style",
            "stretching",
            "additionalContent"
        ]);
        var txt = typeof children === 'string' ? children : text;
        var hasRightContentMargin = Boolean(children || txt || value || contentRight);
        var hasLeftContentMargin = Boolean(contentLeft ? contentLeft && (txt || value) : txt || value);
        var stretchingClass = stretch ? classes.filledStretching : classes["".concat(stretching, "Stretching")];
        var contentRelaxedClass = contentPlacing === 'relaxed' ? classes.contentRelaxed : undefined;
        var squareClass = square ? classes.buttonSquare : undefined;
        var isLoadingClass = isLoading ? classes.buttonLoading : undefined;
        var buttonBorderRadius = pin ? convertRoundnessMatrix(pin, "var(".concat(tokens.buttonRadius, ")"), "var(".concat(tokens.buttonHeight, ")")) : "var(".concat(tokens.buttonRadius, ", calc(var(").concat(tokens.buttonHeight, ") / 4))");
        var contentPlacingMapper = {
            default: 'center',
            relaxed: 'space-between'
        };
        var contentPlacementValue = contentPlacing ? contentPlacingMapper[contentPlacing] : 'center';
        return /*#__PURE__*/ React.createElement(Root, _object_spread({
            type: "button",
            ref: ref,
            view: view,
            size: size,
            value: value,
            disabled: disabled,
            focused: focused || outlined,
            className: cx(squareClass, stretchingClass, classes.buttonItem, isLoadingClass, className),
            style: _object_spread_props(_object_spread({}, style), {
                '--plasma_computed-btn-br': buttonBorderRadius,
                '--plasma_private-blur': blur
            })
        }, rest), /*#__PURE__*/ React.createElement(LoadWrap, {
            contentPlacing: contentPlacementValue,
            isLoading: isLoading
        }, contentLeft && /*#__PURE__*/ React.createElement(StyledContentLeft, {
            hasContentMargin: hasRightContentMargin
        }, contentLeft), txt ? /*#__PURE__*/ React.createElement(ButtonText, {
            className: !additionalContent ? contentRelaxedClass : ''
        }, txt) : children, additionalContent && /*#__PURE__*/ React.createElement(StyledAdditionalContent, {
            className: cls(contentRelaxedClass, _define_property({}, classes.buttonHasValue, Boolean(value)))
        }, additionalContent), value && /*#__PURE__*/ React.createElement(ButtonValue, null, value), contentRight && /*#__PURE__*/ React.createElement(StyledContentRight, {
            hasContentMargin: hasLeftContentMargin
        }, contentRight)), isLoading && /*#__PURE__*/ React.createElement(Loader, null, loader || /*#__PURE__*/ React.createElement(StyledSpinner, null)));
    });
};
export var buttonConfig = {
    name: 'Button',
    tag: 'button',
    layout: buttonRoot,
    base: base,
    variations: {
        view: {
            css: viewCSS
        },
        size: {
            css: sizeCSS
        },
        disabled: {
            css: disabledCSS,
            attrs: true
        },
        focused: {
            css: focusedCSS
        },
        blurred: {
            css: blurredCSS
        },
        stretching: {
            css: stretchingCSS
        }
    },
    defaults: {
        view: 'secondary',
        size: 'm',
        stretching: 'auto'
    }
};
