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

import { _ as _slicedToArray } from '../_chunks/dep-55846619.mjs';
import { computed, watch, onMounted } from 'vue';
import { i as isNumber_1 } from '../_chunks/dep-6eea369f.mjs';
import { i as isObject_1 } from '../_chunks/dep-0f0c89ff.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-cfcedf47.mjs';
import '../_chunks/dep-a0b4a044.mjs';
import '../_chunks/dep-c959a6fb.mjs';
import '../_chunks/dep-b4041200.mjs';
import '../_chunks/dep-c31e1d9e.mjs';
import '../_chunks/dep-e091cfd9.mjs';
import '../_chunks/dep-b6a73f93.mjs';
import '../_chunks/dep-eb20e23f.mjs';
import '../_chunks/dep-1cf2b129.mjs';
import '../_chunks/dep-e3b14d70.mjs';
import '../_chunks/dep-66d6733b.mjs';
import '../_chunks/dep-6f89b77e.mjs';
import '../_chunks/dep-30451c48.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
