"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var _chunkILBP4CTUcjs = require('./chunk-ILBP4CTU.cjs');



var _chunkAEJ7VTL4cjs = require('./chunk-AEJ7VTL4.cjs');


var _chunkL2UOQCGRcjs = require('./chunk-L2UOQCGR.cjs');


var _chunkJZ3TGO3Dcjs = require('./chunk-JZ3TGO3D.cjs');


var _chunkSNSG4HBQcjs = require('./chunk-SNSG4HBQ.cjs');


var _chunkWF7Z2VITcjs = require('./chunk-WF7Z2VIT.cjs');


var _chunkKW3JYLRIcjs = require('./chunk-KW3JYLRI.cjs');


var _chunkZ6EUSUADcjs = require('./chunk-Z6EUSUAD.cjs');

// src/components/ReadClipboardButton.tsx
var _styled = require('@emotion/styled'); var _styled2 = _interopRequireDefault(_styled);
var _react = require('react');
var _jsxruntime = require('@emotion/react/jsx-runtime');
function ReadClipboardButton({
  onChangeTextInput = () => {
  },
  onPasteValue,
  onRemoveValue,
  valueLoading = false,
  value,
  errorMsg,
  description,
  autoButtonText,
  onClickAutoButton,
  hint,
  clipboardCallbacks
}) {
  const bp = _chunkKW3JYLRIcjs.useBreakpoints.call(void 0, );
  const { canReadFromClipboard, readTextFromClipboard } = _chunkJZ3TGO3Dcjs.useClipboard.call(void 0, clipboardCallbacks);
  const [isEditingTextInput, setIsEditingTextInput] = _react.useState.call(void 0, false);
  const isDeleting = _react.useRef.call(void 0, false);
  const getTextFromClipboard = _react.useCallback.call(void 0, () => _chunkZ6EUSUADcjs.__async.call(void 0, this, null, function* () {
    const text = yield readTextFromClipboard();
    if (text !== null) {
      const value2 = text.trim();
      if (value2 === "") {
        setIsEditingTextInput(true);
      } else {
        onPasteValue(value2);
      }
    }
  }), [onPasteValue, readTextFromClipboard]);
  function onClick() {
    if (!isDeleting.current) {
      void getTextFromClipboard();
    } else {
      isDeleting.current = false;
    }
  }
  function onDeleteMouseDown(e) {
    e.stopPropagation();
    isDeleting.current = true;
    onRemoveValue();
  }
  function onDeleteMouseUp() {
    isDeleting.current = false;
  }
  const onKeyDown = _react.useCallback.call(void 0, 
    (event) => {
      const charCode = String.fromCharCode(event.which).toLowerCase();
      if (document.activeElement === document.body) {
        if (canReadFromClipboard) {
          if ((event.ctrlKey || event.metaKey) && charCode === "v") {
            event.preventDefault();
            void getTextFromClipboard();
            return;
          }
        }
        if (event.key === "Escape" || event.key === "Delete") {
          onRemoveValue();
        } else if (charCode.match(/^[a-z0-9]$/i)) {
          setIsEditingTextInput(true);
        }
      }
    },
    [getTextFromClipboard, canReadFromClipboard, onRemoveValue]
  );
  _react.useEffect.call(void 0, () => {
    document.addEventListener("keydown", onKeyDown);
    return () => {
      document.removeEventListener("keydown", onKeyDown);
    };
  }, [onKeyDown]);
  return (canReadFromClipboard || onClickAutoButton) && !bp.isSm() ? /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _react.Fragment, { children: [
    /* @__PURE__ */ _jsxruntime.jsx.call(void 0, 
      ReadClipboardButtonContainer,
      {
        onClick: onClickAutoButton ? onClickAutoButton : onClick,
        hasAddress: Boolean(value),
        hasError: Boolean(errorMsg),
        onMouseUp: onDeleteMouseUp,
        children: autoButtonText ? /* @__PURE__ */ _jsxruntime.jsx.call(void 0, 
          _chunkSNSG4HBQcjs.UnstyledButton,
          {
            onClick: onClickAutoButton,
            css: { cursor: "pointer" },
            children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, 
              _chunkL2UOQCGRcjs.Pill,
              {
                text: autoButtonText,
                icon: "MagicWand",
                cursor: "pointer",
                loading: valueLoading,
                onClickPillBody: onClickAutoButton
              }
            )
          }
        ) : /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _react.Fragment, { children: [
          !value && !isEditingTextInput ? /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _chunkSNSG4HBQcjs.UnstyledButton, { onClick, children: [
            /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkWF7Z2VITcjs.Icon, { name: "Copy", width: 19 }),
            /* @__PURE__ */ _jsxruntime.jsx.call(void 0, "div", { css: { marginTop: "8px" }, children: description })
          ] }) : null,
          value || isEditingTextInput ? /* @__PURE__ */ _jsxruntime.jsx.call(void 0, 
            _chunkL2UOQCGRcjs.Pill,
            {
              text: value,
              onDeleteMouseDown,
              loading: valueLoading,
              isEditing: isEditingTextInput,
              onChangeTextInput: (value2) => {
                onChangeTextInput(value2);
              },
              onFocusTextInput: () => setIsEditingTextInput(true),
              onBlurTextInput: () => setIsEditingTextInput(false),
              onClickPillBody: onClickAutoButton ? onClickAutoButton : () => setIsEditingTextInput(true)
            }
          ) : null
        ] })
      }
    ),
    /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkAEJ7VTL4cjs.InputSubtext, { text: errorMsg || hint, hasError: Boolean(errorMsg) })
  ] }) : /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _react.Fragment, { children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, 
    _chunkILBP4CTUcjs.TextInput,
    {
      onChange: onChangeTextInput,
      value,
      hint,
      error: errorMsg || void 0
    }
  ) });
}
var ReadClipboardButtonContainer = _styled2.default.div`
  ${({ theme, hasAddress, hasError }) => _chunkAEJ7VTL4cjs.inputBlockStyle.call(void 0, { theme, hasValue: hasAddress, hasError })}
`;



exports.ReadClipboardButton = ReadClipboardButton;
