import React__default, { useRef, useState, useContext, useImperativeHandle, useMemo } from "react";
import cs from "../_util/classNames.js";
import autoSizeTextAreaHeight from "./autoSizeTextAreaHeight.js";
import omit from "../_util/omit.js";
import { ConfigContext } from "../ConfigProvider/index.js";
import { formatValue } from "./input.js";
import useMergeValue from "../_util/hooks/useMergeValue.js";
import IconClose from "../../icon/react-icon/IconClose/index.js";
import IconHover from "../_class/icon-hover.js";
import { isObject } from "../_util/is.js";
import useIsomorphicLayoutEffect from "../_util/hooks/useIsomorphicLayoutEffect.js";
import useComposition from "./useComposition.js";
import { jsx, jsxs } from "../../../../../../@emotion_react@11.10.4_3toe27fv7etiytxb5kxc7fxaw4/node_modules/@emotion/react/jsx-runtime/dist/emotion-react-jsx-runtime.browser.esm.js";
var __assign = globalThis && globalThis.__assign || function() {
  __assign = Object.assign || function(t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
      s = arguments[i];
      for (var p in s)
        if (Object.prototype.hasOwnProperty.call(s, p))
          t[p] = s[p];
    }
    return t;
  };
  return __assign.apply(this, arguments);
};
var __rest = globalThis && globalThis.__rest || function(s, e) {
  var t = {};
  for (var p in s)
    if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
      t[p] = s[p];
  if (s != null && typeof Object.getOwnPropertySymbols === "function")
    for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
      if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
        t[p[i]] = s[p[i]];
    }
  return t;
};
var __read = globalThis && globalThis.__read || function(o, n) {
  var m = typeof Symbol === "function" && o[Symbol.iterator];
  if (!m)
    return o;
  var i = m.call(o), r, ar = [], e;
  try {
    while ((n === void 0 || n-- > 0) && !(r = i.next()).done)
      ar.push(r.value);
  } catch (error) {
    e = {
      error
    };
  } finally {
    try {
      if (r && !r.done && (m = i["return"]))
        m.call(i);
    } finally {
      if (e)
        throw e.error;
    }
  }
  return ar;
};
var TextArea = function(props, ref) {
  var _a, _b, _c;
  var className = props.className, style = props.style, wrapperStyle = props.wrapperStyle, placeholder = props.placeholder, disabled = props.disabled, error = props.error, propMaxLength = props.maxLength, showWordLimit = props.showWordLimit, allowClear = props.allowClear, onChange = props.onChange, onClear = props.onClear, onKeyDown = props.onKeyDown, onPressEnter = props.onPressEnter, rest = __rest(props, ["className", "style", "wrapperStyle", "placeholder", "disabled", "error", "maxLength", "showWordLimit", "allowClear", "onChange", "onClear", "onKeyDown", "onPressEnter"]);
  var wordLimitMaxLength = isObject(propMaxLength) ? propMaxLength.length : propMaxLength;
  var maxLength = isObject(propMaxLength) ? propMaxLength.errorOnly ? void 0 : propMaxLength.length : propMaxLength;
  var textareaRef = useRef();
  var _d = __read(useState({}), 2), textAreaStyle = _d[0], setTextAreaStyle = _d[1];
  var _e = __read(useMergeValue("", {
    defaultValue: "defaultValue" in props ? formatValue(props.defaultValue, maxLength) : void 0,
    value: "value" in props ? formatValue(props.value, maxLength) : void 0
  }), 2), value = _e[0], setValue = _e[1];
  var _f = useComposition({
    value,
    maxLength,
    onChange,
    onKeyDown,
    onPressEnter,
    beforeTriggerValueChangeCallback: function(v) {
      if (!("value" in props) && (maxLength === void 0 || v.length <= maxLength)) {
        setValue(v);
      }
    }
  }), compositionValue = _f.compositionValue, compositionHandler = _f.compositionHandler, valueChangeHandler = _f.valueChangeHandler, keyDownHandler = _f.keyDownHandler, triggerValueChangeCallback = _f.triggerValueChangeCallback;
  var _g = useContext(ConfigContext), getPrefixCls = _g.getPrefixCls, rtl = _g.rtl;
  var prefixCls = getPrefixCls("textarea");
  if (disabled) {
    textAreaStyle.resize = "none";
  }
  var onFocus = function() {
    if (textareaRef.current && textareaRef.current.focus) {
      if (textareaRef.current.setSelectionRange) {
        var caretPos = textareaRef.current.textContent.length;
        textareaRef.current.setSelectionRange(caretPos, caretPos);
      }
      textareaRef.current.focus();
    }
  };
  var resizeTextAreaHeight = function() {
    var textAreaStyle2 = autoSizeTextAreaHeight(props.autoSize, textareaRef.current);
    if (textAreaStyle2) {
      setTextAreaStyle(textAreaStyle2);
    }
  };
  var handleClearClick = function(e) {
    e.stopPropagation();
    onFocus();
    triggerValueChangeCallback("", e);
    onClear && onClear();
  };
  useIsomorphicLayoutEffect(function() {
    resizeTextAreaHeight();
  }, [value]);
  useImperativeHandle(ref, function() {
    return {
      dom: textareaRef.current,
      focus: function() {
        onFocus();
      },
      blur: function() {
        textareaRef.current && textareaRef.current.blur && textareaRef.current.blur();
      }
    };
  }, []);
  var valueLength = value ? value.length : 0;
  var withWrapper = wordLimitMaxLength && showWordLimit || allowClear;
  var lengthError = useMemo(function() {
    if (!maxLength && wordLimitMaxLength) {
      return valueLength > wordLimitMaxLength;
    }
    return false;
  }, [valueLength, wordLimitMaxLength, maxLength]);
  var classNames = cs(prefixCls, (_a = {}, _a[prefixCls + "-error"] = error || lengthError, _a[prefixCls + "-disabled"] = disabled, _a[prefixCls + "-rtl"] = rtl, _a), className);
  var TextAreaElement = /* @__PURE__ */ jsx("textarea", {
    ...__assign({}, omit(rest, ["autoSize", "defaultValue"]), {
      maxLength,
      ref: textareaRef,
      style: __assign(__assign({}, style), textAreaStyle),
      className: classNames,
      placeholder,
      disabled,
      value: compositionValue || value || "",
      onChange: valueChangeHandler,
      onKeyDown: keyDownHandler,
      onCompositionStart: compositionHandler,
      onCompositionUpdate: compositionHandler,
      onCompositionEnd: compositionHandler
    })
  });
  if (withWrapper) {
    var showClearIcon = !disabled && allowClear && value;
    var _h = __read(rtl ? [wordLimitMaxLength, valueLength] : [valueLength, wordLimitMaxLength], 2), leftWord = _h[0], rightWord = _h[1];
    return /* @__PURE__ */ jsxs("div", {
      className: cs(prefixCls + "-wrapper", (_b = {}, _b[prefixCls + "-clear-wrapper"] = allowClear, _b[prefixCls + "-wrapper-rtl"] = rtl, _b)),
      style: wrapperStyle,
      children: [TextAreaElement, showClearIcon ? /* @__PURE__ */ jsx(IconHover, {
        className: prefixCls + "-clear-icon",
        children: /* @__PURE__ */ jsx(IconClose, {
          onClick: handleClearClick,
          onMouseDown: function(e) {
            e.preventDefault();
          }
        })
      }) : null, wordLimitMaxLength && showWordLimit && /* @__PURE__ */ jsxs("span", {
        className: cs(prefixCls + "-word-limit", (_c = {}, _c[prefixCls + "-word-limit-error"] = lengthError, _c)),
        children: [leftWord, "/", rightWord]
      })]
    });
  }
  return TextAreaElement;
};
var TextAreaRef = React__default.forwardRef(TextArea);
TextAreaRef.displayName = "TextArea";
const TextArea$1 = TextAreaRef;
export {
  TextArea$1 as default
};
//# sourceMappingURL=textarea.js.map
