/**
 * ldesign v0.3.13
 * (c) 2023 ldesign
 * @license MIT
 */

import { _ as _slicedToArray } from '../_chunks/dep-84acf3c2.mjs';
import { toRefs, ref, computed, nextTick, inject, watch } from 'vue';
import { FormItemInjectionKey } from '../form/const.mjs';
import useVModel from '../hooks/useVModel.mjs';
import { useFormDisabled } from '../form/hooks.mjs';
import useLengthLimit from './useLengthLimit.mjs';
import '../_chunks/dep-b8d7992c.mjs';
import '../hooks/index.mjs';
import '../hooks/icon.mjs';
import '../_chunks/dep-5717e263.mjs';
import '../_chunks/dep-eba0ea6e.mjs';
import '../_chunks/dep-469b4ae0.mjs';
import '../_chunks/dep-255303ba.mjs';
import '../_chunks/dep-a8bcacea.mjs';
import '../hooks/slot.mjs';
import '../_chunks/dep-b57180d0.mjs';
import '../hooks/tnode.mjs';
import '../_chunks/dep-406cd044.mjs';
import '../_chunks/dep-42cc3015.mjs';
import '../_chunks/dep-c8d7b6b4.mjs';
import '../_chunks/dep-2bc37d52.mjs';
import '../_chunks/dep-3da99438.mjs';
import '../_chunks/dep-327559e8.mjs';
import '../_chunks/dep-b2235f26.mjs';
import '../_chunks/dep-8b989474.mjs';
import '../_chunks/dep-1372b2d3.mjs';
import '../_chunks/dep-9a85dac1.mjs';
import '../utils/render-tnode.mjs';
import '../_chunks/dep-888e80c4.mjs';
import '../_chunks/dep-334a4f6b.mjs';
import '../_chunks/dep-e5eb9f23.mjs';
import '../_chunks/dep-9fe2ef12.mjs';
import '../_chunks/dep-672e7d1a.mjs';
import '../_chunks/dep-4d7a675b.mjs';
import '../_chunks/dep-2e7f59b1.mjs';
import '../_chunks/dep-1434aa15.mjs';
import '../_chunks/dep-9262ee2f.mjs';
import '../_chunks/dep-876fb3ec.mjs';
import '../hooks/useConfig.mjs';
import '../config-provider/useConfig.mjs';
import '../_chunks/dep-095107f7.mjs';
import '../_chunks/dep-c989dff3.mjs';
import '../_chunks/dep-9cf86cf1.mjs';
import '../_chunks/dep-bc1d737a.mjs';
import '../_chunks/dep-855db8bc.mjs';
import '../_chunks/dep-a87f3e4c.mjs';
import '../_chunks/dep-1e566bb5.mjs';
import '../_chunks/dep-42a19209.mjs';
import '../_chunks/dep-8d3e9aa3.mjs';
import '../_chunks/dep-40d69a9c.mjs';
import '../_chunks/dep-dcc83161.mjs';
import '../_chunks/dep-0fc02184.mjs';
import '../_chunks/dep-f1994936.mjs';
import '../_chunks/dep-345e1be3.mjs';
import '../_common/js/global-config/default-config.mjs';
import '../_common/js/global-config/locale/zh_CN.mjs';
import '../_chunks/dep-9888611c.mjs';
import '../_chunks/dep-697a1365.mjs';
import '../config-provider/type.mjs';
import '../hooks/useCommonClassName.mjs';
import '../hooks/useDefaultValue.mjs';
import '../hooks/useDestroyOnClose.mjs';
import '../hooks/useKeepAnimation.mjs';
import '../hooks/useRipple.mjs';
import '../utils/set-style.mjs';
import '../hooks/useVirtualScroll.mjs';
import '../_chunks/dep-2db7b3ec.mjs';
import '../_chunks/dep-3f21269a.mjs';
import '../_common/js/log/index.mjs';
import '../_common/js/log/log.mjs';
import '../_common/js/utils/helper.mjs';
import '../_chunks/dep-bf1e33c8.mjs';
import '../_chunks/dep-60fae668.mjs';
import '../_chunks/dep-ed5ebb35.mjs';
import '../_chunks/dep-78847b2e.mjs';

function useInput(props, expose) {
  var _toRefs = toRefs(props),
    value = _toRefs.value,
    modelValue = _toRefs.modelValue;
  var inputValue = ref();
  var clearIconRef = ref(null);
  var innerClickElement = ref();
  var disabled = useFormDisabled();
  var _useVModel = useVModel(value, modelValue, props.defaultValue, props.onChange),
    _useVModel2 = _slicedToArray(_useVModel, 2),
    innerValue = _useVModel2[0],
    setInnerValue = _useVModel2[1];
  var isHover = ref(false);
  var focused = ref(false);
  var renderType = ref(props.type);
  var inputRef = ref(null);
  var limitParams = computed(function () {
    return {
      value: [void 0, null].includes(innerValue.value) ? void 0 : String(innerValue.value),
      status: props.status,
      maxlength: props.maxlength,
      maxcharacter: props.maxcharacter,
      allowInputOverMax: props.allowInputOverMax,
      onValidate: props.onValidate
    };
  });
  var _useLengthLimit = useLengthLimit(limitParams),
    limitNumber = _useLengthLimit.limitNumber,
    getValueByLimitNumber = _useLengthLimit.getValueByLimitNumber,
    tStatus = _useLengthLimit.tStatus;
  var showClear = computed(function () {
    return (innerValue.value && !disabled.value && props.clearable && !props.readonly || props.showClearIconOnEmpty) && isHover.value;
  });
  var focus = function focus() {
    var _inputRef$value;
    return (_inputRef$value = inputRef.value) === null || _inputRef$value === void 0 ? void 0 : _inputRef$value.focus();
  };
  var blur = function blur() {
    var _inputRef$value2;
    return (_inputRef$value2 = inputRef.value) === null || _inputRef$value2 === void 0 ? void 0 : _inputRef$value2.blur();
  };
  var emitFocus = function emitFocus(e) {
    var _props$onFocus;
    inputValue.value = innerValue.value;
    if (props.disabled || props.readonly) return;
    focused.value = true;
    (_props$onFocus = props.onFocus) === null || _props$onFocus === void 0 ? void 0 : _props$onFocus.call(props, innerValue.value, {
      e: e
    });
  };
  var emitClear = function emitClear(_ref) {
    var _props$onClear;
    var e = _ref.e;
    setInnerValue("", {
      e: e,
      trigger: "clear"
    });
    (_props$onClear = props.onClear) === null || _props$onClear === void 0 ? void 0 : _props$onClear.call(props, {
      e: e
    });
  };
  var onClearIconMousedown = function onClearIconMousedown(e) {
    innerClickElement.value = e.target;
  };
  var emitPassword = function emitPassword() {
    var toggleType = renderType.value === "password" ? "text" : "password";
    renderType.value = toggleType;
  };
  var setInputElValue = function setInputElValue() {
    var v = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "";
    var inputEl = inputRef.value;
    if (!inputEl) return;
    var sV = String(v);
    if (!inputEl.value) return;
    if (inputEl.value !== sV) inputEl.value = sV;
  };
  var inputValueChangeHandle = function inputValueChangeHandle(e) {
    var _innerValue$value;
    var target = e.target;
    var val = target.value;
    if (props.type !== "number" && val.length > ((_innerValue$value = innerValue.value) === null || _innerValue$value === void 0 ? void 0 : _innerValue$value.length)) val = getValueByLimitNumber(val);
    setInnerValue(val, {
      e: e
    });
    nextTick(function () {
      setInputElValue(innerValue.value);
    });
  };
  var handleInput = function handleInput(e) {
    var checkInputType = e.inputType && e.inputType === "insertCompositionText";
    if (e.isComposing || checkInputType) return;
    inputValueChangeHandle(e);
  };
  var isClearIcon = function isClearIcon() {
    var _clearIconRef$value;
    var tmp = innerClickElement.value;
    if (!tmp || !tmp.tagName || !((_clearIconRef$value = clearIconRef.value) !== null && _clearIconRef$value !== void 0 && _clearIconRef$value.$el) || !["path", "svg"].includes(tmp.tagName)) return false;
    while (tmp) {
      var _clearIconRef$value2;
      if (((_clearIconRef$value2 = clearIconRef.value) === null || _clearIconRef$value2 === void 0 ? void 0 : _clearIconRef$value2.$el) === tmp) return true;
      tmp = tmp.parentNode;
    }
    return false;
  };
  var formItem = inject(FormItemInjectionKey, void 0);
  var formatAndEmitBlur = function formatAndEmitBlur(e) {
    if (props.format) inputValue.value = props.format(innerValue.value);
    focused.value = false;
    if (!isClearIcon() && props.allowTriggerBlur) {
      var _props$onBlur;
      (_props$onBlur = props.onBlur) === null || _props$onBlur === void 0 ? void 0 : _props$onBlur.call(props, innerValue.value, {
        e: e
      });
      formItem === null || formItem === void 0 ? void 0 : formItem.handleBlur();
    }
  };
  var onHandleCompositionend = function onHandleCompositionend(e) {
    var _props$onCompositione;
    inputValueChangeHandle(e);
    (_props$onCompositione = props.onCompositionend) === null || _props$onCompositione === void 0 ? void 0 : _props$onCompositione.call(props, innerValue.value, {
      e: e
    });
  };
  var onHandleCompositionstart = function onHandleCompositionstart(e) {
    var _props$onCompositions;
    (_props$onCompositions = props.onCompositionstart) === null || _props$onCompositions === void 0 ? void 0 : _props$onCompositions.call(props, innerValue.value, {
      e: e
    });
  };
  var onRootClick = function onRootClick(e) {
    var _inputRef$value3, _props$onClick;
    (_inputRef$value3 = inputRef.value) === null || _inputRef$value3 === void 0 ? void 0 : _inputRef$value3.focus();
    (_props$onClick = props.onClick) === null || _props$onClick === void 0 ? void 0 : _props$onClick.call(props, {
      e: e
    });
  };
  watch(function () {
    return props.autofocus;
  }, function (value2) {
    if (value2 === true) {
      nextTick(function () {
        var _inputRef$value4;
        (_inputRef$value4 = inputRef.value) === null || _inputRef$value4 === void 0 ? void 0 : _inputRef$value4.focus();
      });
    }
  }, {
    immediate: true
  });
  watch(innerValue, function (val, oldVal) {
    if (oldVal === void 0 && props.format) inputValue.value = props.format(val);else inputValue.value = val;
    var newVal = getValueByLimitNumber(val);
    if (newVal !== val && props.type !== "number") setInnerValue(newVal, {
      trigger: "initial"
    });
  }, {
    immediate: true
  });
  watch(function () {
    return props.type;
  }, function (v) {
    renderType.value = v;
  }, {
    immediate: true
  });
  expose({
    inputRef: inputRef,
    focus: focus,
    blur: blur
  });
  return {
    isHover: isHover,
    focused: focused,
    renderType: renderType,
    showClear: showClear,
    inputRef: inputRef,
    clearIconRef: clearIconRef,
    inputValue: inputValue,
    limitNumber: limitNumber,
    tStatus: tStatus,
    emitFocus: emitFocus,
    formatAndEmitBlur: formatAndEmitBlur,
    onHandleCompositionend: onHandleCompositionend,
    onHandleCompositionstart: onHandleCompositionstart,
    onRootClick: onRootClick,
    emitPassword: emitPassword,
    handleInput: handleInput,
    emitClear: emitClear,
    onClearIconMousedown: onClearIconMousedown,
    innerValue: innerValue
  };
}

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