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

'use strict';

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

var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray');
var _typeof = require('@babel/runtime/helpers/typeof');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var tdesignIconsVue = require('tdesign-icons-vue');
var camelCase = require('lodash/camelCase');
var kebabCase = require('lodash/kebabCase');
var _common_js_utils_helper = require('../_common/js/utils/helper.js');
var utils_helper = require('../utils/helper.js');
var configProvider_configReceiver = require('../config-provider/config-receiver.js');
var utils_mixins = require('../utils/mixins.js');
var utils_event = require('../utils/event.js');
var input_props = require('./props.js');
var utils_renderTnode = require('../utils/render-tnode.js');
require('@babel/runtime/helpers/objectWithoutProperties');
require('@babel/runtime/helpers/slicedToArray');
require('vue');
require('lodash/mergeWith');
require('../config-provider/context.js');
require('lodash/merge');
require('../_common/js/global-config/default-config.js');
require('../_common/js/global-config/locale/zh_CN.js');

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

var _toConsumableArray__default = /*#__PURE__*/_interopDefaultLegacy(_toConsumableArray);
var _typeof__default = /*#__PURE__*/_interopDefaultLegacy(_typeof);
var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
var camelCase__default = /*#__PURE__*/_interopDefaultLegacy(camelCase);
var kebabCase__default = /*#__PURE__*/_interopDefaultLegacy(kebabCase);

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 (typeof obj[key] !== "undefined") {
      newObj[key] = obj[key];
    }
  });
  return newObj;
}

var _Input = utils_mixins["default"](configProvider_configReceiver["default"]("input"), configProvider_configReceiver.getGlobalIconMixins()).extend({
  name: "TInput",
  inheritAttrs: false,
  props: _objectSpread(_objectSpread({}, input_props["default"]), {}, {
    showInput: {
      type: Boolean,
      "default": true
    },
    keepWrapperWidth: {
      type: Boolean,
      "default": false
    }
  }),
  data: function data() {
    return {
      formDisabled: void 0,
      isHover: false,
      focused: false,
      renderType: this.type,
      inputValue: this.value,
      composingRef: false,
      composingRefValue: 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() {
      var _this$autocomplete;

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

      return [this.componentName, this.commonSizeClassName[this.size] || "", (_ref = {}, _defineProperty__default["default"](_ref, this.commonStatusClassName.disabled, this.tDisabled), _defineProperty__default["default"](_ref, this.commonStatusClassName.focused, this.focused), _defineProperty__default["default"](_ref, "".concat(this.classPrefix, "-is-").concat(this.status), this.status), _defineProperty__default["default"](_ref, "".concat(this.classPrefix, "-align-").concat(this.align), this.align !== "left"), _defineProperty__default["default"](_ref, "".concat(this.classPrefix, "-is-disabled"), this.tDisabled), _defineProperty__default["default"](_ref, "".concat(this.classPrefix, "-is-readonly"), this.readonly), _defineProperty__default["default"](_ref, "".concat(this.componentName, "--focused"), this.focused), _defineProperty__default["default"](_ref, "".concat(this.componentName, "--auto-width"), this.autoWidth && !this.keepWrapperWidth), _ref)];
    }
  },
  watch: {
    autofocus: {
      handler: function handler(val) {
        var _this = this;

        if (val === true) {
          this.$nextTick(function () {
            var input = _this.$refs.inputRef;
            input === null || input === void 0 ? void 0 : input.focus();
          });
        }
      },
      immediate: true
    },
    value: {
      handler: function handler(val) {
        this.inputValue = val;
      },
      immediate: true
    },
    type: {
      handler: function handler(val) {
        this.renderType = 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__default["default"](iconType)]) {
        return this.$scopedSlots[kebabCase__default["default"](iconType)](null);
      }

      if (this.$scopedSlots[camelCase__default["default"](iconType)]) {
        return this.$scopedSlots[camelCase__default["default"](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() {
      this.$refs.inputRef.focus();
    },
    blur: function blur() {
      this.$refs.inputRef.blur();
    },
    handleKeydown: function handleKeydown(e) {
      if (this.tDisabled) return;
      var code = e.code || e.key;
      var value = e.currentTarget.value;

      if (code === "Enter" || code === "NumpadEnter") {
        utils_event.emitEvent(this, "enter", value, {
          e: e
        });
      } else {
        utils_event.emitEvent(this, "keydown", value, {
          e: e
        });
      }
    },
    handleKeyUp: function handleKeyUp(e) {
      if (this.tDisabled) return;
      var value = e.currentTarget.value;

      if (e.key === "Process") {
        return;
      }

      utils_event.emitEvent(this, "keyup", value, {
        e: e
      });
    },
    handleKeypress: function handleKeypress(e) {
      if (this.tDisabled) return;
      var value = e.currentTarget.value;
      utils_event.emitEvent(this, "keypress", value, {
        e: e
      });
    },
    onHandlePaste: function onHandlePaste(e) {
      if (this.tDisabled) return;
      var clipData = e.clipboardData || window.clipboardData;
      utils_event.emitEvent(this, "paste", {
        e: e,
        pasteValue: clipData === null || clipData === void 0 ? void 0 : clipData.getData("text/plain")
      });
    },
    onHandleMousewheel: function onHandleMousewheel(e) {
      utils_event.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) {
      utils_event.emitEvent(this, "clear", {
        e: e
      });
      utils_event.emitEvent(this, "change", "", {
        e: e
      });
    },
    emitFocus: function emitFocus(e) {
      this.inputValue = this.value;
      if (this.tDisabled) return;
      this.focused = true;
      utils_event.emitEvent(this, "focus", this.value, {
        e: e
      });
    },
    formatAndEmitBlur: function formatAndEmitBlur(e) {
      var _this$tFormItem;

      if (this.format) {
        this.inputValue = this.format(this.value);
      }

      this.focused = false;
      (_this$tFormItem = this.tFormItem) === null || _this$tFormItem === void 0 ? void 0 : _this$tFormItem.validate("blur");
      utils_event.emitEvent(this, "blur", this.value, {
        e: e
      });
    },
    compositionstartHandler: function compositionstartHandler(e) {
      var _this$onCompositionst;

      this.composingRef = true;
      var value = e.currentTarget.value;
      this.composingRefValue = value;
      this === null || this === void 0 ? void 0 : (_this$onCompositionst = this.onCompositionstart) === null || _this$onCompositionst === void 0 ? void 0 : _this$onCompositionst.call(this, value, {
        e: e
      });
    },
    compositionendHandler: function compositionendHandler(e) {
      var _this$onCompositionen;

      var value = e.currentTarget.value;

      if (this.composingRef) {
        this.composingRef = false;
        this.handleInput(e);
      }

      this.composingRefValue = "";
      this === null || this === void 0 ? void 0 : (_this$onCompositionen = this.onCompositionend) === null || _this$onCompositionen === void 0 ? void 0 : _this$onCompositionen.call(this, value, {
        e: 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);
    },
    handleInput: function handleInput(e) {
      var _this3 = this;

      var val = e.currentTarget.value;

      if (this.composingRef) {
        this.composingRefValue = val;
      } else {
        val = _common_js_utils_helper.limitUnicodeMaxLength(val, this.maxlength);

        if (this.maxcharacter && this.maxcharacter >= 0) {
          var stringInfo = utils_helper.getCharacterLength(val, this.maxcharacter);
          val = _typeof__default["default"](stringInfo) === "object" && stringInfo.characters;
        }

        utils_event.emitEvent(this, "change", val, {
          e: e
        });
        this.$nextTick(function () {
          _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;

      if (this.$refs.inputRef) {
        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 = utils_helper.omit(this.$attrs, Object.keys(this.inputAttrs));
    var wrapperEvents = utils_helper.omit(this.$listeners, [].concat(_toConsumableArray__default["default"](Object.keys(inputEvents)), ["input", "paste"]));
    var prefixIcon = this.renderIcon(h, this.prefixIcon, "prefix-icon");
    var suffixIcon = this.renderIcon(h, this.suffixIcon, "suffix-icon");
    var passwordIcon = this.renderIcon(h, void 0, "password-icon");
    var label = utils_renderTnode.renderTNodeJSX(this, "label");
    var suffix = utils_renderTnode.renderTNodeJSX(this, "suffix");
    var labelContent = label ? h("div", {
      "class": "".concat(this.componentName, "__prefix")
    }, [label]) : null;
    var suffixContent = suffix ? h("div", {
      "class": "".concat(this.componentName, "__suffix")
    }, [suffix]) : null;

    var _this$useGlobalIcon = this.useGlobalIcon({
      BrowseIcon: tdesignIconsVue.BrowseIcon,
      BrowseOffIcon: tdesignIconsVue.BrowseOffIcon,
      CloseCircleFilledIcon: tdesignIconsVue.CloseCircleFilledIcon
    }),
        BrowseIcon = _this$useGlobalIcon.BrowseIcon,
        BrowseOffIcon = _this$useGlobalIcon.BrowseOffIcon,
        CloseCircleFilledIcon = _this$useGlobalIcon.CloseCircleFilledIcon;

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

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

    var classes = [this.inputClasses, this.inputClass, (_ref2 = {}, _defineProperty__default["default"](_ref2, "".concat(this.componentName, "--prefix"), prefixIcon || labelContent), _defineProperty__default["default"](_ref2, "".concat(this.componentName, "--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(this.componentName, "__prefix"), "".concat(this.componentName, "__prefix-icon")]
    }, [prefixIcon]) : null, labelContent, this.showInput && h("input", {
      "attrs": _objectSpread({}, this.inputAttrs),
      "on": _objectSpread(_objectSpread({}, inputEvents), {}, {
        "input": this.handleInput,
        "compositionstart": this.compositionstartHandler,
        "compositionend": this.compositionendHandler
      }),
      "ref": "inputRef",
      "class": "".concat(this.componentName, "__inner"),
      "domProps": {
        "value": this.composingRef ? this.composingRefValue : this.inputValue
      }
    }), this.autoWidth && h("span", {
      "ref": "inputPreRef",
      "class": "".concat(this.classPrefix, "-input__input-pre")
    }, [this.value || this.tPlaceholder]), suffixContent, passwordIcon ? h("span", {
      "class": ["".concat(this.componentName, "__suffix"), "".concat(this.componentName, "__suffix-icon"), "".concat(this.componentName, "__clear")]
    }, [passwordIcon]) : null, suffixIcon ? h("span", {
      "class": ["".concat(this.componentName, "__suffix"), "".concat(this.componentName, "__suffix-icon"), _defineProperty__default["default"]({}, "".concat(this.componentName, "__clear"), this.showClear)]
    }, [suffixIcon]) : null]);
    var tips = utils_renderTnode.renderTNodeJSX(this, "tips");
    return h("div", {
      "class": "".concat(this.componentName, "__wrap")
    }, [inputNode, tips && h("div", {
      "class": "".concat(this.componentName, "__tips ").concat(this.componentName, "__tips--").concat(this.status || "normal")
    }, [tips])]);
  }
});

exports["default"] = _Input;
//# sourceMappingURL=input.js.map
