/**
 * tdesign v0.46.4
 * (c) 2022 tdesign
 * @license MIT
 */

import _typeof from '@babel/runtime/helpers/typeof';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import Vue from 'vue';
import isFunction from 'lodash/isFunction';
import { limitUnicodeMaxLength, getUnicodeLength } from '../_common/js/utils/helper.js';
import props from './props.js';
import { getCharacterLength, getPropsApiByEvent } from '../utils/helper.js';
import calcTextareaHeight from './calcTextareaHeight.js';
import { renderTNodeJSX } from '../utils/render-tnode.js';
import { getClassPrefixMixins } from '../config-provider/config-receiver.js';
import mixins from '../utils/mixins.js';
import '@babel/runtime/helpers/toConsumableArray';
import '@babel/runtime/helpers/objectWithoutProperties';
import '@babel/runtime/helpers/slicedToArray';
import 'lodash/camelCase';
import 'lodash/kebabCase';
import 'lodash/mergeWith';
import '../config-provider/context.js';
import 'lodash/merge';
import '../_common/js/global-config/default-config.js';
import '../_common/js/global-config/locale/zh_CN.js';
import '../config-provider/type.js';

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
var classPrefixMixins = getClassPrefixMixins("textarea");

function getValidAttrs(obj) {
  var newObj = {};
  Object.keys(obj).forEach(function (key) {
    if (typeof obj[key] !== "undefined") {
      newObj[key] = obj[key];
    }
  });
  return newObj;
}

var _Textarea = mixins(Vue, classPrefixMixins).extend({
  name: "TTextarea",
  props: _objectSpread({}, props),
  inject: {
    tFormItem: {
      "default": void 0
    }
  },
  data: function data() {
    return {
      formDisabled: void 0,
      focused: false,
      mouseHover: false,
      textareaStyle: {}
    };
  },
  computed: {
    tDisabled: function tDisabled() {
      return this.formDisabled || this.disabled;
    },
    textareaClasses: function textareaClasses() {
      var _ref;

      return [this.componentName, (_ref = {}, _defineProperty(_ref, "".concat(this.classPrefix, "-is-disabled"), this.tDisabled), _defineProperty(_ref, "".concat(this.classPrefix, "-is-readonly"), this.readonly), _ref)];
    },
    inputAttrs: function 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: function characterNumber() {
      var characterInfo = getCharacterLength(String(this.value || ""));

      if (_typeof(characterInfo) === "object") {
        return characterInfo.length;
      }

      return characterInfo;
    }
  },
  mounted: function mounted() {
    this.adjustTextareaHeight();
  },
  methods: {
    adjustTextareaHeight: function 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: function emitEvent(name, value, context) {
      this.$emit(name, value, context);
      var handleName = getPropsApiByEvent(name);
      isFunction(this[handleName]) && this[handleName](value, context);
    },
    focus: function focus() {
      var input = this.$refs.refTextareaElem;
      input === null || input === void 0 ? void 0 : input.focus();
    },
    blur: function blur() {
      var input = this.$refs.refTextareaElem;
      input === null || input === void 0 ? void 0 : input.blur();
    },
    handleInput: function handleInput(e) {
      if (e.isComposing || e.inputType === "insertCompositionText") return;
      this.inputValueChangeHandle(e);
    },
    onCompositionend: function onCompositionend(e) {
      this.inputValueChangeHandle(e);
    },
    inputValueChangeHandle: function inputValueChangeHandle(e) {
      var _this = this;

      var target = e.target;
      var val = target.value;
      val = limitUnicodeMaxLength(val, this.maxlength);

      if (this.maxcharacter && this.maxcharacter >= 0) {
        var stringInfo = getCharacterLength(val, this.maxcharacter);
        val = _typeof(stringInfo) === "object" && stringInfo.characters;
      }

      this.$emit("input", val);
      this.emitEvent("change", val, {
        e: e
      });
      this.$nextTick(function () {
        return _this.setInputValue(val);
      });
      this.adjustTextareaHeight();
    },
    setInputValue: function setInputValue() {
      var v = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "";
      var textareaElem = this.$refs.refTextareaElem;
      var sV = String(v);

      if (!textareaElem) {
        return;
      }

      if (textareaElem.value !== sV) {
        textareaElem.value = sV;
      }
    },
    emitKeyDown: function emitKeyDown(e) {
      if (this.tDisabled) return;
      this.emitEvent("keydown", this.value, {
        e: e
      });
    },
    emitKeyUp: function emitKeyUp(e) {
      if (this.tDisabled) return;
      this.emitEvent("keyup", this.value, {
        e: e
      });
    },
    emitKeypress: function emitKeypress(e) {
      if (this.tDisabled) return;
      this.emitEvent("keypress", this.value, {
        e: e
      });
    },
    emitFocus: function emitFocus(e) {
      if (this.tDisabled) return;
      this.focused = true;
      this.emitEvent("focus", this.value, {
        e: e
      });
    },
    emitBlur: function 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: e
      });
    }
  },
  render: function render() {
    var _ref2;

    var h = arguments[0];
    var inputEvents = getValidAttrs({
      focus: this.emitFocus,
      blur: this.emitBlur,
      keydown: this.emitKeyDown,
      keyup: this.emitKeyUp,
      keypress: this.emitKeypress
    });
    var classes = ["".concat(this.componentName, "__inner"), (_ref2 = {}, _defineProperty(_ref2, "".concat(this.classPrefix, "-is-").concat(this.status), this.status), _defineProperty(_ref2, this.commonStatusClassName.disabled, this.tDisabled), _defineProperty(_ref2, this.commonStatusClassName.focused, this.focused), _defineProperty(_ref2, "".concat(this.classPrefix, "-resize-none"), _typeof(this.autosize) === "object"), _ref2)];
    var tips = renderTNodeJSX(this, "tips");
    return h("div", {
      "class": this.textareaClasses
    }, [h("textarea", {
      "on": _objectSpread({
        "input": this.handleInput,
        "compositionend": this.onCompositionend
      }, inputEvents),
      "attrs": _objectSpread({}, _objectSpread(_objectSpread({}, this.$attrs), this.inputAttrs)),
      "domProps": {
        "value": this.value
      },
      "class": classes,
      "style": this.textareaStyle,
      "ref": "refTextareaElem"
    }), this.maxcharacter ? h("span", {
      "class": "".concat(this.componentName, "__limit")
    }, ["".concat(this.characterNumber, "/").concat(this.maxcharacter)]) : null, !this.maxcharacter && this.maxlength ? h("span", {
      "class": "".concat(this.componentName, "__limit")
    }, ["".concat(this.value ? getUnicodeLength(String(this.value)) : 0, "/").concat(this.maxlength)]) : null, tips && h("div", {
      "class": ["".concat(this.componentName, "__tips"), "".concat(this.componentName, "__tips--").concat(this.status || "normal")]
    }, [tips])]);
  }
});

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