'use client';
import { jsx as _jsx } from "react/jsx-runtime";
import { useRef, useState } from 'react';
import { useMergedRefs } from '@base-ui/utils/useMergedRefs';
import { CopyInteractiveIcon } from '@graphprotocol/gds-react/icons';
import { useGDS } from "../hooks/index.js";
import { cn, isReactNode } from "../utils/index.js";
import { Button } from "./Button.js";
import { Tooltip } from "./Tooltip.js";
export function CopyButton({ ref: passedRef, value, addonAfter, tooltip, onCopy, className, children, ...props }) {
    useGDS();
    const buttonRef = useRef(null);
    const buttonPassedRef = useMergedRefs(buttonRef, passedRef);
    const [copied, privateSetCopied] = useState(false);
    const copiedTimeoutRef = useRef(undefined);
    const setCopied = (newCopied) => {
        window.clearTimeout(copiedTimeoutRef.current);
        privateSetCopied(newCopied);
        if (newCopied) {
            setTooltipOpen(true);
            copiedTimeoutRef.current = window.setTimeout(() => setCopied(false), 1000);
        }
        else {
            // Ensure the original tooltip is shown again when leaving the button and coming back while copied
            copiedTimeoutRef.current = window.setTimeout(() => {
                if (!tooltipOpenRef.current && buttonRef.current?.matches(':hover, :focus-visible')) {
                    setTooltipOpen(true);
                }
            }, 150);
        }
    };
    const hasChildren = children != null;
    const copyIcon = _jsx(CopyInteractiveIcon, { copied: copied });
    const tooltipProps = isReactNode(tooltip) ? { content: tooltip } : tooltip;
    const [tooltipOpen, setTooltipOpen] = useState(false);
    const tooltipOpenRef = useRef(tooltipOpen);
    tooltipOpenRef.current = tooltipOpen;
    return (_jsx(Tooltip, { ...tooltipProps, content: "Copied!", ...(copied
            ? {
                open: true,
                onOpenChange: (newOpen, reason) => {
                    if (reason === 'disabled')
                        return;
                    setTooltipOpen(newOpen);
                },
                className: cn('i:prop-disabled-false', tooltipProps?.className),
            }
            : { disabled: true }), children: _jsx(Button, { ref: buttonPassedRef, addonAfter: addonAfter !== undefined ? addonAfter : hasChildren ? copyIcon : undefined, tooltip: {
                open: tooltipOpen,
                onOpenChange: (newOpen) => {
                    if (copied)
                        return;
                    setTooltipOpen(newOpen);
                },
                ...tooltipProps,
            }, className: cn('gds-copy-button', className), ...props, onClick: async (event) => {
                props.onClick?.(event);
                if (event.defaultPrevented)
                    return;
                await navigator.clipboard.writeText(value);
                onCopy?.(value);
                setCopied(true);
            }, children: hasChildren ? children : copyIcon }) }));
}
//# sourceMappingURL=CopyButton.js.map