/**
 * 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 isNumber = require('lodash/isNumber');
var isObject = require('lodash/isObject');
var _common_js_log_log = require('../_common/js/log/log.js');
var _common_js_utils_helper = require('../_common/js/utils/helper.js');
require('@babel/runtime/helpers/toConsumableArray');
require('@babel/runtime/helpers/objectWithoutProperties');
require('@babel/runtime/helpers/defineProperty');
require('lodash/isString');
require('lodash/isNull');
require('lodash/isUndefined');
require('lodash/isArray');

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

var _slicedToArray__default = /*#__PURE__*/_interopDefaultLegacy(_slicedToArray);
var isNumber__default = /*#__PURE__*/_interopDefaultLegacy(isNumber);
var isObject__default = /*#__PURE__*/_interopDefaultLegacy(isObject);

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 _common_js_utils_helper.limitUnicodeMaxLength(inputValue, maxlength);
    }
    if (maxcharacter) {
      var r = _common_js_utils_helper.getCharacterLength(inputValue, maxcharacter);
      if (isObject__default["default"](r)) return r.characters;
    }
  };
  var limitNumber = vue.computed(function () {
    var _params$value2 = params.value,
      maxlength = _params$value2.maxlength,
      maxcharacter = _params$value2.maxcharacter,
      value = _params$value2.value;
    if (isNumber__default["default"](value)) return String(value);
    if (maxlength && maxcharacter) _common_js_log_log["default"].warn("Input", "Pick one of maxlength and maxcharacter please.");
    if (maxlength) {
      var length = value !== null && value !== void 0 && value.length ? _common_js_utils_helper.getUnicodeLength(value) : 0;
      return "".concat(length, "/").concat(maxlength);
    }
    if (maxcharacter) return "".concat(_common_js_utils_helper.getCharacterLength(value || ""), "/").concat(maxcharacter);
    return "";
  });
  var innerStatus = vue.computed(function () {
    if (limitNumber.value) {
      var _limitNumber$value$sp = limitNumber.value.split("/"),
        _limitNumber$value$sp2 = _slicedToArray__default["default"](_limitNumber$value$sp, 2),
        current = _limitNumber$value$sp2[0],
        total = _limitNumber$value$sp2[1];
      return Number(current) > Number(total) ? "error" : "";
    }
    return "";
  });
  var tStatus = vue.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
    });
  };
  vue.watch(innerStatus, onValidateChange);
  vue.onMounted(function () {
    innerStatus.value && onValidateChange();
  });
  return {
    tStatus: tStatus,
    limitNumber: limitNumber,
    getValueByLimitNumber: getValueByLimitNumber
  };
}

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