import { isVNode, defineComponent, ref, onBeforeUnmount, createVNode, withDirectives, mergeProps, watch, nextTick, onMounted, getCurrentInstance } from 'vue';
import ResizeObserver from '../vc-resize-observer/index.mjs';
import classNames from '../_util/classNames.mjs';
import wrapperRaf from '../_util/raf.mjs';
import warning from '../_util/warning.mjs';
import antInput from '../_util/antInputDirective.mjs';
import omit from '../_util/omit.mjs';
import { textAreaProps } from './inputProps.mjs';
import calculateNodeHeight from './calculateNodeHeight.mjs';

function _isSlot(s) {
  return typeof s === "function" || Object.prototype.toString.call(s) === "[object Object]" && !isVNode(s);
}
const RESIZE_STATUS_NONE = 0;
const RESIZE_STATUS_RESIZING = 1;
const RESIZE_STATUS_RESIZED = 2;
const ResizableTextArea = /* @__PURE__ */ defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "ResizableTextArea",
  inheritAttrs: false,
  props: textAreaProps(),
  setup(props, {
    attrs,
    emit,
    expose
  }) {
    let nextFrameActionId;
    let resizeFrameId;
    const textAreaRef = ref();
    const textareaStyles = ref({});
    const resizeStatus = ref(RESIZE_STATUS_NONE);
    onBeforeUnmount(() => {
      wrapperRaf.cancel(nextFrameActionId);
      wrapperRaf.cancel(resizeFrameId);
    });
    const fixFirefoxAutoScroll = () => {
      try {
        if (document.activeElement === textAreaRef.value) {
          const currentStart = textAreaRef.value.selectionStart;
          const currentEnd = textAreaRef.value.selectionEnd;
          textAreaRef.value.setSelectionRange(currentStart, currentEnd);
        }
      } catch (e) {
      }
    };
    const resizeTextarea = () => {
      const autoSize = props.autoSize || props.autosize;
      if (!autoSize || !textAreaRef.value)
        return;
      const {
        minRows,
        maxRows
      } = autoSize;
      textareaStyles.value = calculateNodeHeight(textAreaRef.value, false, minRows, maxRows);
      resizeStatus.value = RESIZE_STATUS_RESIZING;
      wrapperRaf.cancel(resizeFrameId);
      resizeFrameId = wrapperRaf(() => {
        resizeStatus.value = RESIZE_STATUS_RESIZED;
        resizeFrameId = wrapperRaf(() => {
          resizeStatus.value = RESIZE_STATUS_NONE;
          fixFirefoxAutoScroll();
        });
      });
    };
    const resizeOnNextFrame = () => {
      wrapperRaf.cancel(nextFrameActionId);
      nextFrameActionId = wrapperRaf(resizeTextarea);
    };
    const handleResize = (size) => {
      if (resizeStatus.value !== RESIZE_STATUS_NONE)
        return;
      emit("resize", size);
      const autoSize = props.autoSize || props.autosize;
      if (autoSize)
        resizeOnNextFrame();
    };
    warning(props.autosize === void 0, "Input.TextArea", "autosize is deprecated, please use autoSize instead.");
    const renderTextArea = () => {
      let _slot;
      const {
        prefixCls,
        autoSize,
        autosize,
        disabled
      } = props;
      const otherProps = omit(props, ["prefixCls", "onPressEnter", "autoSize", "autosize", "defaultValue", "allowClear", "type", "lazy", "maxlength", "valueModifiers"]);
      const cls = classNames(prefixCls, attrs.class, {
        [`${prefixCls}-disabled`]: disabled
      });
      const style = [attrs.style, textareaStyles.value, resizeStatus.value === RESIZE_STATUS_RESIZING ? {
        overflowX: "hidden",
        overflowY: "hidden"
      } : null];
      const textareaProps = {
        ...otherProps,
        ...attrs,
        style,
        class: cls
      };
      if (!textareaProps.autofocus)
        delete textareaProps.autofocus;
      if (textareaProps.rows === 0)
        delete textareaProps.rows;
      return createVNode(ResizeObserver, {
        "onResize": handleResize,
        "disabled": !(autoSize || autosize)
      }, _isSlot(_slot = withDirectives(createVNode("textarea", mergeProps(textareaProps, {
        "ref": textAreaRef
      }), null), [[antInput]])) ? _slot : {
        default: () => [_slot]
      });
    };
    watch(() => props.value, () => {
      nextTick(() => {
        resizeTextarea();
      });
    });
    onMounted(() => {
      nextTick(() => {
        resizeTextarea();
      });
    });
    const instance = getCurrentInstance();
    expose({
      resizeTextarea,
      textArea: textAreaRef,
      instance
    });
    return () => {
      return renderTextArea();
    };
  }
});

export { ResizableTextArea as default };
//# sourceMappingURL=ResizableTextArea.mjs.map
