"use client";
'use strict';

var tslib_es6 = require('../../node_modules/.pnpm/@rollup_plugin-typescript@12.1.4_rollup@4.46.2_tslib@2.8.1_typescript@5.9.3/node_modules/tslib/tslib.es6.js');
var jsxRuntime = require('react/jsx-runtime');
var utils = require('@windrun-huaiin/lib/utils');
var server = require('@windrun-huaiin/base-ui/components/server');
var lib = require('@windrun-huaiin/base-ui/lib');
var Link = require('next/link');
var React = require('react');

function GradientButton({ title, icon, align = 'left', disabled = false, className = "", href, openInNewTab = true, onClick, loadingText, preventDoubleClick = true, iconClassName, }) {
    const [isLoading, setIsLoading] = React.useState(false);
    const actualLoadingText = loadingText || (title === null || title === void 0 ? void 0 : title.toString().trim()) || 'Loading...';
    const defaultIconClass = "h-4 w-4";
    const finalIconClass = utils.cn("text-white", iconClassName || defaultIconClass);
    // set justify class according to alignment
    const getAlignmentClass = () => {
        switch (align) {
            case 'center':
                return 'justify-center';
            case 'right':
                return 'justify-end';
            default: // 'left'
                return 'justify-start';
        }
    };
    const handleClick = (e) => tslib_es6.__awaiter(this, void 0, void 0, function* () {
        if (disabled || isLoading) {
            e.preventDefault();
            return;
        }
        if (onClick) {
            e.preventDefault();
            if (preventDoubleClick) {
                setIsLoading(true);
            }
            try {
                yield onClick();
            }
            catch (error) {
                console.error('GradientButton onClick error:', error);
            }
            finally {
                if (preventDoubleClick) {
                    setIsLoading(false);
                }
            }
        }
    });
    const isDisabled = disabled || isLoading;
    const displayTitle = isLoading ? actualLoadingText : title;
    // icon
    const iconProvided = icon !== undefined;
    const iconNode = (() => {
        if (isLoading) {
            return jsxRuntime.jsx(server.globalLucideIcons.Loader2, { className: utils.cn(finalIconClass, 'animate-spin') });
        }
        if (iconProvided) {
            if (icon === null || icon === false) {
                return null;
            }
            if (React.isValidElement(icon)) {
                return React.cloneElement(icon, {
                    className: utils.cn(finalIconClass, icon.props.className),
                });
            }
            return icon;
        }
        return jsxRuntime.jsx(server.globalLucideIcons.ArrowRight, { className: utils.cn(finalIconClass) });
    })();
    const shouldRenderIcon = iconNode !== null && iconNode !== undefined;
    const buttonContent = onClick ? (jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [shouldRenderIcon ? jsxRuntime.jsx("span", { children: iconNode }) : null, jsxRuntime.jsx("span", { className: utils.cn(shouldRenderIcon && 'ml-1'), children: displayTitle })] })) : (jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [jsxRuntime.jsx("span", { children: displayTitle }), shouldRenderIcon ? jsxRuntime.jsx("span", { className: "ml-1", children: iconNode }) : null] }));
    const alignmentClass = align === 'right'
        ? 'justify-end'
        : align === 'center'
            ? 'justify-center'
            : 'justify-start';
    // Base styles extracted from Button component + size="lg" (h-11 px-8)
    // Removed [&_svg] constraints
    const baseButtonStyles = "inline-flex items-center gap-2 whitespace-nowrap h-11 px-8 ring-offset-background focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50";
    const buttonClassName = utils.cn(baseButtonStyles, lib.themeButtonGradientClass, lib.themeButtonGradientHoverClass, 'text-white text-base font-bold shadow-lg hover:shadow-xl transition-all duration-300 rounded-full', alignmentClass, isDisabled && 'opacity-50 cursor-not-allowed', className);
    return (jsxRuntime.jsx("div", { className: `flex flex-row gap-3 ${getAlignmentClass()}`, children: onClick ? (
        // for click
        jsxRuntime.jsx("button", { type: "button", className: buttonClassName, onClick: handleClick, disabled: isDisabled, children: buttonContent })) : (
        // for Link
        jsxRuntime.jsx(Link, Object.assign({ href: href || "#", className: utils.cn(buttonClassName, "no-underline hover:no-underline") }, (openInNewTab ? { target: "_blank", rel: "noopener noreferrer" } : {}), { onClick: isDisabled ? (e) => e.preventDefault() : undefined, "aria-disabled": isDisabled, children: buttonContent }))) }));
}

exports.GradientButton = GradientButton;
