import { jsxs as C, jsx as t } from "react/jsx-runtime";
import { useState as c, useEffect as x } from "react";
import { ActiveCopyButtonIcon as A, CopyButtonIcon as v } from "../../icons/index.js";
import { cn as I } from "../../utils/cn.js";
import { copyToClipboard as _ } from "../../utils/copy-to-clipboard.js";
const h = "Copy the contents from the code block", E = "Copy", O = "Copied!", B = ({
  textToCopy: l,
  onCopied: o,
  className: p,
  showTooltip: d = !0,
  codeBlockTheme: r,
  copyButtonAriaLabel: u = h,
  tooltipCopyText: f = E,
  tooltipCopiedText: m = O,
  ...T
}) => {
  const e = l.trim(), [s, i] = c(!1), [b, a] = c(!1);
  if (x(() => {
    typeof window < "u" && !navigator.clipboard ? (console.warn(
      "The browser's Clipboard API is unavailable. The Clipboard API is only available on HTTPS."
    ), a(!0)) : a(!1);
  }, []), !e || b)
    return null;
  const y = async () => {
    const n = await _(e);
    o && o(n, e), n === "success" && (i(!0), setTimeout(() => {
      i(!1);
    }, 2e3));
  };
  return /* @__PURE__ */ C("div", { className: I("relative z-10 select-none", p), children: [
    /* @__PURE__ */ t(
      "button",
      {
        ...T,
        "aria-label": u,
        className: "peer group/copy-button flex size-[26px] items-center justify-center rounded-md backdrop-blur",
        "data-testid": "copy-code-button",
        onClick: y,
        type: "button",
        children: s ? /* @__PURE__ */ t(A, { codeBlockTheme: r }) : /* @__PURE__ */ t(v, { codeBlockTheme: r })
      }
    ),
    d && /* @__PURE__ */ t(
      "div",
      {
        "aria-hidden": !0,
        className: "absolute top-11 left-1/2 -translate-x-1/2 -translate-y-1/2 transform whitespace-nowrap rounded-lg bg-primary-dark px-1.5 py-0.5 text-tooltip-foreground text-xs opacity-0 peer-hover:opacity-100",
        children: s ? m : f
      }
    )
  ] });
};
export {
  B as CopyToClipboardButton
};
