import { jsxs, Fragment, jsx } from 'react/jsx-runtime';
import { forwardRef } from 'react';
import { Link } from 'react-router-dom';
import { c as cn } from './utils-DdHUxIdC.js';
import { r as renderIcon } from './renderIcon-tlvMyboj.js';
import { Button as Button$1 } from './primitives/Button.js';
import { Spinner } from './primitives/Spinner.js';
import { TooltipProvider, Tooltip, TooltipTrigger, TooltipContent } from './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 = 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(icon) : null;
    // --- Build the inner content ---
    const buttonContent = (jsxs(Fragment, { children: [loading && (jsx("span", { className: "absolute inset-0 flex items-center justify-center", children: jsx(Spinner, { "aria-hidden": "true", className: SPINNER_SIZE_MAP[resolvedSize] }) })), jsxs("span", { className: cn("inline-flex items-center gap-1.5", loading && "invisible"), children: [iconPosition === "left" && iconElement && (jsx("span", { "data-icon": "inline-start", children: iconElement })), renderLabel && jsx("span", { children: renderLabel }), iconPosition === "right" && iconElement && (jsx("span", { "data-icon": "inline-end", children: iconElement }))] })] }));
    // --- Merged className ---
    const mergedClassName = cn(fullWidth && "w-full", loading && "relative pointer-events-none", className);
    // --- Determine render element ---
    let buttonElement;
    if (asChild) {
        buttonElement = (jsx(Button$1, { ref: ref, asChild: true, variant: variant, size: resolvedSize, className: mergedClassName, disabled: isDisabled, ...otherProps, children: children }));
    }
    else if (!disabled && to) {
        buttonElement = (jsx(Button$1, { asChild: true, variant: variant, size: resolvedSize, className: mergedClassName, disabled: isDisabled, ...otherProps, children: jsx(Link, { innerRef: ref, to: to, onClick: handleClick, children: buttonContent }) }));
    }
    else if (!disabled && href) {
        buttonElement = (jsx(Button$1, { asChild: true, variant: variant, size: resolvedSize, className: mergedClassName, disabled: isDisabled, ...otherProps, children: jsx("a", { ref: ref, href: href, onClick: handleClick, children: buttonContent }) }));
    }
    else {
        buttonElement = (jsx(Button$1, { 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 jsx(Fragment, { children: buttonElement });
        }
        const side = position ? TOOLTIP_SIDE_MAP[position] : undefined;
        // Wrap disabled buttons in a span so tooltip still triggers
        const triggerContent = isDisabled ? (jsx("span", { className: "inline-flex", children: buttonElement })) : (buttonElement);
        return (jsx(TooltipProvider, { children: jsxs(Tooltip, { children: [jsx(TooltipTrigger, { asChild: true, children: triggerContent }), jsx(TooltipContent, { side: side, children: content })] }) }));
    }
    return jsx(Fragment, { children: buttonElement });
});
Button.displayName = "Button";

export { Button as B };
//# sourceMappingURL=Button-DSvnInaL.js.map
