'use strict';

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

var vue = require('vue');
require('../../../utils/index.cjs');
require('../../../vue-components/index.cjs');
require('../../../directives/index.cjs');
var calculateNodeHeight = require('./calculateNodeHeight.cjs');
var props = require('./props.cjs');
var raf = require('../../../utils/vue/raf.cjs');
var log = require('../../../utils/log.cjs');
var omit = require('../../../utils/lang/omit.cjs');
var _class = require('../../../utils/dom/class.cjs');
var index = require('../../../vue-components/vc-resize-observer/src/index.cjs');
var index$1 = require('../../../directives/ant-input/index.cjs');

const RESIZE_START = 0;
const RESIZE_MEASURING = 1;
const RESIZE_STABLE = 2;
var ResizableTextArea = /* @__PURE__ */ vue.defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "ResizableTextArea",
  inheritAttrs: false,
  props: props.textAreaProps(),
  setup(props, {
    attrs,
    emit,
    expose
  }) {
    let nextFrameActionId;
    let resizeFrameId;
    const textAreaRef = vue.ref();
    const textareaStyles = vue.ref({});
    const resizeStatus = vue.ref(RESIZE_STABLE);
    vue.onBeforeUnmount(() => {
      raf.raf.cancel(nextFrameActionId);
      raf.raf.cancel(resizeFrameId);
    });
    const fixFirefoxAutoScroll = () => {
      try {
        if (document.activeElement === textAreaRef.value) {
          const currentStart = textAreaRef.value.selectionStart;
          const currentEnd = textAreaRef.value.selectionEnd;
          const scrollTop = textAreaRef.value.scrollTop;
          textAreaRef.value.setSelectionRange(currentStart, currentEnd);
          textAreaRef.value.scrollTop = scrollTop;
        }
      } catch (e) {
      }
    };
    const minRows = vue.ref();
    const maxRows = vue.ref();
    vue.watchEffect(() => {
      const autoSize = props.autoSize || props.autosize;
      if (autoSize) {
        minRows.value = autoSize.minRows;
        maxRows.value = autoSize.maxRows;
      } else {
        minRows.value = void 0;
        maxRows.value = void 0;
      }
    });
    const needAutoSize = vue.computed(() => !!(props.autoSize || props.autosize));
    const startResize = () => {
      resizeStatus.value = RESIZE_START;
    };
    vue.watch([() => props.value, minRows, maxRows, needAutoSize], () => {
      if (needAutoSize.value)
        startResize();
    }, {
      immediate: true,
      flush: "post"
    });
    const autoSizeStyle = vue.ref();
    vue.watch([resizeStatus, textAreaRef], () => {
      if (!textAreaRef.value)
        return;
      if (resizeStatus.value === RESIZE_START) {
        resizeStatus.value = RESIZE_MEASURING;
      } else if (resizeStatus.value === RESIZE_MEASURING) {
        const getTextareaStyles = calculateNodeHeight.default(textAreaRef.value, false, minRows.value, maxRows.value);
        resizeStatus.value = RESIZE_STABLE;
        autoSizeStyle.value = getTextareaStyles;
      } else {
        fixFirefoxAutoScroll();
      }
    }, {
      immediate: true,
      flush: "post"
    });
    const instance = vue.getCurrentInstance();
    const resizeRafRef = vue.ref();
    const cleanRaf = () => {
      raf.raf.cancel(resizeRafRef.value);
    };
    const onInternalResize = (size) => {
      if (resizeStatus.value === RESIZE_STABLE) {
        emit("resize", size);
        if (needAutoSize.value) {
          cleanRaf();
          resizeRafRef.value = raf.raf(() => {
            startResize();
          });
        }
      }
    };
    vue.onBeforeUnmount(() => {
      cleanRaf();
    });
    const resizeTextarea = () => {
      startResize();
    };
    expose({
      resizeTextarea,
      textArea: textAreaRef,
      instance
    });
    log.devWarning(props.autosize === void 0, "Input.TextArea", "autosize is deprecated, please use autoSize instead.");
    const renderTextArea = () => {
      const {
        prefixCls,
        disabled
      } = props;
      const otherProps = omit.omit(props, ["prefixCls", "onPressEnter", "autoSize", "autosize", "defaultValue", "allowClear", "type", "lazy", "maxlength", "valueModifiers"]);
      const cls = _class.classNames(prefixCls, attrs.class, {
        [`${prefixCls}-disabled`]: disabled
      });
      const mergedAutoSizeStyle = needAutoSize.value ? autoSizeStyle.value : null;
      const style = [attrs.style, textareaStyles.value, mergedAutoSizeStyle];
      const textareaProps = {
        ...otherProps,
        ...attrs,
        style,
        class: cls
      };
      if (resizeStatus.value === RESIZE_START || resizeStatus.value === RESIZE_MEASURING) {
        style.push({
          overflowX: "hidden",
          overflowY: "hidden"
        });
      }
      if (!textareaProps.autofocus)
        delete textareaProps.autofocus;
      if (textareaProps.rows === 0)
        delete textareaProps.rows;
      return vue.createVNode(index.default, {
        "onResize": onInternalResize,
        "disabled": !needAutoSize.value
      }, {
        default: () => [vue.withDirectives(vue.createVNode("textarea", {
          ...textareaProps,
          "ref": textAreaRef
        }, null), [[index$1.antInputDirective]])]
      });
    };
    return () => {
      return renderTextArea();
    };
  }
});

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