"use strict";

exports.__esModule = true;
exports.useCopyClipboard = useCopyClipboard;
var _react = require("react");
/** an object for value, onCopy, and resetTimeout props */

/**
 * Hook to use copy to clipboard functionality.
 * @returns object with values:
 * - isCopied - boolean with status of clipboard action
 * - copyToClipboard - a return function that allows to initiate copying
 * - reset - an optional return function
 */
function useCopyClipboard(_ref) {
  var {
    value,
    onCopy,
    resetTimeout
  } = _ref;
  var [isCopied, setIsCopied] = (0, _react.useState)(false);
  var copyToClipboard = (0, _react.useCallback)(() => {
    navigator.clipboard.writeText(value).then(() => {
      setIsCopied(true);
      onCopy == null || onCopy();
    });
  }, [value, onCopy]);
  var reset = (0, _react.useCallback)(() => {
    setIsCopied(false);
  }, [setIsCopied]);
  (0, _react.useEffect)(() => {
    var timeout;
    if (isCopied && resetTimeout) {
      timeout = setTimeout(reset, resetTimeout);
    }
    return () => {
      clearTimeout(timeout);
    };
  }, [isCopied, reset, resetTimeout]);
  (0, _react.useEffect)(reset, [value, reset]);
  return {
    isCopied,
    copyToClipboard,
    reset
  };
}
//# sourceMappingURL=useCopyClipboard.js.map