import _defineProperty from '@babel/runtime/helpers/defineProperty';
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProperties';
import { useState, useRef, useLayoutEffect } from 'react';
import classnames from 'classnames';
import { copyToClipboard } from '@bigbinary/neeto-commons-frontend/utils/general';
import Copy from '@bigbinary/neeto-icons/Copy';
import Check from '@bigbinary/neeto-icons/Check';
import Button from '@bigbinary/neetoui/Button';
import { mergeAll } from 'ramda';
import { useTranslation } from 'react-i18next';
import { n } from './inject-css-C2dztUxs.js';
import { jsx } from 'react/jsx-runtime';

var TIME_OUT = 2000;
var TOOLTIP_CONFIG = {
  trigger: "click mouseenter",
  hideAfter: TIME_OUT,
  position: "top"
};
var BUTTON_SIZES = {
  medium: "medium"};

var css = ".neeto-molecules-copy-button{display:flex;justify-content:space-between}.neeto-molecules-copy-button .neeto-ui-btn:active,.neeto-molecules-copy-button .neeto-ui-btn:focus,.neeto-molecules-copy-button .neeto-ui-btn:hover{box-shadow:none}.neeto-molecules-copy-button--active{background:rgba(var(--neeto-ui-success-500));color:rgba(var(--neeto-ui-white))}.neeto-molecules-copy-button--active.neeto-ui-btn:active,.neeto-molecules-copy-button--active.neeto-ui-btn:focus,.neeto-molecules-copy-button--active.neeto-ui-btn:hover{background:rgba(var(--neeto-ui-success-500));box-shadow:none;color:rgba(var(--neeto-ui-white))}";
n(css,{});

var _excluded = ["className", "icon", "label", "successLabel", "tooltipContent", "successTooltipContent", "value", "style", "size", "tooltipProps", "onClick"];
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), true).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var CopyToClipboardButton = function CopyToClipboardButton(_ref) {
  var className = _ref.className,
    _ref$icon = _ref.icon,
    icon = _ref$icon === void 0 ? Copy : _ref$icon,
    _ref$label = _ref.label,
    label = _ref$label === void 0 ? "" : _ref$label,
    successLabel = _ref.successLabel,
    tooltipContent = _ref.tooltipContent,
    successTooltipContent = _ref.successTooltipContent,
    value = _ref.value,
    style = _ref.style,
    _ref$size = _ref.size,
    size = _ref$size === void 0 ? BUTTON_SIZES.medium : _ref$size,
    tooltipProps = _ref.tooltipProps,
    onClick = _ref.onClick,
    otherProps = _objectWithoutProperties(_ref, _excluded);
  var _useTranslation = useTranslation(),
    t = _useTranslation.t;
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    isChecked = _useState2[0],
    setIsChecked = _useState2[1];
  var ref = useRef();
  var onHandleClick = function onHandleClick(event) {
    event.preventDefault();
    event.stopPropagation();
    if (onClick) onClick(event);else copyToClipboard(value, {
      showToastr: false
    });
    setIsChecked(true);
    setTimeout(function () {
      setIsChecked(false);
    }, TIME_OUT); // Reset copied state after 2 seconds
  };
  useLayoutEffect(function () {
    if (!ref.current) return;
    ref.current.style.minWidth = "";
    var buttonWidth = ref.current.clientWidth;
    ref.current.style.minWidth = "".concat(buttonWidth, "px");
  }, [size]);
  var tooltipOptions = label ? null : mergeAll([TOOLTIP_CONFIG, {
    content: isChecked ? successTooltipContent || t("neetoMolecules.common.copied") : tooltipContent || t("neetoMolecules.common.copyToClipboard")
  }, tooltipProps]);
  var buttonLabel = isChecked && label ? successLabel || t("neetoMolecules.common.copied") : label;
  var buttonIcon = isChecked ? Check : icon;
  return /*#__PURE__*/jsx(Button, _objectSpread({
    ref: ref,
    size: size,
    style: style,
    "data-testid": "copy-to-clipboard-button",
    icon: buttonIcon,
    label: buttonLabel,
    tooltipProps: tooltipOptions,
    className: classnames("neeto-molecules-copy-button", _defineProperty(_defineProperty(_defineProperty({}, className, className), "neeto-molecules-copy-button--active", isChecked), "neeto-molecules-copy-button--".concat(size), size)),
    onClick: onHandleClick
  }, otherProps));
};

export { CopyToClipboardButton as default };
//# sourceMappingURL=CopyToClipboardButton.js.map
