'use client';
'use strict';

var _tslib = require('../_virtual/_tslib.js');
var index = require('../external/copy-to-clipboard/index.js');
var React = require('react');
var composeReactRefs = require('../lib/compose-react-refs.js');

var Root = React.forwardRef(function CopyToClipboardRootComponent(props, ref) {
    var _a, _b;
    var clipboardText = props.clipboardText, onCopy = props.onCopy, children = props.children, options = props.options, otherProps = _tslib.__rest(props, ["clipboardText", "onCopy", "children", "options"]);
    var format = options === null || options === void 0 ? void 0 : options.format;
    var elem = children && React.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 = React.useCallback(function (event) {
        if (clipboardText !== undefined) {
            if (format) {
                index(clipboardText, { format: format });
            }
            else {
                index(clipboardText);
            }
            if (onCopy) {
                onCopy();
            }
        }
        // Bypass onClick if it was present
        if (typeof elemOnClick === "function") {
            elemOnClick(event);
        }
    }, [onCopy, clipboardText, elemOnClick, format]);
    return elem
        ? React.cloneElement(elem, _tslib.__assign(_tslib.__assign({}, otherProps), { onClick: onClick, ref: composeReactRefs.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 };

exports.CopyToClipboard = CopyToClipboard;
exports.Root = Root;
//# sourceMappingURL=copy-to-clipboard.js.map
