import {
  __async
} from "./chunk-W7FAWX6T.js";

// src/hooks/useClipboard.tsx
import { useCallback, useMemo, useState } from "react";
function useClipboard(clipboardArgs) {
  var _a, _b;
  const { onWriteEmptyText, onWriteError, onWrite, onReadError } = useMemo(
    () => ({
      onWriteEmptyText: clipboardArgs == null ? void 0 : clipboardArgs.onWriteEmptyText,
      onWriteError: clipboardArgs == null ? void 0 : clipboardArgs.onWriteError,
      onWrite: clipboardArgs == null ? void 0 : clipboardArgs.onWrite,
      onReadError: clipboardArgs == null ? void 0 : clipboardArgs.onReadError
    }),
    [clipboardArgs]
  );
  const [canReadFromClipboard, setCanReadFromClipboard] = useState(
    typeof ((_a = navigator.clipboard) == null ? void 0 : _a.readText) !== "undefined"
  );
  const [canWriteToClipboard, setCanWriteToClipboard] = useState(
    typeof ((_b = navigator.clipboard) == null ? void 0 : _b.writeText) !== "undefined"
  );
  const writeTextToClipboard = useCallback(
    (text) => __async(this, null, function* () {
      try {
        if (!text.trim()) {
          if (onWriteEmptyText) {
            onWriteEmptyText();
          }
        } else {
          yield navigator.clipboard.writeText(text);
          if (onWrite) {
            onWrite();
          }
        }
      } catch (error) {
        if (onWriteError) {
          onWriteError();
        }
        setCanWriteToClipboard(false);
      }
    }),
    [onWrite, onWriteEmptyText, onWriteError]
  );
  const readTextFromClipboard = useCallback(() => __async(this, null, function* () {
    try {
      const text = yield navigator.clipboard.readText();
      return text;
    } catch (error) {
      if (onReadError) {
        onReadError();
      }
      setCanReadFromClipboard(false);
      return null;
    }
  }), [onReadError]);
  const clipboard = useMemo(
    () => ({
      canReadFromClipboard,
      canWriteToClipboard,
      readTextFromClipboard,
      writeTextToClipboard
    }),
    [
      canReadFromClipboard,
      canWriteToClipboard,
      readTextFromClipboard,
      writeTextToClipboard
    ]
  );
  return clipboard;
}

export {
  useClipboard
};
