'use strict';

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

var vue = require('vue');
require('../../../utils/index.cjs');
var FormItemContext = require('../../form/src/FormItemContext.cjs');
var useConfigInject = require('../../config-provider/src/hooks/useConfigInject.cjs');
require('../../config-provider/index.cjs');
var index = require('../style/index.cjs');
var util = require('./util.cjs');
var ClearableLabeledInput = require('./ClearableLabeledInput.cjs');
var ResizableTextArea = require('./ResizableTextArea.cjs');
var props = require('./props.cjs');
var statusUtils = require('../../../utils/statusUtils.cjs');
var disabledContext = require('../../config-provider/src/context/disabledContext.cjs');
var omit = require('../../../utils/lang/omit.cjs');
var _class = require('../../../utils/dom/class.cjs');

function fixEmojiLength(value, maxLength) {
  return [...value || ""].slice(0, maxLength).join("");
}
function setTriggerValue(isCursorInEnd, preValue, triggerValue, maxLength) {
  let newTriggerValue = triggerValue;
  if (isCursorInEnd) {
    newTriggerValue = fixEmojiLength(triggerValue, maxLength);
  } else if ([...preValue || ""].length < triggerValue.length && [...triggerValue || ""].length > maxLength) {
    newTriggerValue = preValue;
  }
  return newTriggerValue;
}
var AInputTextArea = /* @__PURE__ */ vue.defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "ATextarea",
  inheritAttrs: false,
  props: props.textAreaProps(),
  setup(props, {
    attrs,
    expose,
    emit
  }) {
    const formItemContext = FormItemContext.useInjectFormItemContext();
    const formItemInputContext = FormItemContext.FormItemInputContext.useInject();
    const mergedStatus = vue.computed(() => statusUtils.getMergedStatus(formItemInputContext.status, props.status));
    const stateValue = vue.shallowRef(props.value === void 0 ? props.defaultValue : props.value);
    const resizableTextArea = vue.shallowRef();
    const mergedValue = vue.shallowRef("");
    const {
      prefixCls,
      size,
      direction
    } = useConfigInject.default("input", props);
    const [wrapSSR, hashId] = index.default(prefixCls);
    const disabled = disabledContext.useInjectDisabled();
    const showCount = vue.computed(() => {
      return props.showCount === "" || props.showCount || false;
    });
    const hasMaxLength = vue.computed(() => Number(props.maxlength) > 0);
    const compositing = vue.shallowRef(false);
    const oldCompositionValueRef = vue.shallowRef();
    const oldSelectionStartRef = vue.shallowRef(0);
    const onInternalCompositionStart = (e) => {
      compositing.value = true;
      oldCompositionValueRef.value = mergedValue.value;
      oldSelectionStartRef.value = e.currentTarget.selectionStart;
      emit("compositionstart", e);
    };
    const setValue = (value, callback) => {
      if (stateValue.value === value)
        return;
      if (props.value === void 0) {
        stateValue.value = value;
      } else {
        vue.nextTick(() => {
          var _a, _b, _c;
          if (resizableTextArea.value.textArea.value !== mergedValue.value)
            (_c = (_a = resizableTextArea.value) == null ? void 0 : (_b = _a.instance).update) == null ? void 0 : _c.call(_b);
        });
      }
      vue.nextTick(() => {
        callback && callback();
      });
    };
    const triggerChange = (e) => {
      emit("update:value", e.target.value);
      emit("change", e);
      emit("input", e);
      formItemContext.onFieldChange();
    };
    const onInternalCompositionEnd = (e) => {
      var _a;
      compositing.value = false;
      let triggerValue = e.currentTarget.value;
      if (hasMaxLength.value) {
        const isCursorInEnd = oldSelectionStartRef.value >= props.maxlength + 1 || oldSelectionStartRef.value === ((_a = oldCompositionValueRef.value) == null ? void 0 : _a.length);
        triggerValue = setTriggerValue(isCursorInEnd, oldCompositionValueRef.value, triggerValue, props.maxlength);
      }
      if (triggerValue !== mergedValue.value) {
        setValue(triggerValue);
        util.resolveOnChange(e.currentTarget, e, triggerChange, triggerValue);
      }
      emit("compositionend", e);
    };
    const instance = vue.getCurrentInstance();
    vue.watch(() => props.value, () => {
      var _a;
      if ("value" in instance.vnode.props || {})
        stateValue.value = (_a = props.value) != null ? _a : "";
    });
    const focus = (option) => {
      var _a;
      util.triggerFocus((_a = resizableTextArea.value) == null ? void 0 : _a.textArea, option);
    };
    const blur = () => {
      var _a, _b;
      (_b = (_a = resizableTextArea.value) == null ? void 0 : _a.textArea) == null ? void 0 : _b.blur();
    };
    const handleKeyDown = (e) => {
      if (e.keyCode === 13)
        emit("pressEnter", e);
      emit("keydown", e);
    };
    const onBlur = (e) => {
      var _a;
      (_a = props == null ? void 0 : props.onBlur) == null ? void 0 : _a.call(props, e);
      formItemContext.onFieldBlur();
    };
    const handleReset = (e) => {
      util.resolveOnChange(resizableTextArea.value.textArea, e, triggerChange);
      setValue("", () => {
        focus();
      });
    };
    const handleChange = (e) => {
      const {
        composing
      } = e.target;
      let triggerValue = e.target.value;
      compositing.value = !!(e.isComposing || composing);
      if (compositing.value && props.lazy || stateValue.value === triggerValue)
        return;
      if (hasMaxLength.value) {
        const target = e.target;
        const isCursorInEnd = target.selectionStart >= props.maxlength + 1 || target.selectionStart === triggerValue.length || !target.selectionStart;
        triggerValue = setTriggerValue(isCursorInEnd, mergedValue.value, triggerValue, props.maxlength);
      }
      util.resolveOnChange(e.currentTarget, e, triggerChange, triggerValue);
      setValue(triggerValue);
    };
    const renderTextArea = () => {
      var _a, _b;
      const {
        class: customClass
      } = attrs;
      const {
        bordered = true
      } = props;
      const resizeProps = {
        ...omit.omit(props, ["allowClear"]),
        ...attrs,
        class: [{
          [`${prefixCls.value}-borderless`]: !bordered,
          [`${customClass}`]: customClass && !showCount.value,
          [`${prefixCls.value}-sm`]: size.value === "small",
          [`${prefixCls.value}-lg`]: size.value === "large"
        }, statusUtils.getStatusClassNames(prefixCls.value, mergedStatus.value), hashId.value],
        disabled: disabled.value,
        showCount: null,
        prefixCls: prefixCls.value,
        onInput: handleChange,
        onChange: handleChange,
        onBlur,
        onKeydown: handleKeyDown,
        onCompositionstart: onInternalCompositionStart,
        onCompositionend: onInternalCompositionEnd
      };
      if ((_a = props.valueModifiers) == null ? void 0 : _a.lazy)
        delete resizeProps.onInput;
      return vue.createVNode(ResizableTextArea.default, {
        ...resizeProps,
        "id": (_b = resizeProps == null ? void 0 : resizeProps.id) != null ? _b : formItemContext.id.value,
        "ref": resizableTextArea,
        "maxlength": props.maxlength
      }, null);
    };
    expose({
      focus,
      blur,
      resizableTextArea
    });
    vue.watchEffect(() => {
      let val = util.fixControlledValue(stateValue.value);
      if (!compositing.value && hasMaxLength.value && (props.value === null || props.value === void 0)) {
        val = fixEmojiLength(val, props.maxlength);
      }
      mergedValue.value = val;
    });
    return () => {
      var _a;
      const {
        maxlength,
        bordered = true,
        hidden
      } = props;
      const {
        style,
        class: customClass
      } = attrs;
      const inputProps = {
        ...props,
        ...attrs,
        prefixCls: prefixCls.value,
        inputType: "text",
        handleReset,
        direction: direction.value,
        bordered,
        style: showCount.value ? void 0 : style,
        hashId: hashId.value,
        disabled: (_a = props.disabled) != null ? _a : disabled.value
      };
      let textareaNode = vue.createVNode(ClearableLabeledInput.default, {
        ...inputProps,
        "value": mergedValue.value,
        "status": props.status
      }, {
        element: renderTextArea
      });
      if (showCount.value || formItemInputContext.hasFeedback) {
        const valueLength = [...mergedValue.value].length;
        let dataCount = "";
        if (typeof showCount.value === "object") {
          dataCount = showCount.value.formatter({
            value: mergedValue.value,
            count: valueLength,
            maxlength
          });
        } else {
          dataCount = `${valueLength}${hasMaxLength.value ? ` / ${maxlength}` : ""}`;
        }
        textareaNode = vue.createVNode("div", {
          "hidden": hidden,
          "class": _class.classNames(`${prefixCls.value}-textarea`, {
            [`${prefixCls.value}-textarea-rtl`]: direction.value === "rtl",
            [`${prefixCls.value}-textarea-show-count`]: showCount.value,
            [`${prefixCls.value}-textarea-in-form-item`]: formItemInputContext.isFormItemInput
          }, `${prefixCls.value}-textarea-show-count`, customClass, hashId.value),
          "style": style,
          "data-count": typeof dataCount !== "object" ? dataCount : void 0
        }, [textareaNode, formItemInputContext.hasFeedback && vue.createVNode("span", {
          "class": `${prefixCls.value}-textarea-suffix`
        }, [formItemInputContext.feedbackIcon])]);
      }
      return wrapSSR(textareaNode);
    };
  }
});

exports.default = AInputTextArea;
//# sourceMappingURL=TextArea.cjs.map
