/**
 * ldesign v0.4.2
 * (c) 2023 ldesign
 * @license MIT
 */

'use strict';

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

var _typeof = require('@babel/runtime/helpers/typeof');
var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var vue = require('vue');
var merge = require('lodash/merge');
var isUndefined = require('lodash/isUndefined');
var utils_helper = require('../utils/helper.js');
var form_const = require('../form/const.js');
var hooks_useVModel = require('../hooks/useVModel.js');
var form_hooks = require('../form/hooks.js');
var hooks_tnode = require('../hooks/tnode.js');
var hooks_useConfig = require('../hooks/useConfig.js');
var _common_js_utils_setStyle = require('../_common/js/utils/set-style.js');
var textarea_calcTextareaHeight = require('./calcTextareaHeight.js');
var textarea_props = require('./props.js');
require('@babel/runtime/helpers/objectWithoutProperties');
require('lodash/camelCase');
require('lodash/isNull');
require('lodash/isArray');
require('lodash/isNumber');
require('lodash/isString');
require('lodash/kebabCase');
require('lodash/isFunction');
require('../utils/render-tnode.js');
require('lodash/isEmpty');
require('lodash/isObject');
require('../config-provider/useConfig.js');
require('lodash/cloneDeep');
require('../config-provider/context.js');
require('lodash/mergeWith');
require('../_common/js/global-config/default-config.js');
require('../_common/js/global-config/locale/zh_CN.js');
require('../_chunks/dep-7f0559c3.js');
require('dayjs');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _typeof__default = /*#__PURE__*/_interopDefaultLegacy(_typeof);
var _slicedToArray__default = /*#__PURE__*/_interopDefaultLegacy(_slicedToArray);
var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
var merge__default = /*#__PURE__*/_interopDefaultLegacy(merge);
var isUndefined__default = /*#__PURE__*/_interopDefaultLegacy(isUndefined);

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__default["default"](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; }
function getValidAttrs(obj) {
  var newObj = {};
  Object.keys(obj).forEach(function (key) {
    if (!isUndefined__default["default"](obj[key])) newObj[key] = obj[key];
  });
  return newObj;
}
var _Textarea = vue.defineComponent({
  name: "TTextarea",
  inheritAttrs: false,
  props: _objectSpread({}, textarea_props["default"]),
  setup: function setup(props2, _ref) {
    var attrs = _ref.attrs,
      expose = _ref.expose;
    var prefix = hooks_useConfig.usePrefixClass();
    var name = hooks_useConfig.usePrefixClass("textarea");
    var TEXTAREA_TIPS_CLASS = vue.computed(function () {
      return "".concat(name.value, "__tips");
    });
    var TEXTAREA_LIMIT = vue.computed(function () {
      return "".concat(name.value, "__limit");
    });
    var _toRefs = vue.toRefs(props2),
      value = _toRefs.value,
      modelValue = _toRefs.modelValue;
    var _useVModel = hooks_useVModel["default"](value, modelValue, props2.defaultValue, props2.onChange),
      _useVModel2 = _slicedToArray__default["default"](_useVModel, 2),
      innerValue = _useVModel2[0],
      setInnerValue = _useVModel2[1];
    var disabled = form_hooks.useFormDisabled();
    var textareaStyle = vue.ref({});
    var refTextareaElem = vue.ref();
    var focused = vue.ref(false);
    var isComposing = vue.ref(false);
    var focus = function focus() {
      var _refTextareaElem$valu;
      return (_refTextareaElem$valu = refTextareaElem.value) === null || _refTextareaElem$valu === void 0 ? void 0 : _refTextareaElem$valu.focus();
    };
    var blur = function blur() {
      var _refTextareaElem$valu2;
      return (_refTextareaElem$valu2 = refTextareaElem.value) === null || _refTextareaElem$valu2 === void 0 ? void 0 : _refTextareaElem$valu2.blur();
    };
    var adjustTextareaHeight = function adjustTextareaHeight() {
      var _refTextareaElem$valu3, _refTextareaElem$valu4;
      if (props2.autosize === true) {
        textareaStyle.value = textarea_calcTextareaHeight["default"](refTextareaElem.value);
      } else if (_typeof__default["default"](props2.autosize) === "object") {
        var _props2$autosize = props2.autosize,
          minRows = _props2$autosize.minRows,
          maxRows = _props2$autosize.maxRows;
        textareaStyle.value = textarea_calcTextareaHeight["default"](refTextareaElem.value, minRows, maxRows);
      } else if (attrs.rows) {
        textareaStyle.value = {
          height: "auto",
          minHeight: "auto"
        };
      } else if (attrs.style && (_refTextareaElem$valu3 = refTextareaElem.value) !== null && _refTextareaElem$valu3 !== void 0 && (_refTextareaElem$valu4 = _refTextareaElem$valu3.style) !== null && _refTextareaElem$valu4 !== void 0 && _refTextareaElem$valu4.height) {
        textareaStyle.value = {
          height: refTextareaElem.value.style.height
        };
      }
    };
    var setInputValue = function setInputValue() {
      var v = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "";
      var textareaElem = refTextareaElem.value;
      var sV = String(v);
      if (!textareaElem) return;
      if (textareaElem.value !== sV) {
        textareaElem.value = sV;
        innerValue.value = sV;
      }
    };
    var inputValueChangeHandle = function inputValueChangeHandle(e) {
      var target = e.target;
      var val = target.value;
      if (props2.maxcharacter && props2.maxcharacter >= 0) {
        var stringInfo = utils_helper.getCharacterLength(val, props2.maxcharacter);
        val = _typeof__default["default"](stringInfo) === "object" && stringInfo.characters;
      }
      !isComposing.value && setInnerValue(val, {
        e: e
      });
      vue.nextTick(function () {
        return setInputValue(val);
      });
      adjustTextareaHeight();
    };
    var handleInput = function handleInput(e) {
      inputValueChangeHandle(e);
    };
    var onCompositionstart = function onCompositionstart() {
      isComposing.value = true;
    };
    var onCompositionend = function onCompositionend(e) {
      isComposing.value = false;
      inputValueChangeHandle(e);
    };
    var eventDeal = function eventDeal(name2, e) {
      var _props2$_name;
      if (disabled.value) return;
      var _name = "on".concat(name2[0].toUpperCase()).concat(name2.slice(1));
      (_props2$_name = props2[_name]) === null || _props2$_name === void 0 ? void 0 : _props2$_name.call(props2, innerValue.value, {
        e: e
      });
    };
    var emitKeyDown = function emitKeyDown(e) {
      eventDeal("keydown", e);
    };
    var emitKeyUp = function emitKeyUp(e) {
      eventDeal("keyup", e);
    };
    var emitKeypress = function emitKeypress(e) {
      eventDeal("keypress", e);
    };
    var emitFocus = function emitFocus(e) {
      var _props2$onFocus;
      adjustTextareaHeight();
      if (disabled.value) return;
      focused.value = true;
      (_props2$onFocus = props2.onFocus) === null || _props2$onFocus === void 0 ? void 0 : _props2$onFocus.call(props2, innerValue.value, {
        e: e
      });
    };
    var formItem = vue.inject(form_const.FormItemInjectionKey, void 0);
    var emitBlur = function emitBlur(e) {
      var _props2$onBlur;
      adjustTextareaHeight();
      focused.value = false;
      (_props2$onBlur = props2.onBlur) === null || _props2$onBlur === void 0 ? void 0 : _props2$onBlur.call(props2, innerValue.value, {
        e: e
      });
      formItem === null || formItem === void 0 ? void 0 : formItem.handleBlur();
    };
    var textareaClasses = vue.computed(function () {
      var _ref2;
      return [name.value, (_ref2 = {}, _defineProperty__default["default"](_ref2, "".concat(prefix.value, "-is-disabled"), disabled.value), _defineProperty__default["default"](_ref2, "".concat(prefix.value, "-is-readonly"), props2.readonly), _ref2)];
    });
    var inputAttrs = vue.computed(function () {
      return getValidAttrs({
        autofocus: props2.autofocus,
        disabled: disabled.value,
        readonly: props2.readonly,
        placeholder: props2.placeholder,
        maxlength: props2.maxlength || void 0,
        name: props2.name || void 0
      });
    });
    var characterNumber = vue.computed(function () {
      var characterInfo = utils_helper.getCharacterLength(String(innerValue.value || ""));
      if (_typeof__default["default"](characterInfo) === "object") return characterInfo.length;
      return characterInfo;
    });
    vue.watch(function () {
      return innerValue.value;
    }, function () {
      return adjustTextareaHeight();
    });
    vue.watch(refTextareaElem, function (el) {
      if (!el) return;
      adjustTextareaHeight();
    });
    vue.watch(function () {
      return props2.autofocus;
    }, function (val) {
      if (val) refTextareaElem.value.focus();
    });
    vue.watch(textareaStyle, function (val) {
      var style = attrs.style;
      _common_js_utils_setStyle["default"](refTextareaElem.value, merge__default["default"](style, val));
    });
    expose({
      focus: focus,
      blur: blur
    });
    vue.onMounted(function () {
      adjustTextareaHeight();
    });
    var renderTNodeJSX = hooks_tnode.useTNodeJSX();
    return function () {
      var _String;
      var inputEvents = getValidAttrs({
        onFocus: emitFocus,
        onBlur: emitBlur,
        onKeydown: emitKeyDown,
        onKeyup: emitKeyUp,
        onKeypress: emitKeypress
      });
      var _useCommonClassName = hooks_useConfig.useCommonClassName(),
        STATUS = _useCommonClassName.STATUS;
      var classes = vue.computed(function () {
        var _ref3;
        return ["".concat(name.value, "__inner"), (_ref3 = {}, _defineProperty__default["default"](_ref3, "".concat(prefix.value, "-is-").concat(props2.status), props2.status), _defineProperty__default["default"](_ref3, STATUS.value.disabled, disabled.value), _defineProperty__default["default"](_ref3, STATUS.value.focused, focused.value), _defineProperty__default["default"](_ref3, "".concat(prefix.value, "-resize-none"), _typeof__default["default"](props2.autosize) === "object"), _ref3), "narrow-scrollbar"];
      });
      var tips = renderTNodeJSX("tips");
      var textTips = tips && vue.createVNode("div", {
        "class": "".concat(TEXTAREA_TIPS_CLASS.value, " ").concat(name.value, "__tips--").concat(props2.status || "normal")
      }, [tips]);
      var limitText = props2.maxcharacter && vue.createVNode("span", {
        "class": TEXTAREA_LIMIT.value
      }, ["".concat(characterNumber.value, "/").concat(props2.maxcharacter)]) || !props2.maxcharacter && props2.maxlength && vue.createVNode("span", {
        "class": TEXTAREA_LIMIT.value
      }, ["".concat(innerValue.value ? (_String = String(innerValue.value)) === null || _String === void 0 ? void 0 : _String.length : 0, "/").concat(props2.maxlength)]);
      return vue.createVNode("div", vue.mergeProps({
        "class": textareaClasses.value
      }, utils_helper.omit(attrs, ["style"])), [vue.createVNode("textarea", vue.mergeProps({
        "onInput": handleInput,
        "onCompositionstart": onCompositionstart,
        "onCompositionend": onCompositionend,
        "ref": refTextareaElem,
        "value": innerValue.value,
        "class": classes.value
      }, inputEvents, inputAttrs.value), null), textTips || limitText ? vue.createVNode("div", {
        "class": ["".concat(name.value, "__info_wrapper"), _defineProperty__default["default"]({}, "".concat(name.value, "__info_wrapper_align"), !textTips)]
      }, [textTips, limitText]) : null]);
    };
  }
});

exports["default"] = _Textarea;
//# sourceMappingURL=textarea.js.map
