/**
 * ldesign v0.2.14
 * (c) 2022/11/2 16:16 built by swimly
 * @license MIT
 */

import Vue from 'vue';
import isFunction from 'lodash/isFunction';
import { c as calculateNodeSize, l as limitUnicodeMaxLength, a as getUnicodeLength } from '../../chunks/helper2.js';
import '../../chunks/defineProperty-838e4192.js';
import { getCharacterLength, getPropsApiByEvent } from '../../utils/helper.js';
import { renderLNodeJSX } from '../../utils/render-node.js';
import { getClassPrefixMixins } from '../config-provider/config-receiver.js';
import mixins from '../../utils/mixins.js';
import props from './props.js';
import 'lodash/camelCase';
import 'lodash';
import 'lodash/mergeWith';
import '../config-provider/context.js';
import '../../chunks/zh_CN.js';
import '../config-provider/type.js';

var TEXTAREA_STYLE = "\n  min-height:0 !important;\n  max-height:none !important;\n  height:0 !important;\n  visibility:hidden !important;\n  overflow:hidden !important;\n  position:absolute !important;\n  z-index:-1000 !important;\n  top:0 !important;\n  right:0 !important\n";
var hiddenTextarea;
function calcTextareaHeight(targetElement) {
  var _hiddenTextarea, _hiddenTextarea$paren;
  var minRows = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : 1;
  var maxRows = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : null;
  if (!hiddenTextarea) {
    hiddenTextarea = document.createElement("textarea");
    document.body.appendChild(hiddenTextarea);
  }
  var {
    paddingSize,
    borderSize,
    boxSizing,
    sizingStyle
  } = calculateNodeSize(targetElement);
  hiddenTextarea.setAttribute("style", "".concat(sizingStyle, ";").concat(TEXTAREA_STYLE));
  hiddenTextarea.value = targetElement.value || targetElement.placeholder || "";
  var height = hiddenTextarea.scrollHeight;
  var result = {};
  var isBorderbox = boxSizing === "border-box";
  var isContentbox = boxSizing === "content-box";
  if (isBorderbox) height += borderSize;else if (isContentbox) height -= paddingSize;
  hiddenTextarea.value = "";
  var singleRowHeight = hiddenTextarea.scrollHeight - paddingSize;
  (_hiddenTextarea = hiddenTextarea) === null || _hiddenTextarea === void 0 ? void 0 : (_hiddenTextarea$paren = _hiddenTextarea.parentNode) === null || _hiddenTextarea$paren === void 0 ? void 0 : _hiddenTextarea$paren.removeChild(hiddenTextarea);
  hiddenTextarea = null;
  var calcHeight = rows => {
    var rowsHeight = singleRowHeight * rows;
    if (isBorderbox) rowsHeight = rowsHeight + paddingSize + borderSize;
    return rowsHeight;
  };
  if (minRows !== null) {
    var minHeight = calcHeight(minRows);
    height = Math.max(minHeight, height);
    result.minHeight = "".concat(minHeight, "px");
  }
  if (maxRows !== null) height = Math.min(calcHeight(maxRows), height);
  result.height = "".concat(height, "px");
  return result;
}

const classPrefixMixins = getClassPrefixMixins("textarea");
function getValidAttrs(obj) {
  const newObj = {};
  Object.keys(obj).forEach(key => {
    if (typeof obj[key] !== "undefined") newObj[key] = obj[key];
  });
  return newObj;
}
var _Textarea = mixins(Vue, classPrefixMixins).extend({
  name: "LTextarea",
  props: {
    ...props
  },
  inject: {
    tFormItem: {
      default: void 0
    }
  },
  data() {
    return {
      formDisabled: void 0,
      focused: false,
      mouseHover: false,
      textareaStyle: {}
    };
  },
  computed: {
    tDisabled() {
      return this.formDisabled || this.disabled;
    },
    textareaClasses() {
      return [this.componentName, {
        [`${this.classPrefix}-is-disabled`]: this.tDisabled,
        [`${this.classPrefix}-is-readonly`]: this.readonly
      }];
    },
    limitClasses() {
      return [`${this.componentName}__limit`, {
        [`${this.classPrefix}-is-disabled`]: this.tDisabled
      }];
    },
    inputAttrs() {
      return getValidAttrs({
        autofocus: this.autofocus,
        disabled: this.tDisabled,
        readonly: this.readonly,
        placeholder: this.placeholder,
        name: this.name || void 0,
        unselectable: this.readonly ? "on" : "off"
      });
    },
    characterNumber() {
      const characterInfo = getCharacterLength(String(this.value || ""));
      if (typeof characterInfo === "object") return characterInfo.length;
      return characterInfo;
    }
  },
  mounted() {
    this.adjustTextareaHeight();
  },
  methods: {
    adjustTextareaHeight() {
      if (this.autosize === true) {
        this.textareaStyle = calcTextareaHeight(this.$refs.refTextareaElem);
      } else if (typeof this.autosize === "object") {
        var _this$autosize, _this$autosize2;
        this.textareaStyle = calcTextareaHeight(this.$refs.refTextareaElem, (_this$autosize = this.autosize) === null || _this$autosize === void 0 ? void 0 : _this$autosize.minRows, (_this$autosize2 = this.autosize) === null || _this$autosize2 === void 0 ? void 0 : _this$autosize2.maxRows);
      } else if (this.$attrs.rows) {
        this.textareaStyle = {
          height: "auto",
          minHeight: "auto"
        };
      }
    },
    emitEvent(name, value, context) {
      this.$emit(name, value, context);
      const handleName = getPropsApiByEvent(name);
      isFunction(this[handleName]) && this[handleName](value, context);
    },
    focus() {
      const input = this.$refs.refTextareaElem;
      input === null || input === void 0 ? void 0 : input.focus();
    },
    blur() {
      const input = this.$refs.refTextareaElem;
      input === null || input === void 0 ? void 0 : input.blur();
    },
    handleInput(e) {
      if (e.isComposing || e.inputType === "insertCompositionText") return;
      this.inputValueChangeHandle(e);
    },
    onCompositionend(e) {
      this.inputValueChangeHandle(e);
    },
    inputValueChangeHandle(e) {
      const {
        target
      } = e;
      let val = target.value;
      val = limitUnicodeMaxLength(val, this.maxlength);
      if (this.maxcharacter && this.maxcharacter >= 0) {
        const stringInfo = getCharacterLength(val, this.maxcharacter);
        val = typeof stringInfo === "object" && stringInfo.characters;
      }
      this.$emit("input", val);
      this.emitEvent("change", val, {
        e
      });
      this.$nextTick(() => this.setInputValue(val));
      this.adjustTextareaHeight();
    },
    setInputValue() {
      let v = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "";
      const textareaElem = this.$refs.refTextareaElem;
      const sV = String(v);
      if (!textareaElem) return;
      if (textareaElem.value !== sV) textareaElem.value = sV;
    },
    emitKeyDown(e) {
      if (this.tDisabled) return;
      this.emitEvent("keydown", this.value, {
        e
      });
    },
    emitKeyUp(e) {
      if (this.tDisabled) return;
      this.emitEvent("keyup", this.value, {
        e
      });
    },
    emitKeypress(e) {
      if (this.tDisabled) return;
      this.emitEvent("keypress", this.value, {
        e
      });
    },
    emitFocus(e) {
      if (this.tDisabled) return;
      this.focused = true;
      this.emitEvent("focus", this.value, {
        e
      });
    },
    emitBlur(e) {
      var _this$tFormItem;
      this.focused = false;
      (_this$tFormItem = this.tFormItem) === null || _this$tFormItem === void 0 ? void 0 : _this$tFormItem.validate("blur");
      this.emitEvent("blur", this.value, {
        e
      });
    }
  },
  render() {
    const h = arguments[0];
    const inputEvents = getValidAttrs({
      focus: this.emitFocus,
      blur: this.emitBlur,
      keydown: this.emitKeyDown,
      keyup: this.emitKeyUp,
      keypress: this.emitKeypress
    });
    const classes = [`${this.componentName}__inner`, {
      [`${this.classPrefix}-is-${this.status}`]: this.status,
      [this.commonStatusClassName.disabled]: this.tDisabled,
      [this.commonStatusClassName.focused]: this.focused,
      [`${this.classPrefix}-resize-none`]: typeof this.autosize === "object"
    }];
    const tips = renderLNodeJSX(this, "tips");
    return h("div", {
      "class": this.textareaClasses
    }, [h("textarea", {
      "on": {
        "input": this.handleInput,
        "compositionend": this.onCompositionend,
        ...inputEvents
      },
      "attrs": {
        ...{
          ...this.$attrs,
          ...this.inputAttrs
        }
      },
      "domProps": {
        "value": this.value
      },
      "class": classes,
      "style": this.textareaStyle,
      "ref": "refTextareaElem"
    }), this.maxcharacter ? h("span", {
      "class": this.limitClasses
    }, [`${this.characterNumber}/${this.maxcharacter}`]) : null, !this.maxcharacter && this.maxlength ? h("span", {
      "class": this.limitClasses
    }, [`${this.value ? getUnicodeLength(String(this.value)) : 0}/${this.maxlength}`]) : null, tips && h("div", {
      "class": [`${this.componentName}__tips`, `${this.componentName}__tips--${this.status || "normal"}`]
    }, [tips])]);
  }
});

export { _Textarea as default };
//# sourceMappingURL=textarea.js.map
