import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import React, { useState } from "react";
import { Input } from "antd";
import classNames from "classnames";
import { useEventListener, useUpdateEffect } from "ahooks";
import Delete from "@pisell/icon/es/Delete";
import "./index.less";
//#region src/components/virtual-keyboard/VirtualKeyInput/index.tsx
const list = ["Backspace"];
const VirtualKeyInput = (props) => {
	var _ref;
	const { previewValue = props.value, value, showDelete = true, onValueSelect, defaultSelect, style } = props;
	const [select, setSelect] = useState(defaultSelect !== null && defaultSelect !== void 0 ? defaultSelect : false);
	useEventListener("keydown", (ev) => {
		if (list.includes(ev.key)) _onDelete === null || _onDelete === void 0 || _onDelete();
	});
	const handleSelectChange = (selected) => {
		onValueSelect === null || onValueSelect === void 0 || onValueSelect(selected);
		setSelect(selected);
	};
	useUpdateEffect(() => {
		handleSelectChange(false);
	}, [value]);
	const _onDelete = () => {
		if (select) {
			var _props$onChange;
			(_props$onChange = props.onChange) === null || _props$onChange === void 0 || _props$onChange.call(props, { target: { value: "" } });
			handleSelectChange(false);
			return;
		}
		let _val = props.value + "";
		_val = _val.slice(0, _val.length - 1);
		props.onChange && props.onChange({ target: { value: _val } });
	};
	return /* @__PURE__ */ React.createElement("div", {
		className: "virtual-keyboard-input-wrap",
		style: { backgroundColor: (style === null || style === void 0 ? void 0 : style.backgroundColor) || "#ffffff" }
	}, props.renderInput ? /* @__PURE__ */ React.createElement("div", {
		className: classNames("virtual-keyboard-input", { "virtual-keyboard-input-select": select }),
		onClick: () => {
			if (value) handleSelectChange(!select);
		},
		style: { color: (style === null || style === void 0 ? void 0 : style.color) || "#000000" }
	}, props.renderInput(props)) : /* @__PURE__ */ React.createElement(Input, _objectSpread2(_objectSpread2({
		className: "virtual-keyboard-input",
		bordered: false
	}, props), {}, {
		readOnly: true,
		value: previewValue
	})), ((_ref = `${value !== null && value !== void 0 ? value : ""}`) === null || _ref === void 0 ? void 0 : _ref.length) && showDelete ? /* @__PURE__ */ React.createElement("div", {
		className: "virtual-keyboard-input-delete",
		onClick: _onDelete
	}, /* @__PURE__ */ React.createElement(Delete, { className: "virtual-keyboard-input-delete-icon" })) : null);
};
//#endregion
export { VirtualKeyInput as default };
