import { jsx as _jsx } from "react/jsx-runtime";
// (C) 2026 GoodData Corporation
import { useCallback } from "react";
import { defineMessages, useIntl } from "react-intl";
import { useToastMessage } from "../../Messages/toasts/useToastMessage.js";
import { UiIconButton } from "../UiIconButton/UiIconButton.js";
const messages = defineMessages({
    copySuccess: {
        id: "copyButton.copy.success",
    },
    copyError: {
        id: "copyButton.copy.error",
    },
    copy: {
        id: "copyButton.copy",
    },
});
/**
 * @internal
 */
export function UiCopyButton({ label, clipboardContent, successMessage, successMessageOptions, errorMessage, errorMessageOptions, }) {
    const intl = useIntl();
    const { addSuccess, addError } = useToastMessage();
    const onCopy = useCallback(() => {
        navigator.clipboard
            .writeText(clipboardContent)
            .then(() => {
            addSuccess(successMessage ?? messages.copySuccess, successMessageOptions);
        })
            .catch((e) => {
            addError(errorMessage ?? messages.copyError, errorMessageOptions);
            console.error("Failed copy content to clipboard", e);
        });
    }, [
        addError,
        addSuccess,
        clipboardContent,
        errorMessage,
        errorMessageOptions,
        successMessage,
        successMessageOptions,
    ]);
    return (_jsx(UiIconButton, { label: label ?? intl.formatMessage(messages.copy), icon: "copy", size: "small", variant: "tertiary", onClick: onCopy }));
}
