import _defineProperty from '@babel/runtime/helpers/defineProperty';
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProperties';
import { useState, useRef, useEffect, useLayoutEffect } from 'react';
import { Button, TooltipProvider, Tooltip, TooltipTrigger, TooltipContent } from '@bigbinary/neeto-atoms';
import classnames from 'classnames';
import { copyToClipboard } from '@bigbinary/neeto-commons-frontend/utils/general';
import { useTranslation } from 'react-i18next';
import { jsxs, jsx } from 'react/jsx-runtime';
import { c as createLucideIcon } from '../createLucideIcon-mNMhCCpf.js';

/**
 * @license lucide-react v1.7.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */


const __iconNode$1 = [["path", { d: "M20 6 9 17l-5-5", key: "1gmf2c" }]];
const Check = createLucideIcon("check", __iconNode$1);

/**
 * @license lucide-react v1.7.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */


const __iconNode = [
  ["rect", { width: "14", height: "14", x: "8", y: "8", rx: "2", ry: "2", key: "17jyea" }],
  ["path", { d: "M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2", key: "zix9uf" }]
];
const Copy = createLucideIcon("copy", __iconNode);

var TIME_OUT = 2000;

var _excluded = ["className", "icon", "label", "successLabel", "tooltipContent", "successTooltipContent", "value", "variant", "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,
    _ref$variant = _ref.variant,
    variant = _ref$variant === void 0 ? "outline" : _ref$variant,
    size = _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 timerRef = useRef();
  var resolvedSize = size || (label ? "default" : "icon");
  var onHandleClick = function onHandleClick(event) {
    event.preventDefault();
    event.stopPropagation();
    if (onClick) onClick(event);else copyToClipboard(value, {
      showToastr: false
    });
    clearTimeout(timerRef.current);
    setIsChecked(true);
    timerRef.current = setTimeout(function () {
      setIsChecked(false);
    }, TIME_OUT);
  };
  useEffect(function () {
    return function () {
      return clearTimeout(timerRef.current);
    };
  }, []);
  useLayoutEffect(function () {
    if (!ref.current) return;
    ref.current.style.minWidth = "";
    var buttonWidth = ref.current.clientWidth;
    ref.current.style.minWidth = "".concat(buttonWidth, "px");
  }, [resolvedSize, label]);
  var currentTooltipContent = isChecked ? successTooltipContent || t("neetoMolecules.common.copied") : tooltipContent || t("neetoMolecules.common.copy");
  var buttonLabel = isChecked && label ? successLabel || t("neetoMolecules.common.copied") : label;
  var ButtonIcon = isChecked ? Check : Icon;
  var button = /*#__PURE__*/jsxs(Button, _objectSpread(_objectSpread({
    ref: ref,
    "data-testid": "copy-to-clipboard-button",
    size: resolvedSize,
    variant: isChecked ? "default" : variant,
    className: classnames("neeto-molecules-copy-button", isChecked && "neeto-molecules-copy-button--active", className),
    onClick: onHandleClick
  }, otherProps), {}, {
    children: [/*#__PURE__*/jsx(ButtonIcon, {
      size: 16
    }), buttonLabel]
  }));
  if (label) return button;
  return /*#__PURE__*/jsx(TooltipProvider, {
    children: /*#__PURE__*/jsxs(Tooltip, _objectSpread(_objectSpread({}, tooltipProps), {}, {
      children: [/*#__PURE__*/jsx(TooltipTrigger, {
        asChild: true,
        children: button
      }), /*#__PURE__*/jsx(TooltipContent, {
        side: "top",
        children: currentTooltipContent
      })]
    }))
  });
};

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