Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const require_chunk = require("../chunk-BN4qSVkS.cjs");
const require_styles_breakpoints = require("../styles/breakpoints.cjs");
const require_components_Icon_Icon = require("./Icon/Icon.cjs");
const require_hooks_useClipboard = require("../hooks/useClipboard.cjs");
const require_components_UnstyledButton = require("./UnstyledButton.cjs");
const require_styles_fields = require("../styles/fields.cjs");
const require_components_Pill = require("./Pill.cjs");
const require_components_TextInput = require("./TextInput.cjs");
let _emotion_styled = require("@emotion/styled");
_emotion_styled = require_chunk.__toESM(_emotion_styled);
let react = require("react");
let _emotion_react_jsx_runtime = require("@emotion/react/jsx-runtime");
//#region src/components/ReadClipboardButton.tsx
function ReadClipboardButton({ onChangeTextInput = () => {}, onPasteValue, onRemoveValue, valueLoading = false, value, errorMsg, description, autoButtonText, onClickAutoButton, hint, clipboardCallbacks }) {
	const bp = require_styles_breakpoints.useBreakpoints();
	const { canReadFromClipboard, readTextFromClipboard } = require_hooks_useClipboard.useClipboard(clipboardCallbacks);
	const [isEditingTextInput, setIsEditingTextInput] = (0, react.useState)(false);
	const isDeleting = (0, react.useRef)(false);
	const getTextFromClipboard = (0, react.useCallback)(async () => {
		const text = await readTextFromClipboard();
		if (text !== null) {
			const value = text.trim();
			if (value === "") setIsEditingTextInput(true);
			else onPasteValue(value);
		}
	}, [onPasteValue, readTextFromClipboard]);
	function onClick() {
		if (!isDeleting.current) getTextFromClipboard();
		else isDeleting.current = false;
	}
	function onDeleteMouseDown(e) {
		e.stopPropagation();
		isDeleting.current = true;
		onRemoveValue();
	}
	function onDeleteMouseUp() {
		isDeleting.current = false;
	}
	const onKeyDown = (0, react.useCallback)((event) => {
		const charCode = String.fromCharCode(event.which).toLowerCase();
		if (document.activeElement === document.body) {
			if (canReadFromClipboard) {
				if ((event.ctrlKey || event.metaKey) && charCode === "v") {
					event.preventDefault();
					getTextFromClipboard();
					return;
				}
			}
			if (event.key === "Escape" || event.key === "Delete") onRemoveValue();
			else if (charCode.match(/^[a-z0-9]$/i)) setIsEditingTextInput(true);
		}
	}, [
		getTextFromClipboard,
		canReadFromClipboard,
		onRemoveValue
	]);
	(0, react.useEffect)(() => {
		document.addEventListener("keydown", onKeyDown);
		return () => {
			document.removeEventListener("keydown", onKeyDown);
		};
	}, [onKeyDown]);
	return (canReadFromClipboard || onClickAutoButton) && !bp.isSm() ? /* @__PURE__ */ (0, _emotion_react_jsx_runtime.jsxs)(react.Fragment, { children: [/* @__PURE__ */ (0, _emotion_react_jsx_runtime.jsx)(ReadClipboardButtonContainer, {
		onClick: onClickAutoButton ? onClickAutoButton : onClick,
		hasAddress: Boolean(value),
		hasError: Boolean(errorMsg),
		onMouseUp: onDeleteMouseUp,
		children: autoButtonText ? /* @__PURE__ */ (0, _emotion_react_jsx_runtime.jsx)(require_components_UnstyledButton.UnstyledButton, {
			onClick: onClickAutoButton,
			css: { cursor: "pointer" },
			children: /* @__PURE__ */ (0, _emotion_react_jsx_runtime.jsx)(require_components_Pill.Pill, {
				text: autoButtonText,
				icon: "MagicWand",
				cursor: "pointer",
				loading: valueLoading,
				onClickPillBody: onClickAutoButton
			})
		}) : /* @__PURE__ */ (0, _emotion_react_jsx_runtime.jsxs)(react.Fragment, { children: [!value && !isEditingTextInput ? /* @__PURE__ */ (0, _emotion_react_jsx_runtime.jsxs)(require_components_UnstyledButton.UnstyledButton, {
			onClick,
			children: [/* @__PURE__ */ (0, _emotion_react_jsx_runtime.jsx)(require_components_Icon_Icon.Icon, {
				name: "Copy",
				width: 19
			}), /* @__PURE__ */ (0, _emotion_react_jsx_runtime.jsx)("div", {
				css: { marginTop: "8px" },
				children: description
			})]
		}) : null, value || isEditingTextInput ? /* @__PURE__ */ (0, _emotion_react_jsx_runtime.jsx)(require_components_Pill.Pill, {
			text: value,
			onDeleteMouseDown,
			loading: valueLoading,
			isEditing: isEditingTextInput,
			onChangeTextInput: (value) => {
				onChangeTextInput(value);
			},
			onFocusTextInput: () => setIsEditingTextInput(true),
			onBlurTextInput: () => setIsEditingTextInput(false),
			onClickPillBody: onClickAutoButton ? onClickAutoButton : () => setIsEditingTextInput(true)
		}) : null] })
	}), /* @__PURE__ */ (0, _emotion_react_jsx_runtime.jsx)(require_styles_fields.InputSubtext, {
		text: errorMsg || hint,
		hasError: Boolean(errorMsg)
	})] }) : /* @__PURE__ */ (0, _emotion_react_jsx_runtime.jsx)(react.Fragment, { children: /* @__PURE__ */ (0, _emotion_react_jsx_runtime.jsx)(require_components_TextInput.TextInput, {
		onChange: onChangeTextInput,
		value,
		hint,
		error: errorMsg || void 0
	}) });
}
const ReadClipboardButtonContainer = _emotion_styled.default.div`
  ${({ theme, hasAddress, hasError }) => require_styles_fields.inputBlockStyle({
	theme,
	hasValue: hasAddress,
	hasError
})}
`;
//#endregion
exports.ReadClipboardButton = ReadClipboardButton;
