'use strict';

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
var _objectWithoutProperties = require('@babel/runtime/helpers/objectWithoutProperties');
var React = require('react');
var classnames = require('classnames');
var general = require('@bigbinary/neeto-commons-frontend/utils/general');
var Copy = require('@bigbinary/neeto-icons/Copy');
var Check = require('@bigbinary/neeto-icons/Check');
var Button = require('@bigbinary/neetoui/Button');
var ramda = require('ramda');
var reactI18next = require('react-i18next');
var injectCss = require('./inject-css-B6qYtOJe.js');
var jsxRuntime = require('react/jsx-runtime');

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

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))}";
injectCss.n(css,{});

var _excluded = ["className", "icon", "label", "successLabel", "tooltipContent", "successTooltipContent", "value", "style", "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,
    tooltipProps = _ref.tooltipProps,
    onClick = _ref.onClick,
    otherProps = _objectWithoutProperties(_ref, _excluded);
  var _useTranslation = reactI18next.useTranslation(),
    t = _useTranslation.t;
  var _useState = React.useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    isChecked = _useState2[0],
    setIsChecked = _useState2[1];
  var ref = React.useRef();
  var onHandleClick = function onHandleClick(event) {
    event.preventDefault();
    event.stopPropagation();
    if (onClick) onClick(event);else general.copyToClipboard(value, {
      showToastr: false
    });
    setIsChecked(true);
    setTimeout(function () {
      setIsChecked(false);
    }, TIME_OUT); // Reset copied state after 2 seconds
  };
  React.useEffect(function () {
    if (!ref.current) return;
    var buttonWidth = ref.current.clientWidth;
    ref.current.style.minWidth = "".concat(buttonWidth, "px");
  }, []);
  var tooltipOptions = label ? null : ramda.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__*/jsxRuntime.jsx(Button, _objectSpread({
    ref: ref,
    style: style,
    "data-cy": "copy-to-clipboard-button",
    "data-testid": "copy-button",
    icon: buttonIcon,
    label: buttonLabel,
    tooltipProps: tooltipOptions,
    className: classnames("neeto-molecules-copy-button", _defineProperty(_defineProperty({}, className, className), "neeto-molecules-copy-button--active", isChecked)),
    onClick: onHandleClick
  }, otherProps));
};

module.exports = CopyToClipboardButton;
//# sourceMappingURL=CopyToClipboardButton.js.map
