import { jsx as _jsx } from "react/jsx-runtime";
import { Button, useToastsApi, copy, tasty, CopyIcon as CopyIconUIKit, } from '@cube-dev/ui-kit';
import { useState } from 'react';
import { unstable_batchedUpdates } from 'react-dom';
import { useDebouncedState, useEvent } from '../hooks';
import { CopyIcon } from './CopyIcon';
const CopyButtonElement = tasty(Button, {
    label: 'Copy value to clipboard',
    type: 'clear',
    size: 'small',
    icon: _jsx(CopyIconUIKit, {}),
});
export function CopyButton(props) {
    const { value, onCopy, toastMessage = 'Copied to clipboard', dontShowToast = false, ...buttonProps } = props;
    const { toast } = useToastsApi();
    const [coping, setCoping] = useDebouncedState(false, 300);
    const [copied, setCopied] = useState(false);
    const onCopyAnimationEnd = useEvent(() => setCopied(false));
    const icon = props.icon ?? _jsx(CopyIcon, { isCopied: copied, onCopyAnimationEnd: onCopyAnimationEnd });
    return (_jsx(CopyButtonElement, { ...buttonProps, isLoading: coping, icon: icon, onPress: async () => {
            setCoping(true);
            await copy(value);
            unstable_batchedUpdates(() => {
                setCoping(false);
                setCopied(true);
            });
            onCopy?.();
            if (dontShowToast) {
                return;
            }
            toast.success(toastMessage);
        } }));
}
//# sourceMappingURL=CopyButton.js.map