import { _objectSpread2 } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { _objectWithoutProperties } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js";
import IconFont from "../../../iconfont/index.js";
import { withMode } from "../../dataSourceForm/utils.js";
import PisellQRScanner from "../../../pisellQRScanner/index.js";
import InputReadPretty from "./ReadPretty.js";
import React, { useEffect, useState } from "react";
import { Input } from "antd";
//#region src/components/dataSourceComponents/fields/Input/WithMode.tsx
const _excluded = [
	"enableQRScanner",
	"value",
	"onChange",
	"renderMode",
	"innerRef",
	"className"
];
const Input$1 = (props) => {
	const { enableQRScanner, value: propValue, onChange, renderMode, innerRef, className } = props, restProps = _objectWithoutProperties(props, _excluded);
	const [inputValue, setInputValue] = useState(propValue || "");
	useEffect(() => {
		if (propValue !== void 0 && propValue !== inputValue) setInputValue(propValue);
	}, [propValue]);
	const [scanVisible, setScanVisible] = useState(false);
	const setValue = (value) => {
		console.log("设置输入框值:", value);
		setInputValue(value);
		if (typeof onChange === "function") try {
			onChange({
				target: { value },
				currentTarget: { value },
				preventDefault: () => {},
				stopPropagation: () => {}
			});
		} catch (err) {
			console.error("调用onChange失败:", err);
		}
	};
	const handleInputChange = (e) => {
		const newValue = e.target.value;
		setInputValue(newValue);
		if (typeof onChange === "function") onChange(e);
	};
	const handleScanClick = () => {
		setScanVisible(true);
	};
	const handleScanComplete = (value) => {
		setValue(value);
	};
	const handleScanClose = () => {
		setScanVisible(false);
	};
	const scanSuffix = enableQRScanner ? /* @__PURE__ */ React.createElement(IconFont, {
		type: "pisell2-scan2",
		className: "scan-button-icon",
		style: {
			fontSize: 24,
			color: "#fff",
			cursor: "pointer"
		},
		onClick: handleScanClick
	}) : null;
	if (renderMode === "view" || renderMode === "tableView") return /* @__PURE__ */ React.createElement(InputReadPretty, props);
	useEffect(() => {
		if (enableQRScanner) {
			const styleEl = document.createElement("style");
			styleEl.id = "pisell-qr-scanner-style";
			styleEl.innerHTML = `
        .pisell-input-with-scanner .pisell-lowcode-input-group .pisell-lowcode-input-group-addon {
          background-color: var(--primary-600, #7F56D9);
        }
      `;
			document.head.appendChild(styleEl);
			return () => {
				const existingStyle = document.getElementById("pisell-qr-scanner-style");
				if (existingStyle) document.head.removeChild(existingStyle);
			};
		}
	}, [enableQRScanner]);
	return /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(Input, _objectSpread2(_objectSpread2({}, restProps), {}, {
		ref: innerRef || void 0,
		className: `${className || ""} ${enableQRScanner ? "pisell-input-with-scanner" : ""}`,
		addonAfter: scanSuffix,
		value: inputValue,
		onChange: handleInputChange
	})), /* @__PURE__ */ React.createElement(PisellQRScanner, {
		visible: scanVisible,
		onClose: handleScanClose,
		onScan: handleScanComplete
	}));
};
const InputWithMode = withMode(Input$1, InputReadPretty);
//#endregion
export { InputWithMode as default };
