/**
 * tdesign v0.32.0
 * (c) 2021 tdesign
 * @license MIT
 */

import _typeof from '@babel/runtime/helpers/typeof';
import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import { CloseCircleFilledIcon, BrowseOffIcon, BrowseIcon } from 'tdesign-icons-vue';
import { omit, getCharacterLength } 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 '@babel/runtime/helpers/objectWithoutProperties';
import '@babel/runtime/helpers/slicedToArray';
import 'lodash/camelCase';
import 'vue';
import '../config-provider/zh_CN_config.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");

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 {
      isHover: false,
      focused: false,
      renderType: this.type
    };
  },
  computed: {
    showClear: function showClear() {
      return this.value && !this.disabled && this.clearable && this.isHover;
    },
    inputAttrs: function inputAttrs() {
      return getValidAttrs({
        autofocus: this.autofocus,
        disabled: this.disabled,
        readonly: this.readonly,
        autocomplete: this.autocomplete,
        placeholder: this.placeholder || this.t(this.global.placeholder),
        maxlength: this.maxlength,
        name: this.name || void 0,
        type: this.renderType
      });
    }
  },
  watch: {
    autofocus: {
      handler: function handler(val) {
        var _this = this;

        if (val === true) {
          this.$nextTick(function () {
            _this.$refs.refInputElem.focus();
          });
        }
      },
      immediate: true
    }
  },
  created: function created() {
    this.composing = false;
  },
  render: function render(h) {
    var _ref,
        _this2 = this;

    var inputEvents = getValidAttrs({
      focus: this.emitFocus,
      blur: this.emitBlur,
      keydown: this.handleKeydown,
      keyup: this.handleKeyUp,
      keypress: this.handleKeypress,
      change: function change() {}
    });
    var wrapperAttrs = omit(this.$attrs, Object.keys(this.inputAttrs));
    var wrapperEvents = omit(this.$listeners, [].concat(_toConsumableArray(Object.keys(inputEvents)), ["input"]));
    var prefixIcon = this.renderIcon(h, this.prefixIcon, "prefix-icon");
    var suffixIcon = this.renderIcon(h, this.suffixIcon, "suffix-icon");

    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 = [name, ClASSNAMES.SIZE[this.size] || "", (_ref = {}, _defineProperty(_ref, ClASSNAMES.STATUS.disabled, this.disabled), _defineProperty(_ref, ClASSNAMES.STATUS.focused, this.focused), _defineProperty(_ref, "".concat(prefix, "-is-").concat(this.status), this.status), _defineProperty(_ref, "".concat(name, "--prefix"), prefixIcon), _defineProperty(_ref, "".concat(name, "--suffix"), suffixIcon), _ref)];
    return h("div", {
      "class": classes,
      "on": _objectSpread({
        "mouseenter": function mouseenter() {
          return _this2.mouseEvent(true);
        },
        "mouseleave": function mouseleave() {
          return _this2.mouseEvent(false);
        }
      }, wrapperEvents),
      "attrs": _objectSpread({}, wrapperAttrs)
    }, [prefixIcon ? h("span", {
      "class": "".concat(name, "__prefix")
    }, [prefixIcon]) : null, h("input", {
      "attrs": _objectSpread({}, this.inputAttrs),
      "on": _objectSpread(_objectSpread({}, inputEvents), {}, {
        "input": this.handleInput,
        "compositionend": this.onCompositionend
      }),
      "ref": "refInputElem",
      "domProps": {
        "value": this.value
      },
      "class": "".concat(name, "__inner")
    }), suffixIcon ? h("span", {
      "class": ["".concat(name, "__suffix"), _defineProperty({}, "".concat(name, "__clear"), this.showClear)]
    }, [suffixIcon]) : null]);
  },
  methods: {
    mouseEvent: function mouseEvent(v) {
      this.isHover = v;
    },
    renderIcon: function renderIcon(h, icon, iconType) {
      if (typeof icon === "function") {
        return icon(h);
      }

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

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

      if (input.value !== sV) {
        input.value = sV;
      }
    },
    focus: function focus() {
      var input = this.$refs.refInputElem;
      input === null || input === void 0 ? void 0 : input.focus();
    },
    blur: function blur() {
      var input = this.$refs.refInputElem;
      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.disabled) return;
      var code = e.code;

      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.disabled) return;
      emitEvent(this, "keyup", this.value, {
        e: e
      });
    },
    handleKeypress: function handleKeypress(e) {
      if (this.disabled) return;
      emitEvent(this, "keypress", this.value, {
        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) {
      if (this.disabled) return;
      this.focused = true;
      emitEvent(this, "focus", this.value, {
        e: e
      });
    },
    emitBlur: function emitBlur(e) {
      this.focused = false;
      emitEvent(this, "blur", this.value, {
        e: e
      });
    },
    onCompositionend: function onCompositionend(e) {
      this.inputValueChangeHandle(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);
      });
    }
  }
});

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