import { defineComponent, shallowRef, ref, watch, createVNode } from 'vue';
import '../../../utils/index.mjs';
import { anyType } from '../../../utils/vue/propType.mjs';

const BaseInputCore = /* @__PURE__ */ defineComponent({
  name: "BaseInputCore",
  compatConfig: {
    MODE: 3
  },
  inheritAttrs: false,
  props: {
    disabled: {
      type: Boolean
    },
    type: String,
    value: anyType(),
    lazy: {
      type: Boolean,
      default: true
    },
    tag: {
      type: String,
      default: "input"
    },
    size: String
  },
  emits: ["change", "input", "blur", "keydown", "focus", "compositionstart", "compositionend", "keyup"],
  setup(props, {
    emit,
    attrs,
    expose
  }) {
    const inputRef = shallowRef(null);
    const renderValue = ref();
    const isComposing = ref(false);
    watch([() => props.value, isComposing], () => {
      if (isComposing.value)
        return;
      renderValue.value = props.value;
    }, {
      immediate: true
    });
    const handleChange = (e) => {
      emit("change", e);
    };
    const onCompositionstart = (e) => {
      isComposing.value = true;
      e.target.composing = true;
      emit("compositionstart", e);
    };
    const onCompositionend = (e) => {
      isComposing.value = false;
      e.target.composing = false;
      emit("compositionend", e);
      const event = document.createEvent("HTMLEvents");
      event.initEvent("input", true, true);
      e.target.dispatchEvent(event);
    };
    const handleInput = (e) => {
      if (isComposing.value && props.lazy) {
        renderValue.value = e.target.value;
        return;
      }
      emit("input", e);
    };
    const handleBlur = (e) => {
      emit("blur", e);
    };
    const handleFocus = (e) => {
      emit("focus", e);
    };
    const focus = () => {
      if (inputRef.value)
        inputRef.value.focus();
    };
    const blur = () => {
      if (inputRef.value)
        inputRef.value.blur();
    };
    const handleKeyDown = (e) => {
      emit("keydown", e);
    };
    const handleKeyUp = (e) => {
      emit("keyup", e);
    };
    const setSelectionRange = (start, end, direction) => {
      var _a;
      (_a = inputRef.value) == null ? void 0 : _a.setSelectionRange(start, end, direction);
    };
    const select = () => {
      var _a;
      (_a = inputRef.value) == null ? void 0 : _a.select();
    };
    expose({
      focus,
      blur,
      input: inputRef,
      setSelectionRange,
      select,
      getSelectionStart: () => {
        var _a;
        return (_a = inputRef.value) == null ? void 0 : _a.selectionStart;
      },
      getSelectionEnd: () => {
        var _a;
        return (_a = inputRef.value) == null ? void 0 : _a.selectionEnd;
      },
      getScrollTop: () => {
        var _a;
        return (_a = inputRef.value) == null ? void 0 : _a.scrollTop;
      }
    });
    return () => {
      const {
        tag: Tag,
        ...restProps
      } = props;
      return createVNode(Tag, {
        ...restProps,
        ...attrs,
        "onInput": handleInput,
        "onChange": handleChange,
        "onBlur": handleBlur,
        "onFocus": handleFocus,
        "ref": inputRef,
        "value": renderValue.value,
        "onCompositionstart": onCompositionstart,
        "onCompositionend": onCompositionend,
        "onKeyup": handleKeyUp,
        "onKeydown": handleKeyDown
      }, null);
    };
  }
});

export { BaseInputCore as default };
//# sourceMappingURL=BaseInputCore.mjs.map
