import "core-js/modules/es.array.iterator.js";
import "core-js/modules/web.dom-collections.iterator.js";
import React, { useRef, useImperativeHandle, useState, useEffect } from 'react';
import Input from '../input';
import { useIntl } from '../wau-provider';
import './style';
import { jsx as _jsx } from "react/jsx-runtime";
import { Fragment as _Fragment } from "react/jsx-runtime";
const CompositionInput = /*#__PURE__*/React.forwardRef((props, ref) => {
  const compositionRef = useRef(true);
  const [innerValue, setInnerValue] = useState(props.value);
  useEffect(() => {
    setInnerValue(props.value);
  }, [props.value]);
  const {
    composition,
    ...rest
  } = props;
  return /*#__PURE__*/_jsx(Input, {
    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 = useIntl();
  const inputRef = useRef();
  useImperativeHandle(ref, () => ({
    ...(inputRef.current || {})
  }), [inputRef.current]);
  if (mode === 'read') {
    const dom = text || '-';
    if (render) {
      return render(text, {
        mode,
        ...fieldProps
      }, /*#__PURE__*/_jsx(_Fragment, {
        children: dom
      }));
    }
    return /*#__PURE__*/_jsx(_Fragment, {
      children: dom
    });
  }
  if (mode === 'edit' || mode === 'update') {
    const placeholder = intl.getMessage('tableForm.inputPlaceholder', '请输入');
    const dom = /*#__PURE__*/_jsx(CompositionInput, {
      placeholder: placeholder,
      ref: inputRef,
      allowClear: true,
      ...fieldProps
    });
    if (renderFormItem) {
      return renderFormItem(text, {
        mode,
        ...fieldProps
      }, dom);
    }
    return dom;
  }
  return null;
};
export default /*#__PURE__*/React.forwardRef(FieldText);