'use strict';

var jsxRuntime = require('react/jsx-runtime');
var React = require('react');
var reactRouterDom = require('react-router-dom');
var utils = require('./utils-CTr7wn5d.js');
var renderIcon = require('./renderIcon-fLF3odqg.js');
var primitives_Button = require('./primitives/Button.js');
var primitives_Spinner = require('./primitives/Spinner.js');
var primitives_Tooltip = require('./primitives/Tooltip.js');

const ICON_ONLY_SIZE_MAP = {
    xs: "icon-xs",
    sm: "icon-sm",
    default: "icon",
    lg: "icon-lg",
};
const SPINNER_SIZE_MAP = {
    xs: "size-3",
    sm: "size-3.5",
    default: "size-3.5",
    lg: "size-4",
    icon: "size-3.5",
    "icon-xs": "size-3",
    "icon-sm": "size-3.5",
    "icon-lg": "size-4",
};
// Map NeetoUI tooltip `position` values to Radix side values
const TOOLTIP_SIDE_MAP = {
    top: "top",
    bottom: "bottom",
    left: "left",
    right: "right",
    "top-start": "top",
    "top-end": "top",
    "bottom-start": "bottom",
    "bottom-end": "bottom",
    "left-start": "left",
    "left-end": "left",
    "right-start": "right",
    "right-end": "right",
};

const Button = React.forwardRef(({ variant = "default", size = "default", label, icon = null, iconPosition = "right", loading = false, disabled = false, onClick, to, href, type = "button", fullWidth = false, className, tooltipProps = null, asChild = false, children, ...otherProps }, ref) => {
    const isIconSize = size.startsWith("icon");
    const renderLabel = isIconSize ? null : label || children;
    const isIconOnly = !renderLabel && !!icon;
    const resolvedSize = isIconOnly ? (ICON_ONLY_SIZE_MAP[size] ?? size) : size;
    // --- Loading / disabled state ---
    const isDisabled = disabled || loading;
    // --- Click handler ---
    const handleClick = (e) => {
        if (loading || disabled) {
            e.preventDefault();
            return;
        }
        onClick?.(e);
    };
    // --- Icon elements ---
    const iconElement = icon ? renderIcon.renderIcon(icon) : null;
    // --- Build the inner content ---
    const buttonContent = (jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [loading && (jsxRuntime.jsx("span", { className: "absolute inset-0 flex items-center justify-center", children: jsxRuntime.jsx(primitives_Spinner.Spinner, { "aria-hidden": "true", className: SPINNER_SIZE_MAP[resolvedSize] }) })), jsxRuntime.jsxs("span", { className: utils.cn("inline-flex items-center gap-1.5", loading && "invisible"), children: [iconPosition === "left" && iconElement && (jsxRuntime.jsx("span", { "data-icon": "inline-start", children: iconElement })), renderLabel && jsxRuntime.jsx("span", { children: renderLabel }), iconPosition === "right" && iconElement && (jsxRuntime.jsx("span", { "data-icon": "inline-end", children: iconElement }))] })] }));
    // --- Merged className ---
    const mergedClassName = utils.cn(fullWidth && "w-full", loading && "relative pointer-events-none", className);
    // --- Determine render element ---
    let buttonElement;
    if (asChild) {
        buttonElement = (jsxRuntime.jsx(primitives_Button.Button, { ref: ref, asChild: true, variant: variant, size: resolvedSize, className: mergedClassName, disabled: isDisabled, ...otherProps, children: children }));
    }
    else if (!disabled && to) {
        buttonElement = (jsxRuntime.jsx(primitives_Button.Button, { asChild: true, variant: variant, size: resolvedSize, className: mergedClassName, disabled: isDisabled, ...otherProps, children: jsxRuntime.jsx(reactRouterDom.Link, { innerRef: ref, to: to, onClick: handleClick, children: buttonContent }) }));
    }
    else if (!disabled && href) {
        buttonElement = (jsxRuntime.jsx(primitives_Button.Button, { asChild: true, variant: variant, size: resolvedSize, className: mergedClassName, disabled: isDisabled, ...otherProps, children: jsxRuntime.jsx("a", { ref: ref, href: href, onClick: handleClick, children: buttonContent }) }));
    }
    else {
        buttonElement = (jsxRuntime.jsx(primitives_Button.Button, { ref: ref, type: type, variant: variant, size: resolvedSize, className: mergedClassName, disabled: isDisabled, onClick: handleClick, ...otherProps, children: buttonContent }));
    }
    // --- Tooltip wrapper ---
    if (tooltipProps) {
        const { content, position, disabled: tooltipDisabled } = tooltipProps;
        if (tooltipDisabled || !content) {
            return jsxRuntime.jsx(jsxRuntime.Fragment, { children: buttonElement });
        }
        const side = position ? TOOLTIP_SIDE_MAP[position] : undefined;
        // Wrap disabled buttons in a span so tooltip still triggers
        const triggerContent = isDisabled ? (jsxRuntime.jsx("span", { className: "inline-flex", children: buttonElement })) : (buttonElement);
        return (jsxRuntime.jsx(primitives_Tooltip.TooltipProvider, { children: jsxRuntime.jsxs(primitives_Tooltip.Tooltip, { children: [jsxRuntime.jsx(primitives_Tooltip.TooltipTrigger, { asChild: true, children: triggerContent }), jsxRuntime.jsx(primitives_Tooltip.TooltipContent, { side: side, children: content })] }) }));
    }
    return jsxRuntime.jsx(jsxRuntime.Fragment, { children: buttonElement });
});
Button.displayName = "Button";

exports.Button = Button;
//# sourceMappingURL=Button-BniJ4TT_.js.map
