/**
 * ldesign v0.2.2
 * (c) 2022年9月20日 swimly
 * @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 { c as calculateNodeSize, l as limitUnicodeMaxLength, a as getUnicodeLength } from '../../_chunks/dep-fc383775.js';
import props from './props.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 '@babel/runtime/helpers/toConsumableArray';
import '@babel/runtime/helpers/slicedToArray';
import '@babel/runtime/helpers/objectWithoutProperties';
import 'lodash/camelCase';
import 'lodash/kebabCase';
import 'lodash/mergeWith';
import 'lodash';
import '../config-provider/context.js';
import '../../_chunks/dep-a0f2090c.js';

/**
 * LDesignUtility v0.1.1
 * (c) 2022 undefined
 * @license MIT
 */
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 _calculateNodeSize = calculateNodeSize(targetElement),
      paddingSize = _calculateNodeSize.paddingSize,
      borderSize = _calculateNodeSize.borderSize,
      boxSizing = _calculateNodeSize.boxSizing,
      sizingStyle = _calculateNodeSize.sizingStyle;

  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 = function 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;
}

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: "LTextarea",
  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 = renderLNodeJSX(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
