"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.CopyToClipboardButton = CopyToClipboardButton;
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _faCopy = require("@fortawesome/free-solid-svg-icons/faCopy");
var _faCheck = require("@fortawesome/free-solid-svg-icons/faCheck");
var _fontAwesomeIcon = require("../font-awesome-icon");
var _button = require("../button");
var _tooltip = require("../tooltip");
var _constants = require("./constants");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
function CopyToClipboardButton({
  ariaLabel,
  ariaLabelledBy,
  value,
  copyTooltipText,
  copiedTooltipText,
  testId,
  className,
  isDisabled,
  copyLabel,
  onClick,
  accentType = _button.ACCENT_TYPES.ROUND_ICON
}) {
  const [isCopied, setIsCopied] = (0, _react.useState)(false);
  (0, _react.useEffect)(() => {
    let timer;
    if (isCopied) {
      timer = setTimeout(() => {
        setIsCopied(false);
      }, _constants.COPY_TIMEOUT);
    }
    return () => {
      clearTimeout(timer);
    };
  }, [isCopied]);
  const onCopy = (0, _react.useCallback)(() => {
    navigator.clipboard.writeText(value);
    setIsCopied(true);
    if (onClick) {
      onClick();
    }
  }, [value, onClick]);
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_button.Button, {
    isDisabled: isDisabled,
    onClick: onCopy,
    accentType: accentType,
    size: _button.BUTTON_SIZES.XS,
    "aria-label": ariaLabel,
    "aria-labelledby": ariaLabelledBy,
    tooltipDelayType: _tooltip.TOOLTIP_DELAY_TYPES.FAST,
    tooltipText: isCopied ? copiedTooltipText : copyTooltipText,
    tooltipPlacement: "top",
    className: (0, _classnames.default)('copy-to-clipboard-btn', {
      disabled: isDisabled
    }, {
      'with-label': !!copyLabel
    }, className),
    testId: testId,
    children: [!!copyLabel && /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
      className: "copy-label",
      children: copyLabel
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_fontAwesomeIcon.FontAwesomeIcon, {
      icon: isCopied ? _faCheck.faCheck : _faCopy.faCopy,
      className: (0, _classnames.default)(isCopied && 'copy-to-clipboard-icon-copied'),
      "test-id": `${testId}-icon`
    })]
  });
}