"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const jsxRuntime = require("react/jsx-runtime");
const React = require("react");
const kit_Icon = require("./Icon.cjs");
const kit_utils_functions = require("./utils/functions.cjs");
const kit_Button = require("./Button.cjs");
const kit_Toast = require("./Toast.cjs");
const kit_Tooltip = require("./Tooltip.cjs");
const TOOLTIP_LABEL_DEFAULT = "Copy to clipboard";
const TOOLTIP_LABEL_SUCCESS = "Copied!";
const ClipboardButton = ({
  copiedMessage = TOOLTIP_LABEL_SUCCESS,
  disabled,
  getContent,
  tooltipPlacement = "top",
  onCopy
}) => {
  const [iconName, setIconName] = React.useState("clipboard");
  const [tooltipLabel, setTooltipLabel] = React.useState(TOOLTIP_LABEL_DEFAULT);
  const [tooltipOpen, setTooltipOpen] = React.useState(false);
  const buttonRef = React.useRef(null);
  const { openToast } = kit_Toast.useToast();
  const icon = React.useMemo(() => /* @__PURE__ */ jsxRuntime.jsx(kit_Icon.default, { name: iconName, title: TOOLTIP_LABEL_DEFAULT }), [iconName]);
  const handleCopyToClipboard = React.useCallback(async () => {
    try {
      await kit_utils_functions.copyToClipboard(getContent());
      setTooltipLabel(copiedMessage);
      setIconName("checkmark");
      onCopy == null ? void 0 : onCopy();
    } catch (e) {
      setTooltipOpen(false);
      openToast({
        description: e == null ? void 0 : e.message,
        severity: "Error",
        title: "Unable to Copy to Clipboard"
      });
    }
  }, [copiedMessage, getContent, onCopy, openToast]);
  React.useEffect(() => {
    const button = buttonRef.current;
    const onMouseEnter = () => {
      setTooltipLabel(TOOLTIP_LABEL_DEFAULT);
      setIconName("clipboard");
      setTooltipOpen(true);
    };
    const onMouseLeave = () => {
      setTooltipOpen(false);
      setIconName("clipboard");
    };
    button == null ? void 0 : button.addEventListener("mouseenter", onMouseEnter);
    button == null ? void 0 : button.addEventListener("mouseleave", onMouseLeave);
    return () => {
      button == null ? void 0 : button.removeEventListener("mouseenter", onMouseEnter);
      button == null ? void 0 : button.removeEventListener("mouseleave", onMouseLeave);
    };
  }, []);
  return /* @__PURE__ */ jsxRuntime.jsx(kit_Tooltip, { content: tooltipLabel, open: tooltipOpen, placement: tooltipPlacement, children: /* @__PURE__ */ jsxRuntime.jsx(
    kit_Button,
    {
      "aria-label": tooltipLabel,
      disabled,
      icon,
      ref: buttonRef,
      onClick: handleCopyToClipboard
    }
  ) });
};
exports.TOOLTIP_LABEL_DEFAULT = TOOLTIP_LABEL_DEFAULT;
exports.default = ClipboardButton;
