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

import { _ as _slicedToArray } from '../_chunks/dep-84acf3c2.mjs';
import { computed, watch, onMounted } from 'vue';
import { i as isNumber_1 } from '../_chunks/dep-3f21269a.mjs';
import { i as isObject_1 } from '../_chunks/dep-a8bcacea.mjs';
import '../_common/js/log/index.mjs';
import { limitUnicodeMaxLength, getCharacterLength, getUnicodeLength } from '../_common/js/utils/helper.mjs';
import log from '../_common/js/log/log.mjs';
import '../_chunks/dep-b8d7992c.mjs';
import '../_chunks/dep-eba0ea6e.mjs';
import '../_chunks/dep-469b4ae0.mjs';
import '../_chunks/dep-255303ba.mjs';
import '../_chunks/dep-3da99438.mjs';
import '../_chunks/dep-bf1e33c8.mjs';
import '../_chunks/dep-60fae668.mjs';
import '../_chunks/dep-2db7b3ec.mjs';
import '../_chunks/dep-876fb3ec.mjs';
import '../_chunks/dep-b57180d0.mjs';
import '../_chunks/dep-c8d7b6b4.mjs';
import '../_chunks/dep-ed5ebb35.mjs';
import '../_chunks/dep-78847b2e.mjs';

function useLengthLimit(params) {
  var getValueByLimitNumber = function getValueByLimitNumber(inputValue) {
    var _params$value = params.value,
      allowInputOverMax = _params$value.allowInputOverMax,
      maxlength = _params$value.maxlength,
      maxcharacter = _params$value.maxcharacter;
    if (!(maxlength || maxcharacter) || allowInputOverMax || !inputValue) return inputValue;
    if (maxlength) {
      return limitUnicodeMaxLength(inputValue, maxlength);
    }
    if (maxcharacter) {
      var r = getCharacterLength(inputValue, maxcharacter);
      if (isObject_1(r)) return r.characters;
    }
  };
  var limitNumber = computed(function () {
    var _params$value2 = params.value,
      maxlength = _params$value2.maxlength,
      maxcharacter = _params$value2.maxcharacter,
      value = _params$value2.value;
    if (isNumber_1(value)) return String(value);
    if (maxlength && maxcharacter) log.warn("Input", "Pick one of maxlength and maxcharacter please.");
    if (maxlength) {
      var length = value !== null && value !== void 0 && value.length ? getUnicodeLength(value) : 0;
      return "".concat(length, "/").concat(maxlength);
    }
    if (maxcharacter) return "".concat(getCharacterLength(value || ""), "/").concat(maxcharacter);
    return "";
  });
  var innerStatus = computed(function () {
    if (limitNumber.value) {
      var _limitNumber$value$sp = limitNumber.value.split("/"),
        _limitNumber$value$sp2 = _slicedToArray(_limitNumber$value$sp, 2),
        current = _limitNumber$value$sp2[0],
        total = _limitNumber$value$sp2[1];
      return Number(current) > Number(total) ? "error" : "";
    }
    return "";
  });
  var tStatus = computed(function () {
    var status = params.value.status;
    return status || innerStatus.value;
  });
  var onValidateChange = function onValidateChange() {
    var _params$value$onValid, _params$value3;
    (_params$value$onValid = (_params$value3 = params.value).onValidate) === null || _params$value$onValid === void 0 ? void 0 : _params$value$onValid.call(_params$value3, {
      error: innerStatus.value ? "exceed-maximum" : void 0
    });
  };
  watch(innerStatus, onValidateChange);
  onMounted(function () {
    innerStatus.value && onValidateChange();
  });
  return {
    tStatus: tStatus,
    limitNumber: limitNumber,
    getValueByLimitNumber: getValueByLimitNumber
  };
}

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