'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var vue = require('vue');
var index = require('../vc-resize-observer/index.js');
var classNames = require('../_util/classNames.js');
var raf = require('../_util/raf.js');
var warning = require('../_util/warning.js');
var antInputDirective = require('../_util/antInputDirective.js');
var omit = require('../_util/omit.js');
var inputProps = require('./inputProps.js');
var calculateNodeHeight = require('./calculateNodeHeight.js');

function _isSlot(s) {
  return typeof s === "function" || Object.prototype.toString.call(s) === "[object Object]" && !vue.isVNode(s);
}
const RESIZE_STATUS_NONE = 0;
const RESIZE_STATUS_RESIZING = 1;
const RESIZE_STATUS_RESIZED = 2;
const ResizableTextArea = /* @__PURE__ */ vue.defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "ResizableTextArea",
  inheritAttrs: false,
  props: inputProps.textAreaProps(),
  setup(props, {
    attrs,
    emit,
    expose
  }) {
    let nextFrameActionId;
    let resizeFrameId;
    const textAreaRef = vue.ref();
    const textareaStyles = vue.ref({});
    const resizeStatus = vue.ref(RESIZE_STATUS_NONE);
    vue.onBeforeUnmount(() => {
      raf.default.cancel(nextFrameActionId);
      raf.default.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.default(textAreaRef.value, false, minRows, maxRows);
      resizeStatus.value = RESIZE_STATUS_RESIZING;
      raf.default.cancel(resizeFrameId);
      resizeFrameId = raf.default(() => {
        resizeStatus.value = RESIZE_STATUS_RESIZED;
        resizeFrameId = raf.default(() => {
          resizeStatus.value = RESIZE_STATUS_NONE;
          fixFirefoxAutoScroll();
        });
      });
    };
    const resizeOnNextFrame = () => {
      raf.default.cancel(nextFrameActionId);
      nextFrameActionId = raf.default(resizeTextarea);
    };
    const handleResize = (size) => {
      if (resizeStatus.value !== RESIZE_STATUS_NONE)
        return;
      emit("resize", size);
      const autoSize = props.autoSize || props.autosize;
      if (autoSize)
        resizeOnNextFrame();
    };
    warning.default(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.default(props, ["prefixCls", "onPressEnter", "autoSize", "autosize", "defaultValue", "allowClear", "type", "lazy", "maxlength", "valueModifiers"]);
      const cls = classNames.default(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 vue.createVNode(index.default, {
        "onResize": handleResize,
        "disabled": !(autoSize || autosize)
      }, _isSlot(_slot = vue.withDirectives(vue.createVNode("textarea", vue.mergeProps(textareaProps, {
        "ref": textAreaRef
      }), null), [[antInputDirective.default]])) ? _slot : {
        default: () => [_slot]
      });
    };
    vue.watch(() => props.value, () => {
      vue.nextTick(() => {
        resizeTextarea();
      });
    });
    vue.onMounted(() => {
      vue.nextTick(() => {
        resizeTextarea();
      });
    });
    const instance = vue.getCurrentInstance();
    expose({
      resizeTextarea,
      textArea: textAreaRef,
      instance
    });
    return () => {
      return renderTextArea();
    };
  }
});

exports.default = ResizableTextArea;
//# sourceMappingURL=ResizableTextArea.js.map
