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

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
var vue = require('vue');
var form_const = require('../form/const.js');
var hooks_useVModel = require('../hooks/useVModel.js');
var form_hooks = require('../form/hooks.js');
var input_useLengthLimit = require('./useLengthLimit.js');
require('../hooks/useConfig.js');
require('../config-provider/useConfig.js');
require('lodash/cloneDeep');
require('lodash/isFunction');
require('lodash/isString');
require('../config-provider/context.js');
require('lodash/mergeWith');
require('lodash/merge');
require('lodash/isArray');
require('../_common/js/global-config/default-config.js');
require('../_common/js/global-config/locale/zh_CN.js');
require('@babel/runtime/helpers/typeof');
require('../_chunks/dep-7f0559c3.js');
require('dayjs');
require('lodash/kebabCase');
require('lodash/isNumber');
require('lodash/isObject');
require('../_common/js/log/log.js');
require('../_common/js/utils/helper.js');
require('@babel/runtime/helpers/toConsumableArray');
require('@babel/runtime/helpers/objectWithoutProperties');
require('@babel/runtime/helpers/defineProperty');
require('lodash/isNull');
require('lodash/isUndefined');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _slicedToArray__default = /*#__PURE__*/_interopDefaultLegacy(_slicedToArray);

function useInput(props, expose) {
  var _toRefs = vue.toRefs(props),
    value = _toRefs.value,
    modelValue = _toRefs.modelValue;
  var inputValue = vue.ref();
  var isComposition = vue.ref(false);
  var compositionValue = vue.ref();
  var clearIconRef = vue.ref(null);
  var innerClickElement = vue.ref();
  var disabled = form_hooks.useFormDisabled();
  var _useVModel = hooks_useVModel["default"](value, modelValue, props.defaultValue, props.onChange),
    _useVModel2 = _slicedToArray__default["default"](_useVModel, 2),
    innerValue = _useVModel2[0],
    setInnerValue = _useVModel2[1];
  var isHover = vue.ref(false);
  var focused = vue.ref(false);
  var renderType = vue.ref(props.type);
  var inputRef = vue.ref(null);
  var limitParams = vue.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 = input_useLengthLimit["default"](limitParams),
    limitNumber = _useLengthLimit.limitNumber,
    getValueByLimitNumber = _useLengthLimit.getValueByLimitNumber,
    tStatus = _useLengthLimit.tStatus;
  var showClear = vue.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
    });
    vue.nextTick(function () {
      setInputElValue(innerValue.value);
    });
  };
  var handleInput = function handleInput(e) {
    var checkInputType = e.inputType && e.inputType === "insertCompositionText";
    var val = e.currentTarget.value;
    if (checkInputType || isComposition.value) {
      compositionValue.value = val;
      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 = vue.inject(form_const.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;
    isComposition.value = false;
    compositionValue.value = "";
    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;
    isComposition.value = true;
    var value2 = e.currentTarget.value;
    compositionValue.value = value2;
    (_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
    });
  };
  vue.watch(function () {
    return props.autofocus;
  }, function (value2) {
    if (value2 === true) {
      vue.nextTick(function () {
        var _inputRef$value4;
        (_inputRef$value4 = inputRef.value) === null || _inputRef$value4 === void 0 ? void 0 : _inputRef$value4.focus();
      });
    }
  }, {
    immediate: true
  });
  vue.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
  });
  vue.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,
    isComposition: isComposition,
    compositionValue: compositionValue,
    limitNumber: limitNumber,
    tStatus: tStatus,
    emitFocus: emitFocus,
    formatAndEmitBlur: formatAndEmitBlur,
    onHandleCompositionend: onHandleCompositionend,
    onHandleCompositionstart: onHandleCompositionstart,
    onRootClick: onRootClick,
    emitPassword: emitPassword,
    handleInput: handleInput,
    emitClear: emitClear,
    onClearIconMousedown: onClearIconMousedown,
    innerValue: innerValue
  };
}

exports["default"] = useInput;
//# sourceMappingURL=useInput.js.map
