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

import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
import _typeof from '@babel/runtime/helpers/typeof';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import { CloseCircleFilledIcon, BrowseOffIcon, BrowseIcon } from 'tdesign-icons-vue';
import camelCase from 'lodash/camelCase';
import kebabCase from 'lodash/kebabCase';
import { getCharacterLength, omit } from '../utils/helper.js';
import getConfigReceiverMixins from '../config-provider/config-receiver.js';
import mixins from '../utils/mixins.js';
import ClASSNAMES from '../utils/classnames.js';
import { emitEvent } from '../utils/event.js';
import { prefix } from '../config.js';
import props from './props.js';
import { renderTNodeJSX } from '../utils/render-tnode.js';
import '@babel/runtime/helpers/objectWithoutProperties';
import '@babel/runtime/helpers/slicedToArray';
import 'vue';
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 name = "".concat(prefix, "-input");
var INPUT_WRAP_CLASS = "".concat(prefix, "-input__wrap");
var INPUT_TIPS_CLASS = "".concat(prefix, "-input__tips");

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

var _Input = mixins(getConfigReceiverMixins("input")).extend({
  name: "TInput",
  inheritAttrs: false,
  props: _objectSpread({}, props),
  data: function data() {
    return {
      formDisabled: void 0,
      isHover: false,
      focused: false,
      renderType: this.type,
      inputValue: this.value
    };
  },
  computed: {
    tDisabled: function tDisabled() {
      return this.formDisabled || this.disabled;
    },
    tPlaceholder: function tPlaceholder() {
      var _this$placeholder;

      return (_this$placeholder = this.placeholder) !== null && _this$placeholder !== void 0 ? _this$placeholder : this.t(this.global.placeholder);
    },
    showClear: function showClear() {
      return (this.value && !this.disabled && this.clearable && !this.readonly || this.showClearIconOnEmpty) && this.isHover;
    },
    inputAttrs: function inputAttrs() {
      return getValidAttrs({
        autofocus: this.autofocus,
        disabled: this.tDisabled,
        readonly: this.readonly,
        autocomplete: this.autocomplete,
        placeholder: this.tPlaceholder,
        maxlength: this.maxlength,
        name: this.name || void 0,
        type: this.renderType,
        unselectable: this.readonly ? "on" : "off"
      });
    },
    inputClasses: function inputClasses() {
      var _ref;

      return [name, ClASSNAMES.SIZE[this.size] || "", (_ref = {}, _defineProperty(_ref, ClASSNAMES.STATUS.disabled, this.tDisabled), _defineProperty(_ref, ClASSNAMES.STATUS.focused, this.focused), _defineProperty(_ref, "".concat(prefix, "-is-").concat(this.status), this.status), _defineProperty(_ref, "".concat(prefix, "-align-").concat(this.align), this.align !== "left"), _defineProperty(_ref, "".concat(prefix, "-is-disabled"), this.tDisabled), _defineProperty(_ref, "".concat(prefix, "-is-readonly"), this.readonly), _defineProperty(_ref, "".concat(name, "--focused"), this.focused), _defineProperty(_ref, "".concat(name, "--auto-width"), this.autoWidth), _ref)];
    }
  },
  watch: {
    autofocus: {
      handler: function handler(val) {
        var _this = this;

        if (val === true) {
          this.$nextTick(function () {
            _this.$refs.inputRef.focus();
          });
        }
      },
      immediate: true
    },
    value: {
      handler: function handler(val) {
        this.inputValue = val;
      },
      immediate: true
    }
  },
  created: function created() {
    this.composing = false;

    if (this.autoWidth) {
      this.addListeners();
    }
  },
  methods: {
    addListeners: function addListeners() {
      var _this2 = this;

      this.$watch(function () {
        return _this2.value + _this2.placeholder;
      }, function () {
        if (!_this2.autoWidth) return;

        _this2.$nextTick(function () {
          _this2.updateInputWidth();
        });
      }, {
        immediate: true
      });
    },
    mouseEvent: function mouseEvent(v) {
      this.isHover = v;
    },
    renderIcon: function renderIcon(h, icon, iconType) {
      if (typeof icon === "function") {
        return icon(h);
      }

      if (this.$scopedSlots[kebabCase(iconType)]) {
        return this.$scopedSlots[kebabCase(iconType)](null);
      }

      if (this.$scopedSlots[camelCase(iconType)]) {
        return this.$scopedSlots[camelCase(iconType)](null);
      }

      return null;
    },
    setInputValue: function setInputValue() {
      var v = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "";
      var input = this.$refs.inputRef;
      if (!input) return;
      var sV = String(v);

      if (input.value !== sV) {
        input.value = sV;
      }
    },
    focus: function focus() {
      var input = this.$refs.inputRef;
      input === null || input === void 0 ? void 0 : input.focus();
    },
    blur: function blur() {
      var input = this.$refs.inputRef;
      input === null || input === void 0 ? void 0 : input.blur();
    },
    handleInput: function handleInput(e) {
      var isCheckInputType = e.inputType && e.inputType === "insertCompositionText";
      if (e.isComposing || isCheckInputType) return;
      this.inputValueChangeHandle(e);
    },
    handleKeydown: function handleKeydown(e) {
      if (this.tDisabled) return;
      var code = e.code || e.key;

      if (code === "Enter" || code === "NumpadEnter") {
        emitEvent(this, "enter", this.value, {
          e: e
        });
      } else {
        emitEvent(this, "keydown", this.value, {
          e: e
        });
      }
    },
    handleKeyUp: function handleKeyUp(e) {
      if (this.tDisabled) return;
      emitEvent(this, "keyup", this.value, {
        e: e
      });
    },
    handleKeypress: function handleKeypress(e) {
      if (this.tDisabled) return;
      emitEvent(this, "keypress", this.value, {
        e: e
      });
    },
    onHandlePaste: function onHandlePaste(e) {
      if (this.tDisabled) return;
      var clipData = e.clipboardData || window.clipboardData;
      emitEvent(this, "paste", {
        e: e,
        pasteValue: clipData === null || clipData === void 0 ? void 0 : clipData.getData("text/plain")
      });
    },
    onHandleMousewheel: function onHandleMousewheel(e) {
      emitEvent(this, "wheel", {
        e: e
      });
    },
    emitPassword: function emitPassword() {
      var renderType = this.renderType;
      var toggleType = renderType === "password" ? "text" : "password";
      this.renderType = toggleType;
    },
    emitClear: function emitClear(e) {
      emitEvent(this, "clear", {
        e: e
      });
      emitEvent(this, "change", "", {
        e: e
      });
    },
    emitFocus: function emitFocus(e) {
      this.inputValue = this.value;
      if (this.tDisabled) return;
      this.focused = true;
      emitEvent(this, "focus", this.value, {
        e: e
      });
    },
    formatAndEmitBlur: function formatAndEmitBlur(e) {
      if (this.format) {
        this.inputValue = this.format(this.value);
      }

      this.focused = false;
      emitEvent(this, "blur", this.value, {
        e: e
      });
    },
    compositionendHandler: function compositionendHandler(e) {
      this.inputValueChangeHandle(e);
    },
    onRootClick: function onRootClick(e) {
      var _this$$refs$inputRef;

      (_this$$refs$inputRef = this.$refs.inputRef) === null || _this$$refs$inputRef === void 0 ? void 0 : _this$$refs$inputRef.focus();
      this.$emit("click", e);
    },
    inputValueChangeHandle: function inputValueChangeHandle(e) {
      var _this3 = this;

      var target = e.target;
      var val = target.value;

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

      emitEvent(this, "change", val, {
        e: e
      });
      this.$nextTick(function () {
        return _this3.setInputValue(_this3.value);
      });
    },
    onInputMouseenter: function onInputMouseenter(e) {
      var _this$onMouseenter;

      this.mouseEvent(true);
      (_this$onMouseenter = this.onMouseenter) === null || _this$onMouseenter === void 0 ? void 0 : _this$onMouseenter.call(this, {
        e: e
      });
    },
    onInputMouseleave: function onInputMouseleave(e) {
      var _this$onMouseleave;

      this.mouseEvent(false);
      (_this$onMouseleave = this.onMouseleave) === null || _this$onMouseleave === void 0 ? void 0 : _this$onMouseleave.call(this, {
        e: e
      });
    },
    updateInputWidth: function updateInputWidth() {
      var pre = this.$refs.inputPreRef;
      if (!pre) return;
      var width = pre.offsetWidth;
      this.$refs.inputRef.style.width = "".concat(width, "px");
    }
  },
  render: function render(h) {
    var _ref2;

    var inputEvents = getValidAttrs({
      focus: this.emitFocus,
      blur: this.formatAndEmitBlur,
      keydown: this.handleKeydown,
      keyup: this.handleKeyUp,
      keypress: this.handleKeypress,
      paste: this.onHandlePaste,
      change: function change() {}
    });
    var wrapperAttrs = omit(this.$attrs, Object.keys(this.inputAttrs));
    var wrapperEvents = omit(this.$listeners, [].concat(_toConsumableArray(Object.keys(inputEvents)), ["input", "paste"]));
    var prefixIcon = this.renderIcon(h, this.prefixIcon, "prefix-icon");
    var suffixIcon = this.renderIcon(h, this.suffixIcon, "suffix-icon");
    var label = renderTNodeJSX(this, "label");
    var suffix = renderTNodeJSX(this, "suffix");
    var labelContent = label ? h("div", {
      "class": "".concat(name, "__prefix")
    }, [label]) : null;
    var suffixContent = suffix ? h("div", {
      "class": "".concat(name, "__suffix")
    }, [suffix]) : null;

    if (this.showClear) {
      suffixIcon = h(CloseCircleFilledIcon, {
        "class": "".concat(name, "__suffix-clear"),
        "nativeOn": {
          "click": this.emitClear
        }
      });
    }

    if (this.type === "password") {
      if (this.renderType === "password") {
        suffixIcon = h(BrowseOffIcon, {
          "class": "".concat(name, "__suffix-clear"),
          "nativeOn": {
            "click": this.emitPassword
          }
        });
      } else if (this.renderType === "text") {
        suffixIcon = h(BrowseIcon, {
          "class": "".concat(name, "__suffix-clear"),
          "nativeOn": {
            "click": this.emitPassword
          }
        });
      }
    }

    var classes = [this.inputClasses, this.inputClass, (_ref2 = {}, _defineProperty(_ref2, "".concat(name, "--prefix"), prefixIcon || labelContent), _defineProperty(_ref2, "".concat(name, "--suffix"), suffixIcon || suffixContent), _ref2)];
    var inputNode = h("div", {
      "class": classes,
      "on": _objectSpread(_objectSpread({
        "click": this.onRootClick
      }, wrapperEvents), {}, {
        "mouseenter": this.onInputMouseenter,
        "mouseleave": this.onInputMouseleave,
        "wheel": this.onHandleMousewheel
      }),
      "attrs": _objectSpread({}, wrapperAttrs)
    }, [prefixIcon ? h("span", {
      "class": ["".concat(name, "__prefix"), "".concat(name, "__prefix-icon")]
    }, [prefixIcon]) : null, labelContent, h("input", {
      "attrs": _objectSpread({}, this.inputAttrs),
      "on": _objectSpread(_objectSpread({}, inputEvents), {}, {
        "input": this.handleInput,
        "compositionend": this.compositionendHandler
      }),
      "ref": "inputRef",
      "class": "".concat(name, "__inner"),
      "domProps": {
        "value": this.inputValue
      }
    }), this.autoWidth && h("span", {
      "ref": "inputPreRef",
      "class": "".concat(prefix, "-input__input-pre")
    }, [this.value || this.tPlaceholder]), suffixContent, suffixIcon ? h("span", {
      "class": ["".concat(name, "__suffix"), "".concat(name, "__suffix-icon"), _defineProperty({}, "".concat(name, "__clear"), this.showClear)]
    }, [suffixIcon]) : null]);
    var tips = renderTNodeJSX(this, "tips");
    return h("div", {
      "class": INPUT_WRAP_CLASS
    }, [inputNode, tips && h("div", {
      "class": "".concat(INPUT_TIPS_CLASS, " ").concat(prefix, "-input__tips--").concat(this.status || "normal")
    }, [tips])]);
  }
});

export { _Input as default };
//# sourceMappingURL=input.js.map
