/**
 * ldesign v0.2.2
 * (c) 2022年9月20日 swimly
 * @license MIT
 */

import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
import _typeof from '@babel/runtime/helpers/typeof';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import { BrowseIcon, BrowseOffIcon, CloseCircleFilledIcon } from '@ldesign/icons-vue';
import camelCase from 'lodash/camelCase';
import kebabCase from 'lodash/kebabCase';
import { l as limitUnicodeMaxLength } from '../../chunks/helper2.js';
import { getCharacterLength, omit } from '../../utils/helper.js';
import getConfigReceiverMixins, { getGlobalIconMixins } from '../config-provider/config-receiver.js';
import mixins from '../../utils/mixins.js';
import { emitEvent } from '../../utils/event.js';
import props from './props.js';
import { renderLNodeJSX } from '../../utils/render-node.js';
import '@babel/runtime/helpers/slicedToArray';
import '../../chunks/defineProperty-28395935.js';
import '@babel/runtime/helpers/objectWithoutProperties';
import 'vue';
import 'lodash/mergeWith';
import 'lodash';
import '../config-provider/context.js';
import '../../chunks/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; }

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"), getGlobalIconMixins()).extend({
  name: "LInput",
  inheritAttrs: false,
  props: _objectSpread(_objectSpread({}, props), {}, {
    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(_ref, this.commonStatusClassName.disabled, this.tDisabled), _defineProperty(_ref, this.commonStatusClassName.focused, this.focused), _defineProperty(_ref, "".concat(this.classPrefix, "-is-").concat(this.status), this.status), _defineProperty(_ref, "".concat(this.classPrefix, "-align-").concat(this.align), this.align !== "left"), _defineProperty(_ref, "".concat(this.classPrefix, "-is-disabled"), this.tDisabled), _defineProperty(_ref, "".concat(this.classPrefix, "-is-readonly"), this.readonly), _defineProperty(_ref, "".concat(this.componentName, "--focused"), this.focused), _defineProperty(_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(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() {
      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") {
        emitEvent(this, "enter", value, {
          e: e
        });
      } else {
        emitEvent(this, "keydown", value, {
          e: e
        });
      }
    },
    handleKeyUp: function handleKeyUp(e) {
      if (this.tDisabled) return;
      var value = e.currentTarget.value;

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

      emitEvent(this, "keyup", value, {
        e: e
      });
    },
    handleKeypress: function handleKeypress(e) {
      if (this.tDisabled) return;
      var value = e.currentTarget.value;
      emitEvent(this, "keypress", 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) {
      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");
      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 = limitUnicodeMaxLength(val, this.maxlength);

        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 () {
          _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 = 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 passwordIcon = this.renderIcon(h, void 0, "password-icon");
    var label = renderLNodeJSX(this, "label");
    var suffix = renderLNodeJSX(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: BrowseIcon,
      BrowseOffIcon: BrowseOffIcon,
      CloseCircleFilledIcon: CloseCircleFilledIcon
    }),
        BrowseIcon$1 = _this$useGlobalIcon.BrowseIcon,
        BrowseOffIcon$1 = _this$useGlobalIcon.BrowseOffIcon,
        CloseCircleFilledIcon$1 = _this$useGlobalIcon.CloseCircleFilledIcon;

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

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

    var classes = [this.inputClasses, this.inputClass, (_ref2 = {}, _defineProperty(_ref2, "".concat(this.componentName, "--prefix"), prefixIcon || labelContent), _defineProperty(_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({}, "".concat(this.componentName, "__clear"), this.showClear)]
    }, [suffixIcon]) : null]);
    var tips = renderLNodeJSX(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])]);
  }
});

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