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

import { BrowseIcon, BrowseOffIcon, CloseCircleFilledIcon } from '@ldesign/icons-vue';
import camelCase from 'lodash/camelCase';
import kebabCase from 'lodash/kebabCase';
import { l as limitUnicodeMaxLength } from '../../_chunks/dep-d640115b.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 'vue';
import 'lodash/mergeWith';
import 'lodash';
import '../config-provider/context.js';
import '../../_chunks/dep-b1584b65.js';

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

var _Input = mixins(getConfigReceiverMixins("input"), getGlobalIconMixins()).extend({
  name: "LInput",
  inheritAttrs: false,
  props: { ...props,
    showInput: {
      type: Boolean,
      default: true
    },
    keepWrapperWidth: {
      type: Boolean,
      default: false
    }
  },

  data() {
    return {
      formDisabled: void 0,
      isHover: false,
      focused: false,
      renderType: this.type,
      inputValue: this.value,
      composingRef: false,
      composingRefValue: this.value
    };
  },

  computed: {
    tDisabled() {
      return this.formDisabled || this.disabled;
    },

    tPlaceholder() {
      return this.placeholder ?? this.t(this.global.placeholder);
    },

    showClear() {
      return (this.value && !this.disabled && this.clearable && !this.readonly || this.showClearIconOnEmpty) && this.isHover;
    },

    inputAttrs() {
      return getValidAttrs({
        autofocus: this.autofocus,
        disabled: this.tDisabled,
        readonly: this.readonly,
        autocomplete: this.autocomplete ?? this.global.autocomplete,
        placeholder: this.tPlaceholder,
        name: this.name || void 0,
        type: this.renderType,
        unselectable: this.readonly ? "on" : "off"
      });
    },

    inputClasses() {
      return [this.componentName, this.commonSizeClassName[this.size] || "", {
        [this.commonStatusClassName.disabled]: this.tDisabled,
        [this.commonStatusClassName.focused]: this.focused,
        [`${this.classPrefix}-is-${this.status}`]: this.status,
        [`${this.classPrefix}-align-${this.align}`]: this.align !== "left",
        [`${this.classPrefix}-is-disabled`]: this.tDisabled,
        [`${this.classPrefix}-is-readonly`]: this.readonly,
        [`${this.componentName}--focused`]: this.focused,
        [`${this.componentName}--auto-width`]: this.autoWidth && !this.keepWrapperWidth
      }];
    },

    inputWrapClass() {
      const wrapClass = `${this.componentName}__wrap`;
      return [`${wrapClass}`, {
        [`${wrapClass}--focused`]: this.focused
      }];
    }

  },
  watch: {
    autofocus: {
      handler(val) {
        if (val === true) {
          this.$nextTick(() => {
            const input = this.$refs.inputRef;
            input === null || input === void 0 ? void 0 : input.focus();
          });
        }
      },

      immediate: true
    },
    value: {
      handler(val) {
        this.inputValue = val;
      },

      immediate: true
    },
    type: {
      handler(val) {
        this.renderType = val;
      },

      immediate: true
    }
  },

  created() {
    this.composing = false;

    if (this.autoWidth) {
      this.addListeners();
    }
  },

  methods: {
    addListeners() {
      this.$watch(() => this.value + this.placeholder, () => {
        if (!this.autoWidth) return;
        this.$nextTick(() => {
          this.updateInputWidth();
        });
      }, {
        immediate: true
      });
    },

    mouseEvent(v) {
      this.isHover = v;
    },

    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() {
      let v = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "";
      const input = this.$refs.inputRef;
      if (!input) return;
      const sV = String(v);

      if (input.value !== sV) {
        input.value = sV;
      }
    },

    focus() {
      this.$refs.inputRef.focus();
    },

    blur() {
      this.$refs.inputRef.blur();
    },

    handleKeydown(e) {
      if (this.tDisabled) return;
      const code = e.code || e.key;
      const {
        currentTarget: {
          value
        }
      } = e;

      if (code === "Enter" || code === "NumpadEnter") {
        emitEvent(this, "enter", value, {
          e
        });
      } else {
        emitEvent(this, "keydown", value, {
          e
        });
      }
    },

    handleKeyUp(e) {
      if (this.tDisabled) return;
      const {
        currentTarget: {
          value
        }
      } = e;

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

      emitEvent(this, "keyup", value, {
        e
      });
    },

    handleKeypress(e) {
      if (this.tDisabled) return;
      const {
        currentTarget: {
          value
        }
      } = e;
      emitEvent(this, "keypress", value, {
        e
      });
    },

    onHandlePaste(e) {
      if (this.tDisabled) return;
      const clipData = e.clipboardData || window.clipboardData;
      emitEvent(this, "paste", {
        e,
        pasteValue: clipData === null || clipData === void 0 ? void 0 : clipData.getData("text/plain")
      });
    },

    onHandleMousewheel(e) {
      emitEvent(this, "wheel", {
        e
      });
    },

    emitPassword() {
      const {
        renderType
      } = this;
      const toggleType = renderType === "password" ? "text" : "password";
      this.renderType = toggleType;
    },

    emitClear(e) {
      emitEvent(this, "clear", {
        e
      });
      emitEvent(this, "change", "", {
        e
      });
    },

    emitFocus(e) {
      this.inputValue = this.value;
      if (this.tDisabled) return;
      this.focused = true;
      emitEvent(this, "focus", this.value, {
        e
      });
    },

    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
      });
    },

    compositionstartHandler(e) {
      var _this$onCompositionst;

      this.composingRef = true;
      const {
        currentTarget: {
          value
        }
      } = e;
      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
      });
    },

    compositionendHandler(e) {
      var _this$onCompositionen;

      const {
        currentTarget: {
          value
        }
      } = e;

      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
      });
    },

    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(e) {
      let {
        currentTarget: {
          value: val
        }
      } = e;

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

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

        emitEvent(this, "change", val, {
          e
        });
        this.$nextTick(() => {
          this.setInputValue(this.value);
        });
      }
    },

    onInputMouseenter(e) {
      var _this$onMouseenter;

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

    onInputMouseleave(e) {
      var _this$onMouseleave;

      this.mouseEvent(false);
      (_this$onMouseleave = this.onMouseleave) === null || _this$onMouseleave === void 0 ? void 0 : _this$onMouseleave.call(this, {
        e
      });
    },

    updateInputWidth() {
      const pre = this.$refs.inputPreRef;
      if (!pre) return;
      const width = pre.offsetWidth;

      if (this.$refs.inputRef) {
        this.$refs.inputRef.style.width = `${width}px`;
      }
    }

  },

  render(h) {
    const inputEvents = getValidAttrs({
      focus: this.emitFocus,
      blur: this.formatAndEmitBlur,
      keydown: this.handleKeydown,
      keyup: this.handleKeyUp,
      keypress: this.handleKeypress,
      paste: this.onHandlePaste,
      change: () => {}
    });
    const wrapperAttrs = omit(this.$attrs, Object.keys(this.inputAttrs));
    const wrapperEvents = omit(this.$listeners, [...Object.keys(inputEvents), "input", "paste"]);
    const prefixIcon = this.renderIcon(h, this.prefixIcon, "prefix-icon");
    let suffixIcon = this.renderIcon(h, this.suffixIcon, "suffix-icon");
    let passwordIcon = this.renderIcon(h, void 0, "password-icon");
    const label = renderLNodeJSX(this, "label");
    const suffix = renderLNodeJSX(this, "suffix");
    const labelContent = label ? h("div", {
      "class": `${this.componentName}__prefix`
    }, [label]) : null;
    const suffixContent = suffix ? h("div", {
      "class": `${this.componentName}__suffix`
    }, [suffix]) : null;
    const {
      BrowseIcon: BrowseIcon$1,
      BrowseOffIcon: BrowseOffIcon$1,
      CloseCircleFilledIcon: CloseCircleFilledIcon$1
    } = this.useGlobalIcon({
      BrowseIcon: BrowseIcon,
      BrowseOffIcon: BrowseOffIcon,
      CloseCircleFilledIcon: CloseCircleFilledIcon
    });

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

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

    const classes = [this.inputClasses, this.inputClass, {
      [`${this.componentName}--prefix`]: prefixIcon || labelContent,
      [`${this.componentName}--suffix`]: suffixIcon || suffixContent
    }];
    const inputNode = h("div", {
      "class": classes,
      "on": {
        "click": this.onRootClick,
        ...wrapperEvents,
        "mouseenter": this.onInputMouseenter,
        "mouseleave": this.onInputMouseleave,
        "wheel": this.onHandleMousewheel
      },
      "attrs": { ...wrapperAttrs
      }
    }, [prefixIcon ? h("span", {
      "class": [`${this.componentName}__prefix`, `${this.componentName}__prefix-icon`]
    }, [prefixIcon]) : null, labelContent, this.showInput && h("input", {
      "attrs": { ...this.inputAttrs
      },
      "on": { ...inputEvents,
        "input": this.handleInput,
        "compositionstart": this.compositionstartHandler,
        "compositionend": this.compositionendHandler
      },
      "ref": "inputRef",
      "class": `${this.componentName}__inner`,
      "domProps": {
        "value": this.composingRef ? this.composingRefValue : this.inputValue
      }
    }), this.autoWidth && h("span", {
      "ref": "inputPreRef",
      "class": `${this.classPrefix}-input__input-pre`
    }, [this.value || this.tPlaceholder]), suffixContent, passwordIcon ? h("span", {
      "class": [`${this.componentName}__suffix`, `${this.componentName}__suffix-icon`, `${this.componentName}__clear`]
    }, [passwordIcon]) : null, suffixIcon ? h("span", {
      "class": [`${this.componentName}__suffix`, `${this.componentName}__suffix-icon`, {
        [`${this.componentName}__clear`]: this.showClear
      }]
    }, [suffixIcon]) : null]);
    const tips = renderLNodeJSX(this, "tips");
    return h("div", {
      "class": this.inputWrapClass
    }, [inputNode, tips && h("div", {
      "class": `${this.componentName}__tips ${this.componentName}__tips--${this.status}`
    }, [tips])]);
  }

});

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