/**
 * ldesign v0.2.22
 * (c) 2022/12/7 16:21 built by swimly
 * @license MIT
 */

'use strict';

var log = require('./log.js');

var SIZE_MAP = {
  B: 1,
  KB: 1024,
  MB: 1048576,
  GB: 1073741824
};
function returnFileSize(number) {
  if (number < SIZE_MAP.KB) return "".concat(number, " Bytes");
  if (number >= SIZE_MAP.KB && number < SIZE_MAP.MB) return "".concat((number / SIZE_MAP.KB).toFixed(1), " KB");
  if (number >= SIZE_MAP.MB && number < SIZE_MAP.GB) return "".concat((number / SIZE_MAP.MB).toFixed(1), " MB");
  if (number >= SIZE_MAP.GB) return "".concat((number / SIZE_MAP.GB).toFixed(1), " GB");
  return "";
}
function getCurrentDate() {
  var needTime = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : false;
  var d = new Date();
  var month = d.getMonth() + 1;
  month = month < 10 ? "0".concat(month) : month;
  var date = "".concat(d.getFullYear(), "-").concat(month, "-").concat(d.getDate());
  var time = "".concat(d.getHours(), ":").concat(d.getMinutes(), ":").concat(d.getSeconds());
  if (needTime) return [date, time].join(" ");
  return date;
}
function abridgeName(inputName) {
  var leftCount = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : 5;
  var rightCount = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : 7;
  var name = inputName;
  var leftLength = 0;
  var rightLength = 0;
  if (!name) return "";
  for (var i = 0; i < name.length; i++) {
    var w = name[i];
    var isCn = escape(w).indexOf("%u") === 0;
    if (i < leftCount * 2 && leftLength < leftCount) isCn ? leftLength += 1 : leftLength += 2;else if (i > i - rightCount && rightLength < rightCount) isCn ? rightLength += 1 : rightLength += 2;
  }
  return name.replace(new RegExp("^(.{".concat(leftLength, "})(.+)(.{").concat(rightLength, "})$")), "$1\u2026$3");
}
function getFileSizeText(number) {
  if (number < 1024) return "".concat(number, " Bytes");
  if (number >= 1024 && number < 1048576) return "".concat((number / 1024).toFixed(1), " KB");
  if (number >= 1048576) return "".concat((number / 1048576).toFixed(1), " MB");
  return "";
}
function isOverSizeLimit(fileSize, sizeLimit, unit) {
  var units = ["B", "KB", "MB", "GB"];
  var KBIndex = 1;
  var index = units.indexOf(unit);
  if (index === -1) {
    log.log.warn("Upload", "`sizeLimit.unit` can only be one of ".concat(units.join()));
    index = KBIndex;
  }
  var num = SIZE_MAP[unit];
  return fileSize > sizeLimit * num;
}
function getFileUrlByFileRaw(fileRaw) {
  return new Promise(resolve => {
    if (!fileRaw) {
      resolve("");
      return;
    }
    var reader = new FileReader();
    reader.readAsDataURL(fileRaw);
    reader.onload = event => {
      var _event$target;
      resolve((_event$target = event.target) === null || _event$target === void 0 ? void 0 : _event$target.result);
    };
  });
}

exports.abridgeName = abridgeName;
exports.getCurrentDate = getCurrentDate;
exports.getFileSizeText = getFileSizeText;
exports.getFileUrlByFileRaw = getFileUrlByFileRaw;
exports.isOverSizeLimit = isOverSizeLimit;
exports.returnFileSize = returnFileSize;
//# sourceMappingURL=utils2.js.map
