var _excluded = ["enableQRScanner", "value", "onChange", "renderMode", "innerRef", "className"];
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import { Input as AntInput } from 'antd';
import React, { useState, useEffect } from 'react';
import IconFont from "./../../../../components/iconfont";
import ReadPretty from "./ReadPretty";
import { withMode } from "../../dataSourceForm/utils";
import PisellQRScanner from "../../../../components/pisellQRScanner";
var Input = function Input(props) {
  // 明确解构renderMode，避免它被传递给底层DOM元素
  var enableQRScanner = props.enableQRScanner,
    propValue = props.value,
    onChange = props.onChange,
    renderMode = props.renderMode,
    innerRef = props.innerRef,
    className = props.className,
    restProps = _objectWithoutProperties(props, _excluded);

  // 使用内部状态来管理输入值
  var _useState = useState(propValue || ''),
    _useState2 = _slicedToArray(_useState, 2),
    inputValue = _useState2[0],
    setInputValue = _useState2[1];

  // 当props.value变化时更新内部状态
  useEffect(function () {
    if (propValue !== undefined && propValue !== inputValue) {
      setInputValue(propValue);
    }
  }, [propValue]);

  // 扫码对话框显示状态
  var _useState3 = useState(false),
    _useState4 = _slicedToArray(_useState3, 2),
    scanVisible = _useState4[0],
    setScanVisible = _useState4[1];

  // 创建一个用于设置输入框值的函数
  var setValue = function setValue(value) {
    console.log('设置输入框值:', value);

    // 1. 更新内部状态
    setInputValue(value);

    // 2. 通知父组件值已变化
    if (typeof onChange === 'function') {
      try {
        // 创建模拟事件
        var syntheticEvent = {
          target: {
            value: value
          },
          currentTarget: {
            value: value
          },
          preventDefault: function preventDefault() {},
          stopPropagation: function stopPropagation() {}
        };
        onChange(syntheticEvent);
      } catch (err) {
        console.error('调用onChange失败:', err);
      }
    }
  };

  // 处理输入变化
  var handleInputChange = function handleInputChange(e) {
    var newValue = e.target.value;

    // 更新内部状态
    setInputValue(newValue);

    // 调用原始onChange
    if (typeof onChange === 'function') {
      onChange(e);
    }
  };

  // 处理扫码按钮点击
  var handleScanClick = function handleScanClick() {
    setScanVisible(true);
  };

  // 处理扫码完成
  var handleScanComplete = function handleScanComplete(value) {
    setValue(value);
  };

  // 处理扫码关闭
  var handleScanClose = function handleScanClose() {
    setScanVisible(false);
  };

  // 仅在启用扫码功能时显示扫码图标
  var scanSuffix = enableQRScanner ? /*#__PURE__*/React.createElement(IconFont, {
    type: "pisell2-scan2",
    className: "scan-button-icon",
    style: {
      fontSize: 24,
      color: '#fff',
      cursor: 'pointer'
    },
    onClick: handleScanClick
  }) : null;

  // 如果是只读模式，使用ReadPretty组件渲染
  if (renderMode === 'view' || renderMode === 'tableView') {
    return /*#__PURE__*/React.createElement(ReadPretty, props);
  }

  // 注入自定义样式到文档
  useEffect(function () {
    if (enableQRScanner) {
      // 创建一个样式元素
      var styleEl = document.createElement('style');
      styleEl.id = 'pisell-qr-scanner-style';
      styleEl.innerHTML = "\n        .pisell-input-with-scanner .pisell-lowcode-input-group .pisell-lowcode-input-group-addon {\n          background-color: var(--primary-600, #7F56D9);\n        }\n      ";

      // 添加到文档中
      document.head.appendChild(styleEl);

      // 清理函数
      return function () {
        var existingStyle = document.getElementById('pisell-qr-scanner-style');
        if (existingStyle) {
          document.head.removeChild(existingStyle);
        }
      };
    }
  }, [enableQRScanner]);
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(AntInput, _extends({}, restProps, {
    ref: innerRef || undefined,
    className: "".concat(className || '', " ").concat(enableQRScanner ? 'pisell-input-with-scanner' : ''),
    addonAfter: scanSuffix,
    value: inputValue,
    onChange: handleInputChange
  })), /*#__PURE__*/React.createElement(PisellQRScanner, {
    visible: scanVisible,
    onClose: handleScanClose,
    onScan: handleScanComplete
  }));
};
var InputWithMode = withMode(Input, ReadPretty);
export default InputWithMode;