'use client';
import { __rest, __assign } from '../_virtual/_tslib.js';
import copy from '../external/copy-to-clipboard/index.js';
import React__default, { useCallback } from 'react';
import { composeRefs } from '../lib/compose-react-refs.js';

var Root = React__default.forwardRef(function CopyToClipboardRootComponent(props, ref) {
    var _a, _b;
    var clipboardText = props.clipboardText, onCopy = props.onCopy, children = props.children, options = props.options, otherProps = __rest(props, ["clipboardText", "onCopy", "children", "options"]);
    var format = options === null || options === void 0 ? void 0 : options.format;
    var elem = children && React__default.Children.only(children);
    var elemOnClick = (_a = elem === null || elem === void 0 ? void 0 : elem.props) === null || _a === void 0 ? void 0 : _a.onClick;
    var onClick = useCallback(function (event) {
        if (clipboardText !== undefined) {
            if (format) {
                copy(clipboardText, { format: format });
            }
            else {
                copy(clipboardText);
            }
            if (onCopy) {
                onCopy();
            }
        }
        // Bypass onClick if it was present
        if (typeof elemOnClick === "function") {
            elemOnClick(event);
        }
    }, [onCopy, clipboardText, elemOnClick, format]);
    return elem
        ? React__default.cloneElement(elem, __assign(__assign({}, otherProps), { onClick: onClick, ref: composeRefs(ref, (_b = elem === null || elem === void 0 ? void 0 : elem.props) === null || _b === void 0 ? void 0 : _b.ref) }))
        : null;
});
var CopyToClipboard = { Root: Root };

export { CopyToClipboard, Root };
//# sourceMappingURL=copy-to-clipboard.js.map
