import { useState } from "react";
const fallbackCopy = (text) => {
  return new Promise((resolve, reject) => {
    try {
      const textarea = document.createElement("textarea");
      textarea.value = text;
      textarea.style.cssText = "position:fixed;left:-9999px;opacity:0;";
      document.body.appendChild(textarea);
      textarea.focus();
      textarea.select();
      const successful = document.execCommand("copy");
      textarea.remove();
      if (successful) {
        resolve(void 0);
      } else {
        reject(new Error("Fallback copy failed"));
      }
    } catch (err) {
      reject(err);
    }
  });
};
function useFallbackClipboard({ timeout = 2e3 } = {}) {
  const [error, setError] = useState(null);
  const [copied, setCopied] = useState(false);
  const [copyTimeout, setCopyTimeout] = useState(null);
  const handleCopyResult = (value) => {
    window.clearTimeout(copyTimeout);
    setCopyTimeout(window.setTimeout(() => setCopied(false), timeout));
    setCopied(value);
  };
  const copy = (valueToCopy) => {
    fallbackCopy(valueToCopy).then(() => handleCopyResult(true)).catch((err) => setError(err));
  };
  const reset = () => {
    setCopied(false);
    setError(null);
    window.clearTimeout(copyTimeout);
  };
  return { copy, reset, error, copied };
}
export {
  useFallbackClipboard
};
