import _extends from "@babel/runtime/helpers/esm/extends";
import React, { Children } from 'react';
import copy from './copy';
import Button from '../button';
import { useToasts } from '@helpdice/theme';
// icons
import Copy from "@helpdice/icons/curved/Copy";

// export function Clipboard(props: ClipboardProps) {
//     const { setToast } = useToasts();
//     const { showIcon, text, onCopy, options: options, children, } = props;
//     const elem: ReactNode = React.Children.only(children) as ReactNode;
//     const onClick = (event: MouseEventHandler<HTMLButtonElement | MouseEvent>) => {
//         const result = copy(text, options);

//         if (result) {
//             setToast({
//                 text: 'Text Copied'
//             });
//         }

//         if (onCopy) {
//             onCopy(text, result);
//         }

//         // Bypass onClick if it was present
//         if (elem && elem.props && typeof elem.props.onClick === 'function') {
//             elem.props.onClick(event);
//         }
//     };

//     if (showIcon) {
//         return React.cloneElement(
//             <div>
//                 {elem}
//                 <Button auto scale={0.1} px={0.6} onClick={onClick} icon={<Copy />} />
//             </div>,
//             { ...props }
//         );
//     }

//     return React.cloneElement(
//         <div onClick={onClick}>
//             {elem}
//         </div>,
//         { ...props }
//     );
// }

export default function Clipboard(props) {
  var _useToasts = useToasts(),
    setToast = _useToasts.setToast;
  var showIcon = props.showIcon,
    text = props.text,
    onCopy = props.onCopy,
    options = props.options,
    children = props.children;
  var elem = Children.only(children);
  var _onClick = function onClick() {
    var result = copy(text, options);
    if (result) {
      setToast({
        text: 'Text Copied'
      });
    }
    if (onCopy) {
      onCopy(text, result);
    }

    // ✅ Safely invoke child's onClick if it exists
    // if (isValidElement(elem) && typeof elem?.props?.onClick === 'function') {
    //   elem.props.onClick();
    // }
  };

  // If showIcon is true, render the child + a copy icon button
  if (showIcon) {
    return /*#__PURE__*/React.createElement("div", null, elem, /*#__PURE__*/React.createElement(Button, {
      auto: true,
      scale: 0.1,
      px: 0.6,
      onClick: function onClick() {
        return _onClick;
      },
      icon: /*#__PURE__*/React.createElement(Copy, null)
    }));
  }

  // Otherwise, make the entire div clickable for copy
  return /*#__PURE__*/React.cloneElement(/*#__PURE__*/React.createElement("div", {
    onClick: _onClick
  }, elem), _extends({}, props));
}