"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
require("core-js/modules/es.array.iterator.js");
require("core-js/modules/web.dom-collections.iterator.js");
var _react = _interopRequireWildcard(require("react"));
var _input = _interopRequireDefault(require("../input"));
var _wauProvider = require("../wau-provider");
require("./style");
var _jsxRuntime = require("react/jsx-runtime");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
const CompositionInput = /*#__PURE__*/_react.default.forwardRef((props, ref) => {
  const compositionRef = (0, _react.useRef)(true);
  const [innerValue, setInnerValue] = (0, _react.useState)(props.value);
  (0, _react.useEffect)(() => {
    setInnerValue(props.value);
  }, [props.value]);
  const {
    composition,
    ...rest
  } = props;
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_input.default, {
    ref: ref,
    ...rest,
    ...(composition ? {
      value: innerValue,
      onCompositionStart: () => {
        compositionRef.current = false;
      },
      onCompositionEnd: e => {
        if (compositionRef.current === undefined) {
          var _props$onChange;
          props === null || props === void 0 ? void 0 : (_props$onChange = props.onChange) === null || _props$onChange === void 0 ? void 0 : _props$onChange.call(props, e);
        }
        compositionRef.current = true;
      },
      onChange: e => {
        setInnerValue(e.target.value);
        if (compositionRef.current) {
          var _props$onChange2;
          props === null || props === void 0 ? void 0 : (_props$onChange2 = props.onChange) === null || _props$onChange2 === void 0 ? void 0 : _props$onChange2.call(props, e);
        } else {
          compositionRef.current = undefined;
        }
      }
    } : {})
  });
});
CompositionInput.displayName = 'CompositionInput';
/**
 * 最基本的组件，就是个普通的 Input
 *
 * @param
 */
const FieldText = ({
  text,
  mode,
  render,
  renderFormItem,
  fieldProps
}, ref) => {
  const intl = (0, _wauProvider.useIntl)();
  const inputRef = (0, _react.useRef)();
  (0, _react.useImperativeHandle)(ref, () => ({
    ...(inputRef.current || {})
  }), [inputRef.current]);
  if (mode === 'read') {
    const dom = text || '-';
    if (render) {
      return render(text, {
        mode,
        ...fieldProps
      }, /*#__PURE__*/(0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {
        children: dom
      }));
    }
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {
      children: dom
    });
  }
  if (mode === 'edit' || mode === 'update') {
    const placeholder = intl.getMessage('tableForm.inputPlaceholder', '请输入');
    const dom = /*#__PURE__*/(0, _jsxRuntime.jsx)(CompositionInput, {
      placeholder: placeholder,
      ref: inputRef,
      allowClear: true,
      ...fieldProps
    });
    if (renderFormItem) {
      return renderFormItem(text, {
        mode,
        ...fieldProps
      }, dom);
    }
    return dom;
  }
  return null;
};
var _default = /*#__PURE__*/_react.default.forwardRef(FieldText);
exports.default = _default;